The Rollup Build System
Relevant source files
The following files were used as context for generating this wiki page:The React repository utilizes a sophisticated, in-house build system powered by Rollup.js. Its primary purpose is to compile the React source code into a multitude of package formats and bundles tailored for various consumption environments, such as Node.js (CommonJS), modern browsers (ESM), and legacy environments (UMD/globals). The system is highly configurable, handling different build types like development, production, and profiling, and manages environment-specific features through a "forking" mechanism.
The main orchestration script is scripts/rollup/build.js, which reads bundle configurations from scripts/rollup/bundles.js and processes them through a pipeline of standard and custom Rollup plugins. This system is integral to the React release process, generating the artifacts published to NPM and used by downstream tooling.
Build Orchestration
The build process is initiated and controlled by scripts/rollup/build.js. This script is responsible for parsing command-line arguments, iterating through bundle definitions, and invoking Rollup for each target.
Sources: scripts/rollup/build.js
Invocation
The build can be triggered via npm scripts defined in package.json.
| Script | Description |
|---|---|
build | Executes scripts/rollup/build-all-release-channels.js, which is the standard command for building all release artifacts. |
build-for-devtools | A specialized script that builds a specific set of packages required for React DevTools development. |
build-for-flight-dev | A specialized script that builds a specific set of packages required for React Flight (Server Components) development. |
The core build script accepts command-line arguments to customize its behavior:
--watch: Runs Rollup in watch mode for continuous compilation.--type <type>: Filters bundles by their type (e.g.,NODE_DEV,ESM_PROD).--pretty: Forces human-readable output for production builds that would otherwise be minified.bundleName: Positional arguments specify which bundles to build by name (e.g.,react-dom,scheduler).
Sources: package.json:200-204, scripts/rollup/build.js:110-118
Build Flow
The main buildEverything function in build.js orchestrates the entire process. The following diagram illustrates its high-level workflow.
This diagram shows the sequence of operations from parsing arguments to generating final packages and reporting statistics.
Sources: scripts/rollup/build.js:827-876
Bundle Configuration
The heart of the build system's configuration lies in scripts/rollup/bundles.js, which defines every possible bundle that can be generated.
Bundle Definition
Each bundle is an object with properties that define its entry point, output format, dependencies, and build-time behavior.
| Property | Type | Description |
|---|---|---|
entry | string | The source entry point for the bundle (e.g., react-dom). |
bundleTypes | Array<string> | An array of build types this bundle supports (e.g., NODE_PROD, ESM_DEV). |
global | string | The global variable name for UMD/IIFE builds (e.g., React). |
moduleType | string | The module system type (NODE, FB, RN_OSS, etc.). |
externals | Array<string> | A list of external dependencies that should not be included in the bundle. |
babel | function | An optional function to customize the Babel configuration for this specific bundle. |
wrapWithModuleBoundaries | boolean | Determines if the bundle output should be wrapped in a module boundary. |
Sources: scripts/rollup/bundles.js
Bundle Types
The system defines a set of constants for different bundle targets. These types determine the output format, minification level, and feature flags.
| Bundle Type | Format | Environment | Description |
|---|---|---|---|
NODE_ES2015 | cjs | Production | Node.js build with ES2015+ syntax. |
ESM_DEV | es | Development | ES Module build for development. |
ESM_PROD | es | Production | ES Module build for production. |
NODE_DEV | cjs | Development | CommonJS build for Node.js development. |
NODE_PROD | cjs | Production | CommonJS build for Node.js production. |
NODE_PROFILING | cjs | Profiling | CommonJS build with profiling enabled. |
BROWSER_SCRIPT | iife | Production | A <script> tag-compatible build for browsers. |
FB_WWW_DEV | cjs | Development | Meta-internal WWW build for development. |
RN_OSS_DEV | cjs | Development | React Native open-source build for development. |
CJS_DTS | cjs | - | Generates a .d.ts file for CommonJS modules. |
ESM_DTS | es | - | Generates a .d.ts file for ES modules. |
Sources: scripts/rollup/bundles.js:28-48, scripts/rollup/build.js:264-292
The Rollup Plugin Pipeline
For each bundle, build.js assembles a specific sequence of Rollup plugins to transform the source code. The getPlugins function is central to this process.
This diagram shows the typical order of plugins applied during a build.
Sources: scripts/rollup/build.js:419-612
Key Plugins
useForks: A custom plugin that implements the module forking mechanism. It shims modules at build time based on the target environment.
Sources: scripts/rollup/plugins/use-forks-plugin.js@rollup/plugin-replace: Injects global constants like__DEV__,__PROFILE__, andprocess.env.NODE_ENVto enable dead-code elimination for environment-specific code.
Sources: scripts/rollup/build.js:509-521@rollup/plugin-babel: Transpiles modern JavaScript and JSX into code compatible with target environments. It uses a base configuration that can be customized per-bundle.
Sources: scripts/rollup/build.js:160-201closure-plugin: A custom wrapper around Google Closure Compiler, used for advanced optimization and minification in most production builds.
Sources: scripts/rollup/build.js:533-565sizes: A custom plugin that records the final size and gzipped size of each bundle for statistics tracking.
Sources: scripts/rollup/build.js:598-612
Module Forking
A key feature of the build system is "forking," which allows different implementations of a module to be used for different build targets. This is used extensively to separate stable features from experimental ones, and to provide environment-specific logic (e.g., for React Native vs. WWW).
Entry Point Resolution
The forking starts at the entry point. The resolveEntryFork function in build.js determines which source file to use by checking for the existence of files with specific suffixes in a particular order.
This logic allows a single bundle entry like react-dom to resolve to react-dom.experimental.js or react-dom.stable.js depending on the __EXPERIMENTAL__ flag.
Sources: scripts/rollup/build.js:749-800
Module-Level Forking
In addition to entry points, individual modules can be forked. The scripts/rollup/forks.js file defines mappings from a base module to its forked implementation for different bundle types and module types.
For example, the scheduler module might be forked to use a different implementation for React Native (RN_OSS) versus other environments. The use-forks-plugin reads this configuration and tells Rollup to substitute the module import accordingly.
Sources: scripts/rollup/forks.js, scripts/rollup/plugins/use-forks-plugin.js
Packaging and Release
After all bundles are created, the packaging.js script handles the final steps of preparing the build directory for publishing.
copyAllShims(): Copies predefined shim modules into the build output.prepareNpmPackages(): Creates apackage.jsonfor each package in thebuild/packagesdirectory, populating it with the correct version, dependencies, and entry points for the generated bundles. It also copies static files likeLICENSEandREADME.md.
This process ensures that the directory structure and metadata are correct for publishing to npm. The release scripts documented in scripts/release/README.md then consume these artifacts to publish next, experimental, or stable releases.
Sources: scripts/rollup/packaging.js, scripts/release/README.md:20-30