babel-plugin-pure-calls-annotation

raw JSON →
0.4.2 verified Sat Apr 25 auth: no javascript

A Babel plugin (v0.4.2, stable, low release cadence) that automatically adds /*#__PURE__*/ comments to call expressions in variable declarators, assignment expressions, call arguments, and other expression contexts. This helps bundlers like Webpack identify side-effect-free calls for tree shaking and dead code elimination. Unlike manual annotation, it eliminates human error and ensures consistent coverage across large codebases. Ships TypeScript types and integrates seamlessly into any Babel-based build pipeline.

error SyntaxError: 'babel-plugin-pure-calls-annotation' threw an error: Cannot find module '@babel/core'
cause Missing @babel/core as a peer dependency.
fix
npm install @babel/core --save-dev
error TypeError: Cannot read properties of undefined (reading 'callee')
cause The plugin expects a Babel AST node with a callee property; corrupted or outdated Babel version may cause this.
fix
Update Babel to version 7.x or above and ensure the plugin is compatible.
error Error: Plugin/preset files are not allowed to export objects, only functions.
cause This error occurs if the plugin is imported incorrectly (e.g., using named import instead of default).
fix
Use default import: import pureCallsAnnotation from 'babel-plugin-pure-calls-annotation'
gotcha The plugin annotates all eligible call expressions, including those that may have side effects. Use with caution; only include if you are certain the annotated calls are pure.
fix Manually verify or use other methods (e.g., /*#__PURE__*/ comments) to ensure purity.
breaking BREAKING: Version 0.4.0 changed annotation behavior for IIFEs: previously IIFEs were not annotated; now they are.
fix If you rely on IIFEs not being annotated, downgrade to <0.4.0 or update your code to handle the change.
deprecated The plugin may not be actively maintained; last release was in 2021. Consider alternatives like manual annotation or custom Babel plugin for tree shaking.
fix Review current tree shaking needs; use terser-webpack-plugin with sideEffects: false as a more maintained alternative.
gotcha Does not annotate call expressions in all positions (e.g., as standalone statements). Only variable declarators, assignment expressions, and call arguments are covered.
fix Manually add /*#__PURE__*/ to unannotated pure calls or extend the plugin.
npm install babel-plugin-pure-calls-annotation
yarn add babel-plugin-pure-calls-annotation
pnpm add babel-plugin-pure-calls-annotation

Add the plugin to Babel config to automatically annotate pure call expressions with /*#__PURE__*/ for tree shaking.

// babel.config.js
module.exports = {
  plugins: [
    'babel-plugin-pure-calls-annotation'
  ]
};

// Input: const result = compute()
// Output: const result = /*#__PURE__*/compute()