This guide helps library authors understand how to use React Compiler to ship optimized library code to their users.
As a library author, you can compile your library code before publishing to npm. This provides several benefits:
Add React Compiler to your library’s build process:
Configure your build tool to compile your library. For example, with Babel:
// babel.config.js
module.exports = {
plugins: [
'babel-plugin-react-compiler',
],
// ... other config
}; If your library supports React versions below 19, you’ll need additional configuration:
We recommend installing react-compiler-runtime as a direct dependency:
{
"dependencies": {
"react-compiler-runtime": "^1.0.0"
},
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
}
} Set the minimum React version your library supports:
{
target: '17', // Minimum supported React version
} Test your library both with and without compilation to ensure compatibility. Run your existing test suite against the compiled code, and also create a separate test configuration that bypasses the compiler. This helps catch any issues that might arise from the compilation process and ensures your library works correctly in all scenarios.
If your compiled library throws errors in React 17 or 18:
react-compiler-runtime as a dependencytarget configuration matches your minimum supported React versionSome Babel plugins may conflict with React Compiler:
babel-plugin-react-compiler early in your plugin listIf users see “Cannot find module ‘react-compiler-runtime’“:
dependencies, not devDependencies
© 2013–present Facebook Inc.
Licensed under the Creative Commons Attribution 4.0 International Public License.
https://react.dev/reference/react-compiler/compiling-libraries