rollup-plugin-pure
raw JSON → 0.4.0 verified Mon Apr 27 auth: no javascript
Rollup/Vite plugin that automatically injects /*#__PURE__*/ annotations before specific function calls and /*@__NO_SIDE_EFFECTS__*/ before function declarations, enabling better tree-shaking for libraries with definition functions like Vue's defineComponent. Current stable version is 0.4.0, actively maintained. Supports regex function names, uses AST-based approach, and handles Rollup's awareness of pure annotations. Key differentiator: automatic annotation injection without manual marks, works with Vite and Rollup 3/4.
Common errors
error Plugin 'rollup-plugin-pure' does not provide a default export, but a named export 'PluginPure' is available. ↓
cause Default import used instead of named import.
fix
Use import { PluginPure } from 'rollup-plugin-pure' instead.
error TypeError: PluginPure is not a constructor ↓
cause PluginPure is a function (factory), not a class. Calling new PluginPure() fails.
fix
Call PluginPure({...}) without new.
error Error: Must specify at least one function name or regex in 'functions' option ↓
cause Missing or empty functions array in configuration.
fix
Provide an array of function names or regex patterns in the functions option.
Warnings
breaking Plugin name changed in v0.3.0 from 'rollup-plugin-pure' to 'PluginPure' export ↓
fix Update import to correctly use named export PluginPure.
breaking AST-based approach introduced in v0.3.0; old RegExp-based approach removed ↓
fix Migrate to new configuration; see changelog for details.
gotcha When Rollup already marks a function as pure, the plugin may not add an annotation, leading to no effect ↓
fix Check if Rollup's built-in pure detection covers your functions; if not, adjust your configuration.
deprecated Support for Rollup <3.0 may be dropped in future versions ↓
fix Upgrade to Rollup 3 or 4.
gotcha Using `include` glob may not match expected files if patterns conflict with `exclude` ↓
fix Test patterns in the playground or use more specific include/exclude rules.
Install
npm install rollup-plugin-pure yarn add rollup-plugin-pure pnpm add rollup-plugin-pure Imports
- PluginPure wrong
import PluginPure from 'rollup-plugin-pure'correctimport { PluginPure } from 'rollup-plugin-pure' - PluginPure as rollupPluginPure wrong
const rollupPluginPure = require('rollup-plugin-pure').PluginPure;correctimport { PluginPure as rollupPluginPure } from 'rollup-plugin-pure' - PluginPure type wrong
import { PluginPure } from 'rollup-plugin-pure'correctimport type { PluginPure } from 'rollup-plugin-pure'
Quickstart
// vite.config.ts
import { PluginPure } from 'rollup-plugin-pure';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
PluginPure({
functions: ['defineComponent', /^define(Page|Meta)$/],
include: [/(?<!im)pure\.js$/],
// exclude: [],
// sourcemap: true,
}),
],
});