rollup-plugin-tree-shakeable
raw JSON → 2.0.0 verified Mon Apr 27 auth: no javascript
A Rollup plugin (v2.0.0, latest) that automatically adds @__PURE__ annotations to top-level expressions in your JavaScript/TypeScript modules, convincing bundlers like Rollup, esbuild, and webpack that the module is side-effect free and enabling effective tree-shaking. Unlike setting `sideEffects: false` in package.json, which cannot handle function calls at module scope, this plugin annotates each call individually. Requires Node >= 22 and Rollup. Lightweight, zero-config, and actively maintained with few dependencies.
Common errors
error Require is not defined in ES module scope ↓
cause Package uses ESM; require() is not supported without special configuration.
fix
Use import syntax or set "type": "module" in package.json and consume as ES module.
error Cannot find module 'rollup-plugin-tree-shakeable' ↓
cause Plugin not installed or path incorrect.
fix
Run npm install rollup-plugin-tree-shakeable --save-dev and ensure import path is correct.
error TypeError: treeShakeable is not a plugin function ↓
cause treeShakeable() returns a plugin object, but treeShakeable alone is the function, not a plugin.
fix
Call treeShakeable() in the plugins array: plugins: [treeShakeable()]
Warnings
breaking Only use if your package is actually tree-shakeable: each export works independently when others are stripped. ↓
fix Verify that your module does not have side-effectful top-level expressions that depend on other exports.
gotcha Does NOT handle all cases: side effects inside exported function bodies are not annotated. ↓
fix Manually add @__PURE__ or refactor code that produces side effects inside functions.
breaking v2.0.0 requires Node >= 22; older versions of Node are unsupported. ↓
fix Upgrade to Node 22 or stay on v1.0.3.
deprecated v1.x may not be receiving further updates; upgrade to v2 for ongoing maintenance. ↓
fix Update to latest: npm install rollup-plugin-tree-shakeable@latest
Install
npm install rollup-plugin-tree-shakeable yarn add rollup-plugin-tree-shakeable pnpm add rollup-plugin-tree-shakeable Imports
- default wrong
const treeShakeable = require('rollup-plugin-tree-shakeable')correctimport treeShakeable from 'rollup-plugin-tree-shakeable' - Plugin wrong
import { Plugin } from 'rollup-plugin-tree-shakeable'correctimport type { Plugin } from 'rollup' - application wrong
export default { plugins: [treeShakeable] }correctimport treeShakeable from 'rollup-plugin-tree-shakeable'; export default { plugins: [treeShakeable()] }
Quickstart
// install: npm i rollup rollup-plugin-tree-shakeable
import treeShakeable from 'rollup-plugin-tree-shakeable';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'esm',
},
plugins: [treeShakeable()],
};
// src/index.js (before plugin)
const withLogging = fn => (...args) => {
console.log('start');
try { return fn(...args); } finally { console.log('end'); }
};
export const f1 = withLogging(() => 1);
export const f2 = withLogging(() => 2);
// After plugin: each top-level call gets @__PURE__
// export const f1 = /* @__PURE__*/ withLogging(() => 1);
// export const f2 = /* @__PURE__*/ withLogging(() => 2);