logo by @sawaratsuki1004
React
v19.2
Learn
Reference
Community
Blog

Is this page useful?

في هذه الصفحة

  • Overview
  • Why Ship Compiled Code?
  • Setting Up Compilation
  • Backwards Compatibility
  • 1. Install the runtime package
  • 2. Configure the target version
  • Testing Strategy
  • Troubleshooting
  • Library doesn’t work with older React versions
  • Compilation conflicts with other Babel plugins
  • Runtime module not found
  • Next Steps

    react@19.2

  • نظرة عامة
  • Hooks
    • useActionState
    • useCallback
    • useContext
    • useDebugValue
    • useDeferredValue
    • useEffect
    • useEffectEvent
    • useId
    • useImperativeHandle
    • useInsertionEffect
    • useLayoutEffect
    • useMemo
    • useOptimistic
    • useReducer
    • useRef
    • useState
    • useSyncExternalStore
    • useTransition
  • المكونات
    • <Fragment> (<>)
    • <Profiler>
    • <StrictMode>
    • <Suspense>
    • <Activity>
    • <ViewTransition> - This feature is available in the latest Canary version of React
  • APIs
    • act
    • addTransitionType - This feature is available in the latest Canary version of React
    • cache
    • cacheSignal
    • captureOwnerStack
    • createContext
    • lazy
    • memo
    • startTransition
    • use
    • experimental_taintObjectReference - This feature is available in the latest Experimental version of React
    • experimental_taintUniqueValue - This feature is available in the latest Experimental version of React
  • react-dom@19.2

  • Hooks
    • useFormStatus
  • المكونات (Components)
    • Common (e.g. <div>)
    • <form>
    • <input>
    • <option>
    • <progress>
    • <select>
    • <textarea>
    • <link>
    • <meta>
    • <script>
    • <style>
    • <title>
  • APIs
    • createPortal
    • flushSync
    • preconnect
    • prefetchDNS
    • preinit
    • preinitModule
    • preload
    • preloadModule
  • Client APIs
    • createRoot
    • hydrateRoot
  • Server APIs
    • renderToPipeableStream
    • renderToReadableStream
    • renderToStaticMarkup
    • renderToString
    • resume
    • resumeToPipeableStream
  • Static APIs
    • prerender
    • prerenderToNodeStream
    • resumeAndPrerender
    • resumeAndPrerenderToNodeStream
  • React Compiler

  • الإعدادات (Configuration)
    • compilationMode
    • gating
    • logger
    • panicThreshold
    • target
  • Directives
    • "use memo"
    • "use no memo"
  • تصريف المكتبات (Compiling Libraries)
  • React DevTools

  • React Performance tracks
  • eslint-plugin-react-hooks

  • Lints
    • exhaustive-deps
    • rules-of-hooks
    • component-hook-factories
    • config
    • error-boundaries
    • gating
    • globals
    • immutability
    • incompatible-library
    • preserve-manual-memoization
    • purity
    • refs
    • set-state-in-effect
    • set-state-in-render
    • static-components
    • unsupported-syntax
    • use-memo
  • قواعد React (Rules of React)

  • نظرة عامة (Overview)
    • Components و Hooks يجب أن تكون Pure
    • React تستدعي Components و Hooks
    • قواعد Hooks
  • React Server Components

  • Server Components
  • Server Functions
  • Directives
    • 'use client'
    • 'use server'
  • Legacy APIs

  • Legacy React APIs
    • Children
    • cloneElement
    • Component
    • createElement
    • createRef
    • forwardRef
    • isValidElement
    • PureComponent
مرجع API

Compiling Libraries

This guide helps library authors understand how to use React Compiler to ship optimized library code to their users.

  • Why Ship Compiled Code?
  • Setting Up Compilation
  • Backwards Compatibility
    • 1. Install the runtime package
    • 2. Configure the target version
  • Testing Strategy
  • Troubleshooting
    • Library doesn’t work with older React versions
    • Compilation conflicts with other Babel plugins
    • Runtime module not found
  • Next Steps

Why Ship Compiled Code?

As a library author, you can compile your library code before publishing to npm. This provides several benefits:

  • Performance improvements for all users - Your library users get optimized code even if they aren’t using React Compiler yet
  • No configuration required by users - The optimizations work out of the box
  • Consistent behavior - All users get the same optimized version regardless of their build setup

Setting Up Compilation

Add React Compiler to your library’s build process:

Terminal
npm install -D babel-plugin-react-compiler@latest

Configure your build tool to compile your library. For example, with Babel:

// babel.config.js module.exports = { plugins: [ 'babel-plugin-react-compiler', ], // ... other config };

Backwards Compatibility

If your library supports React versions below 19, you’ll need additional configuration:

1. Install the runtime package

We recommend installing react-compiler-runtime as a direct dependency:

Terminal
npm install react-compiler-runtime@latest

{ "dependencies": { "react-compiler-runtime": "^1.0.0" }, "peerDependencies": { "react": "^17.0.0 || ^18.0.0 || ^19.0.0" } }

2. Configure the target version

Set the minimum React version your library supports:

{ target: '17', // Minimum supported React version }

Testing Strategy

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.

Troubleshooting

Library doesn’t work with older React versions

If your compiled library throws errors in React 17 or 18:

  1. Verify you’ve installed react-compiler-runtime as a dependency
  2. Check that your target configuration matches your minimum supported React version
  3. Ensure the runtime package is included in your published bundle

Compilation conflicts with other Babel plugins

Some Babel plugins may conflict with React Compiler:

  1. Place babel-plugin-react-compiler early in your plugin list
  2. Disable conflicting optimizations in other plugins
  3. Test your build output thoroughly

Runtime module not found

If users see “Cannot find module ‘react-compiler-runtime’“:

  1. Ensure the runtime is listed in dependencies, not devDependencies
  2. Check that your bundler includes the runtime in the output
  3. Verify the package is published to npm with your library

Next Steps

  • Learn about debugging techniques for compiled code
  • Check the configuration options for all compiler options
  • Explore compilation modes for selective optimization
السابق"use no memo"

Copyright © Meta Platforms, Inc
no uwu plz
uwu?
Logo by@sawaratsuki1004
تعلم React
بداية سريعة
التثبيت
وصف واجهة المستخدم (UI)
إضافة التفاعلية
إدارة State
مخارج الطوارئ
مرجع API
React APIs
React DOM APIs
المجتمع
ميثاق السلوك
تعرف على الفريق
المساهمون في التوثيق
شكر وتقدير
المزيد
المدونة
React Native
الخصوصية
الشروط
// babel.config.js
module.exports = {
plugins: [
'babel-plugin-react-compiler',
],
// ... other config
};
{
"dependencies": {
"react-compiler-runtime": "^1.0.0"
},
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
}
}
{
target: '17', // Minimum supported React version
}