Sub topic 7
Setting up the Babel plugin
This chapter assumes you can read a package.json, you know how a bundler such as Vite or webpack runs Babel, and you have a working React app. It covers installing the compiler, wiring it into a build, and checking that it ran. The options for gradual rollout and version targets are introduced here and covered in detail in later chapters.
What you are installing
The React Compiler ships as a Babel plugin named babel-plugin-react-compiler. It reads each source file, finds components and hooks, and rewrites them so that values are cached between renders. This is the same result you would get by writing useMemo, useCallback, and React.memo by hand.
The plugin runs at build time, so the output is plain JavaScript. Nothing about the compiler is loaded in the browser except a small runtime helper that comes with React 19.
Install the package
Install the plugin as a development dependency:
npm install -D babel-plugin-react-compiler@latestBecause the compiler changes how your code runs, treat upgrades like any other build-tool upgrade. Commit your lockfile and read the release notes before you bump the version.
Configure plain Babel
If your project builds with Babel directly (through babel-loader, a CLI script, or a custom pipeline), add the plugin to babel.config.js:
// babel.config.js
module.exports = {
plugins: [
// The compiler plugin must run first in the plugin list.
['babel-plugin-react-compiler', {}],
],
};The empty object is the options slot. An empty object means every default applies.
Why the plugin must run first
The compiler analyzes your original source. It needs to see the JSX, the hook calls, and the control flow as you wrote them. If another plugin rewrites the code before the compiler sees it, the compiler may fail to recognize components, or it may skip them silently.
Babel runs plugins before presets, and it runs plugins in the order listed. So the rules are:
- Put
babel-plugin-react-compilerat the top ofplugins. - Do not rely on a preset to transform JSX before the compiler. Presets run after plugins, so the compiler still sees JSX first.
- If you use a plugin that rewrites function bodies before this one (some decorator and instrumentation plugins do), move the compiler ahead of it or test the output carefully.
Configure Vite
Vite does not use babel.config.js for React files by default. The @vitejs/plugin-react plugin builds its own Babel call, and it ignores your Babel config files. You pass Babel plugins through its babel option instead:
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
react({
babel: {
plugins: ['babel-plugin-react-compiler'],
},
}),
],
});A common mistake is to create a babel.config.js, see no change in the build output, and conclude that the compiler does nothing. In a Vite project the Babel config file is never read, so the plugin never runs.
Using Babel in the Vite pipeline makes dev server transforms and production builds somewhat slower than a build without Babel. The cost scales with the number of files, so measure it on your project.
Configure Next.js
Next.js builds with SWC and has its own switch for the compiler. You still install babel-plugin-react-compiler, but you enable it in next.config.js. In recent versions the option sits at the top level:
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactCompiler: true,
};
module.exports = nextConfig;In Next.js 15 the same flag lives under experimental:
// next.config.js (Next.js 15)
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
reactCompiler: true,
},
};
module.exports = nextConfig;Next.js decides which files need the compiler and applies it only to those, so you do not edit a Babel file. Check the Next.js documentation for your version, because this option has moved between releases.
Options you will use
The second element of the plugin entry is an options object. Two options matter for most setups.
| Option | Values | What it does |
|---|---|---|
| `compilationMode` | `'infer'` (default), `'annotation'`, `'all'` | Chooses which functions the compiler tries to compile. |
| `target` | `'17'`, `'18'`, `'19'` | Chooses the React version the output must run on. |
With the default infer mode, the compiler picks functions that look like components (capitalized names that return JSX) and hooks (names starting with use). With annotation, it compiles only functions that contain a 'use memo' directive. That mode is the usual first step when you adopt the compiler in an existing codebase.
// babel.config.js
module.exports = {
plugins: [
[
'babel-plugin-react-compiler',
{
// Compile only functions that opt in with a "use memo" directive.
compilationMode: 'annotation',
// Needed for React 17 or 18; omit for React 19.
target: '18',
},
],
],
};Setting target is required for React 17 and 18 because the compiled output imports a helper that only React 19 ships. For those versions you also install the runtime package:
npm install react-compiler-runtime@latestThe compiler targets React 19 by default, so a React 19 project needs neither target nor the runtime package.
Check that it ran
A build that finishes without errors does not prove the compiler touched your code. Two quick checks work without opening the browser.
Transform one file by hand
This script runs Babel on a small component and looks for the compiler's runtime import in the output. Install its dependencies first:
npm install -D @babel/core @babel/preset-react babel-plugin-react-compiler// check-compile.js
const babel = require('@babel/core');
const source = `
export function Hello({ name }) {
return <p>Hello, {name}</p>;
}
`;
const result = babel.transformSync(source, {
filename: 'Hello.jsx',
babelrc: false,
configFile: false,
presets: ['@babel/preset-react'],
plugins: ['babel-plugin-react-compiler'],
});
if (result.code.includes('react/compiler-runtime')) {
console.log('Compiled: the output uses the compiler runtime.');
} else {
console.log('Not compiled.');
}
console.log(result.code);Run it with node check-compile.js. In the output you should see an import from react/compiler-runtime and a call like _c(2). That call creates the cache array the compiler uses in place of useMemo and useCallback. If you see neither, the plugin did not process the file.
Inspect the built bundle
Search your production output for compiler-runtime. In a minified bundle the import name may be shortened, but the cache pattern, a useMemoCache-style call at the top of component functions, is still visible. You can also build once with the plugin and once without and compare the bundle sizes. The compiled version is usually a little larger.
The later chapter on DevTools shows the more direct check, where compiled components carry a badge in the component tree.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No change in output on Vite | Config put in `babel.config.js`, which Vite's React plugin ignores | Move the plugin into `react({ babel: { plugins: [...] } })` |
| Runtime error about `react/compiler-runtime` | React 17 or 18 without `target` set | Set `target` and install `react-compiler-runtime` |
| Some components compiled, others not | Compiler skipped code that breaks the Rules of React | Check the lint rules and the bailouts chapter |
| Nothing compiled in `annotation` mode | No functions carry `'use memo'` | Add the directive to a function you want compiled |
| Plugin throws during parse | Another plugin ran first and changed the syntax | Move the compiler to the top of `plugins` |
Where setup ends and adoption begins
At this point the plugin is installed and wired into your build. Whether it compiles your whole app, a few files, or a few annotated functions depends on compilationMode. The next chapters cover React version support and gradual adoption in detail.
Exercise
Your team uses Vite with React 18. You added babel.config.js with ['babel-plugin-react-compiler', {}], and the build output has no compiler code. List the two changes needed to get a working setup.
Show answer
First, move the plugin into vite.config.js under react({ babel: { plugins: ['babel-plugin-react-compiler'] } }), because @vitejs/plugin-react ignores babel.config.js. Second, because the app uses React 18, set the plugin option target: '18' and install react-compiler-runtime, so the compiled output has a runtime helper to import. The option goes in the plugin entry as ['babel-plugin-react-compiler', { target: '18' }].
Exercise
Why must the compiler plugin appear first in the Babel plugins list, and how can you check that a single file was compiled?
Show answer
The compiler analyzes your original source, so it has to see JSX, hooks, and control flow before other plugins rewrite them. To check one file, run it through babel.transformSync with the plugin and look for an import from react/compiler-runtime and a cache call such as _c(2) in the output.