A Babel plugin that automatically adds /*#__PURE__*/ annotations to specific React method calls to enable better tree shaking optimization by bundlers like Webpack and Rollup. The plugin identifies React method calls whether they're imported as named imports or accessed through default/namespace imports, and marks them as pure functions that can be safely eliminated during dead code elimination if their results are unused.
npm install --save-dev @babel/plugin-transform-react-pure-annotationsThis is a Babel plugin, so it's typically configured in your Babel configuration rather than imported directly:
{
"plugins": ["@babel/plugin-transform-react-pure-annotations"]
}For programmatic usage with Babel API:
import transformReactPureAnnotations from "@babel/plugin-transform-react-pure-annotations";The plugin internally uses these imports (for plugin development reference):
import { declare } from "@babel/helper-plugin-utils";
import annotateAsPure from "@babel/helper-annotate-as-pure";
import { types as t, type NodePath } from "@babel/core";Add the plugin to your Babel configuration file (.babelrc, babel.config.js, or package.json):
{
"plugins": ["@babel/plugin-transform-react-pure-annotations"]
}{
"presets": ["@babel/preset-react"],
"plugins": ["@babel/plugin-transform-react-pure-annotations"]
}import { transform } from "@babel/core";
import transformReactPureAnnotations from "@babel/plugin-transform-react-pure-annotations";
const result = transform(code, {
plugins: [transformReactPureAnnotations]
});Input:
import React from 'react';
import { createContext, memo } from 'react';
import * as ReactAll from 'react';
import ReactDOM from 'react-dom';
const element = React.createElement('div');
const context = createContext();
const memoized = memo(Component);
const namespaced = ReactAll.forwardRef(Component);
const portal = ReactDOM.createPortal(element, container);Output:
import React from 'react';
import { createContext, memo } from 'react';
import * as ReactAll from 'react';
import ReactDOM from 'react-dom';
const element = /*#__PURE__*/React.createElement('div');
const context = /*#__PURE__*/createContext();
const memoized = /*#__PURE__*/memo(Component);
const namespaced = /*#__PURE__*/ReactAll.forwardRef(Component);
const portal = /*#__PURE__*/ReactDOM.createPortal(element, container);The plugin exports a default Babel plugin created using the declare helper from @babel/helper-plugin-utils.
/**
* A Babel plugin that adds pure annotations to React method calls
* Created using declare() from @babel/helper-plugin-utils
*/
declare const plugin: BabelPlugin;
export default plugin;
interface BabelPlugin {
name: string;
visitor: {
CallExpression(path: NodePath<t.CallExpression>): void;
};
}
type NodePath<T> = import('@babel/core').NodePath<T>;
type t = typeof import('@babel/core').types;The plugin implementation includes a visitor pattern that processes CallExpression nodes in the AST. It uses an internal helper function to identify React method calls:
/**
* Internal function that determines if a call expression is a React method call
* that should be annotated as pure. Handles both named imports and member expressions.
* @param path - Babel NodePath for the CallExpression
* @returns boolean indicating if the call should be marked as pure
*/
function isReactCall(path: NodePath<t.CallExpression>): boolean;The plugin processes two types of call patterns:
createContext() from import { createContext } from 'react'React.createContext() from import React from 'react' or import * as React from 'react'The plugin uses an internal PURE_CALLS mapping to define exactly which methods should be annotated as pure.
The plugin automatically annotates calls to the following React methods (as defined in the internal PURE_CALLS constant):
The plugin supports all common React import patterns:
// Named imports
import { createElement, memo } from 'react';
createElement('div'); // ✓ Will be annotated
memo(Component); // ✓ Will be annotated
// Default import
import React from 'react';
React.createElement('div'); // ✓ Will be annotated
React.memo(Component); // ✓ Will be annotated
// Namespace import
import * as React from 'react';
React.createElement('div'); // ✓ Will be annotated
React.memo(Component); // ✓ Will be annotated
// Mixed imports
import React, { memo } from 'react';
React.createElement('div'); // ✓ Will be annotated
memo(Component); // ✓ Will be annotatedThis plugin requires no configuration options. It works automatically once added to your Babel configuration.
@babel/core ^7.0.0-0The plugin uses the following Babel helpers:
@babel/helper-annotate-as-pure: Adds the pure annotations to AST nodes@babel/helper-plugin-utils: Provides the declare function for plugin creation