The React Compiler is an experimental compiler introduced in React 19 that can automatically optimize your React code.
Before starting to use the React Compiler, it is recommended to read the React Compiler documentation to understand its features, current status, and usage.
If you are using React 19, Modern.js has built-in support for React Compiler, and no additional configuration is required.
If you are using React 17 or 18, you need to configure it as follows:
react-compiler-runtime to allow the compiled code to run on versions before 19:npm install react-compiler-runtimebabel-plugin-react-compiler:npm install babel-plugin-react-compilerimport { appTools, defineConfig } from '@modern-js/app-tools';
export default defineConfig({
tools: {
babel(_, { addPlugins }) {
addPlugins([
[
'babel-plugin-react-compiler',
{
target: '18', // or '17', depending on your React version
},
],
]);
},
},
plugins: [appTools()],
});For detailed code, you can refer to the Modern.js & React Compiler example project