Files
Alex Hunt 9c3fddf9cd Move core-cli-utils to private/ as a reference implementation (#56922)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/56922

Move `react-native/core-cli-utils` from `packages/` to `private/`, stop publishing it to npm, and reframe it as a reference implementation of React Native CLI tooling.

The package has no known external consumers and is only used internally by `private/helloworld/` and `packages/rn-tester/`. Publishing it to npm creates a maintenance surface for a package that serves no external users.

See https://github.com/react-native-community/discussions-and-proposals/pull/1002.

#### Changes

- Move `packages/core-cli-utils/` to `private/core-cli-utils/`.
- Convert all source files to CJS (require/module.exports) with Flow comment syntax (/*:: */), eliminating the runtime Babel dependency.
 - Remove from the JS build pipeline (`scripts/build/config.js`).
 - Remove the defunct `patchCoreCLIUtilsPackageJSON()` runtime patching from both `helloworld/cli.js` and `rn-tester/cli.js`, and delete both `monorepo.js` files that contained it.
  - Add as `devDependency` to `rn-tester` and `helloworld`.
  - Skip `"*"` version deps in `_prepareHelloWorld()` so they aren't rewritten to the Verdaccio-published version.
  - Remove redundant desktop import ignore entry (`private/**` already covers it).
  - Rewrite README as reference implementation documentation.

Changelog:
[General][Breaking] - The `react-native/core-cli-utils` package is no longer published. It remains available in the React Native repo as a reference implementation.

Reviewed By: cortinico

Differential Revision: D105959855

fbshipit-source-id: 42e439a45273bdeca76029eff306cdf2451308e2
2026-05-22 12:36:57 -07:00
..

scripts/build

Shared build setup for the React Native monorepo.

Overview

These scripts form the modern build setup for JavaScript (Flow) packages in react-native, exposed as yarn build.

Tip

Generally, React Native maintainers do not need to run yarn build, as all packages will run from source during development. Please continue reading if you are adding/removing a package or modifying its build configuration.

Key info

  • Which packages are included?
    • Currently, only Node.js-targeting packages are included, configured in config.js.
    • We don't yet include runtime packages (targeting Metro). These are instead transformed in user space via @react-native/babel-preset.
  • When does the build run?
    • Packages are built in CI workflows — both for integration/E2E tests, and before publishing to npm.

Usage

💡 Reminder: 99% of the time, there is no need to use yarn build, as all packages will run from source during development.

Build commands are exposed as npm scripts at the repo root.

# Build all packages
yarn build

# Build a specific package
yarn build dev-middleware

# Clean build directories
yarn clean

Once built, developing in the monorepo should continue to work — now using the compiled version of each package.

Configuration

Monorepo packages must be opted in for build, configured in config.js (where build options are also documented).

const buildConfig /*: BuildConfig */ = {
  'packages': {
    'dev-middleware': {
      emitTypeScriptDefs: true,
      target: 'node',
    },
    ...

Required package structure

Opting a package into the yarn build setup requires a strict file layout. This is done to simplify config and to force consistency across the monorepo.

packages/
  example-pkg/
    src/             # All source files
      index.js       # Entry point wrapper file (calls babel-register.js) (compiled away)
      index.flow.js  # Entry point implementation in Flow
      [other files]
    package.json     # Includes "exports" field, ideally only src/index.js

Notes:

  • We make use of "wrapper files" (.js → .js.flow) for each package entry point, to enable running from source with zero config. To validate these, package entry points must be explicitly defined via "exports".
  • To minimize complexity, prefer only a single entry of {".": "src/index.js"} in "exports" for new packages.

Build behavior

Running yarn build will compile each package following the below steps, depending on the configured target and other build options.

  • Create a dist/ directory, replicating each source file under src/:
    • For every @flow file, strip Flow annotations using flow-api-extractor.
    • For each entry point in "exports", remove the .js wrapper file and compile from the .flow.js source.
  • If configured, emit a Flow (.js.flow) or TypeScript (.d.ts) type definition file per source file, using flow-api-extractor.

Together, this might look like the following:

packages/
  example-pkg/
    dist/
      index.js       # Compiled source file (from index.flow.js)
      index.js.flow  # Flow definition file
      index.d.ts     # TypeScript definition file
      [other transformed files]
    package.json     # "publishConfig" will override exports to "dist/" on publish

Link: Example dist/ output on npm.