eslint-plugin-better-tree-shaking
ESLint plugin to improve tree-shaking of TypeScript code
When bundlers like Webpack, Rollup, or esbuild encounter top-level side effects, they can't safely remove unused code. This plugin detects these patterns and suggests annotating them with /* @__PURE__ */ comments to enable better tree-shaking.
The /* @__PURE__ */ annotation tells the bundler this call has no side effects and can be safely removed if result is unused.
Function Calls
Calling a function at the top level is a side effect because the bundler doesn't know if the function modifies global state.
Method Calls
Same applies to method calls - the bundler can't know if obj.method() has side effects.
Property Access Chains
Accessing nested properties like foo.bar.bazz can trigger getters, which are side effects. Wrap in an IIFE to mark as pure.
Objects with Side Effects
When an object contains multiple expressions with side effects (calls, computed keys), wrap the entire object in a pure IIFE.
Usage
Install the eslint-plugin-better-tree-shaking package, add it to the plugins section of your ESLint configuration file, and enable thebetter-tree-shaking/no-top-level-side-effects rule in the rules section.