Skip to main content

Babel Plugin

The babel-plugin-react-compiler package is the primary interface for transforming React code with automatic memoization.

Installation

Basic Setup

Babel Configuration

Plugin Options

Pass configuration options as the second element in the plugin array:
babel.config.js

Option Structure

Compilation Modes

Compile All Components

By default, the compiler attempts to compile all components and hooks:

Opt-In Mode

Only compile components with "use memo" directive:
babel.config.js
Mark components for compilation:

Opt-Out Mode

Compile everything except components with "use no memo" directive:
babel.config.js
Skip specific components:

File-Based Configuration

babel.config.js

Build Tool Integration

Webpack

Integrate with babel-loader:
webpack.config.js

Vite

Configure with @vitejs/plugin-react:
vite.config.js

Next.js

Next.js 13.5+ has built-in support:
next.config.js
For older versions, use custom Babel config:
.babelrc

Create React App

Use react-app-rewired or craco:

Rollup

Use with @rollup/plugin-babel:
rollup.config.js

esbuild

Use with esbuild-plugin-babel:
build.js

Custom Logger

Implement custom logging for compiler events:
babel.config.js

Logger Interface

Performance Optimization

Enable Caching

webpack.config.js

Exclude node_modules

Parallel Processing

Use thread-loader for parallel compilation:
webpack.config.js

TypeScript Support

The plugin works seamlessly with TypeScript:
babel.config.js

TSX Files

webpack.config.js

Debugging

Enable Compiler Timing

Debug Output

Implement debugLogIRs in your logger:

Source Maps

Ensure source maps are enabled:
webpack.config.js

Troubleshooting

Plugin Order

React Compiler should run early in the plugin chain:

Conflicting Transforms

Avoid transforms that modify React code structure:

Memory Issues

For large codebases, increase Node memory:
package.json

Next Steps

Configuration

Detailed configuration options

ESLint Plugin

Set up React rules validation

How It Works

Understanding the compilation process

Contributing

Contribute to React Compiler