Babel Plugin
Thebabel-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
Opt-Out Mode
Compile everything except components with"use no memo" directive:
babel.config.js
File-Based Configuration
babel.config.js
Build Tool Integration
Webpack
Integrate withbabel-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
.babelrc
Create React App
Usereact-app-rewired or craco:
Rollup
Use with@rollup/plugin-babel:
rollup.config.js
esbuild
Use withesbuild-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
Usethread-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
ImplementdebugLogIRs 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