forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shared monorepo build setup (facebook#38718)
Summary: Pull Request resolved: facebook#38718 > NOTE: Replaces facebook#38240 ## Context RFC: Decoupling Flipper from React Native core: react-native-community/discussions-and-proposals#641 ## Changes To support incoming new React Native packages around debugging (including migrating over [`react-native-community/cli-plugin-metro`](https://github.com/react-native-community/cli/tree/main/packages/cli-plugin-metro)) — which target Node.js and require a build step, this PR adds a minimal shared build setup across the `react-native` monorepo. The setup is closely inspired/based on the build scripts in Jest, Metro, and React Native CLI — and is a simple set of script wrappers around Babel. These are available as build commands at the root of the repo: - `yarn build` — Builds all configured packages. Functionally, this: - Outputs a `dist/` directory with built files. - Rewrites package.json `"exports"` to update every `./src/*` reference to `./dist/*` (source of truth). - `scripts/build/babel-register.js` — Allows running all Node.js entry points from source, similar to the current setup in [facebook/metro](https://github.com/facebook/metro). (Example entry point file in this PR: `packages/dev-middleware/src/index.js`) Build configuration (i.e. Babel config) is shared as a set standard across the monorepo, and **packages are opted-in to requiring a build**, configured in `scripts/build.config.js`. ``` const buildConfig /*: BuildConfig */ = { // The packages to include for build and their build options packages: { 'dev-middleware': {target: 'node'}, }, }; ``` For now, there is a single `target: 'node'` option — this is necessary as `react-native`, unlike the above other projects, is a repository with packages targeting several runtimes. We may, in future, introduce a build step for other, non-Node, packages — which may be useful for things such as auto-generated TypeScript definitions. {F1043312771} **Differences from the Metro setup** - References (and compiles out) repo-local `scripts/build/babel-register.js` — removing need for an npm-published dependency. ## Current integration points - **CircleCI** — `yarn build` is added to the `build_npm_package` and `find_and_publish_bumped_packages` jobs. **New Node.js package(s) are not load bearing quite yet**: There are not yet any built packages added to the dependencies of `packages/react-native/`, so this will be further tested in a later PR (and is actively being done in an internal commit stack). ### Alternative designs **Per-package config file** Replace `scripts/build/config.js` with a package-defined key in in `package.json`, similar to Jest's [`publishConfig`](https://github.com/jestjs/jest/blob/1f019afdcdfc54a6664908bb45f343db4e3d0848/packages/jest-cli/package.json#L87C3-L89C4). ``` "buildConfig": { "type": "node" }, ``` This would be the only customisation required, with a single Babel config still standardised. Another option this might receive in future is `enableTypeScriptCodgeen`. **Rollup** More sophisticated build tool for Node.js, used by the React codebase (albeit within a custom script setup as well). **Lerna and Nx** - Most sophisticated setup enabling caching and optimised cloud runs. - Probably the most likely thing we'll move towards at a later stage. Changelog: [Internal] Differential Revision: D47760330 fbshipit-source-id: 2c0128f7a3c54e222d3f640f8d2bf48264f23813
- Loading branch information
1 parent
1f24750
commit e4c22b6
Showing
13 changed files
with
503 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
declare module '@pkgjs/parseargs' { | ||
declare type ParseArgsOptionConfig = { | ||
type: 'string' | 'boolean', | ||
short?: string, | ||
multiple?: boolean, | ||
}; | ||
|
||
declare type ParseArgsOptionsConfig = { | ||
[longOption: string]: ParseArgsOptionConfig, | ||
}; | ||
|
||
declare export type ParseArgsConfig = { | ||
strict?: boolean, | ||
allowPositionals?: boolean, | ||
tokens?: boolean, | ||
options?: ParseArgsOptionsConfig, | ||
args?: string[], | ||
}; | ||
|
||
declare type ParsedResults = { | ||
values: { | ||
[longOption: string]: void | string | boolean | Array<string | boolean>, | ||
}, | ||
positionals: string[], | ||
... | ||
}; | ||
|
||
declare export function parseArgs(config: ParseArgsConfig): ParsedResults; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
export {default as createDevMiddleware} from './createDevMiddleware'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
const path = require('path'); | ||
const {PACKAGES_DIR} = require('./build'); | ||
const {buildConfig, getBabelConfig} = require('./config'); | ||
|
||
let isRegisteredForMonorepo = false; | ||
|
||
/** | ||
* Calling this function enables all Node.js packages to run from source when | ||
* developing in the React Native repo. | ||
* | ||
* A call should located in each entry point file in a package (i.e. for all | ||
* paths in "exports"), inside a special `if` condition that will be compiled | ||
* away on build. | ||
* | ||
* if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { | ||
* require('../../../scripts/build/babel-register').registerForMonorepo(); | ||
* } | ||
*/ | ||
function registerForMonorepo() { | ||
if (isRegisteredForMonorepo) { | ||
return; | ||
} | ||
|
||
for (const [packageName, {target}] of Object.entries(buildConfig.packages)) { | ||
if (target === 'node') { | ||
registerPackage(packageName); | ||
} | ||
} | ||
|
||
isRegisteredForMonorepo = true; | ||
} | ||
|
||
function registerPackage(packageName /*: string */) { | ||
const packageDir = path.join(PACKAGES_DIR, packageName); | ||
|
||
require('@babel/register')({ | ||
...getBabelConfig(packageName), | ||
root: packageDir, | ||
ignore: [/\/node_modules\//], | ||
}); | ||
} | ||
|
||
module.exports = {registerForMonorepo}; |
Oops, something went wrong.