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.

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.
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.
npm install rollup-plugin-pure
yarn add rollup-plugin-pure
pnpm add rollup-plugin-pure

Configures Vite to annotate defineComponent and regex-matched functions as pure, targeting files ending with 'pure.js' except those starting with 'im'.

// 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,
    }),
  ],
});