Commit Graph

25 Commits

Author SHA1 Message Date
Alex Hunt
01b40789ee Update build script to minimally support subpath patterns (#47673)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47673

Update shared monorepo build script:
- Refactor entry point discovery and file iteration
- Add support for "exports" subpath patterns (skip considering as entry point with warning)

This is required for the incoming migration of `react-native-codegen` to this build setup (D51465053).

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D66121262

fbshipit-source-id: 0deb2c2b26de442ee348ef9de54a1fa144368b6f
2024-11-20 09:21:13 -08:00
Alex Hunt
35ab62cca3 Substitute @pkgjs/parseargs for Node builtin (#47704)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47704

Now that we have a min requirement of Node 18.18 on `main`, drop dependency on this polyfill.

Changelog: [Internal]

Reviewed By: vzaidman

Differential Revision: D66162328

fbshipit-source-id: e8ab6669fe14ed177eccf4b861c01df4fb0d405a
2024-11-19 09:47:40 -08:00
Rubén Norte
07b34f4ca3 Add Flow typing for test runner for RN integration tests (#47612)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47612

Changelog: [internal]

Just using babel-register so we can use Flow types in the code for the RN integration tests runner.

Reviewed By: sammy-SC

Differential Revision: D65948801

fbshipit-source-id: 6c54b0463fe4b71c818ff1c4faa973cc8b13b5d2
2024-11-15 03:00:17 -08:00
Vitali Zaidman
e08c7eb22a fix how @babel/register is used and update it (#46987)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46987

X-link: https://github.com/facebook/metro/pull/1371

Update `babel/register` to latest version, fixing the bug that were preventing us from updating it previously.

Changelog: [Internal]

Reviewed By: huntie

Differential Revision: D64245277

fbshipit-source-id: f3d07b06a11fbe3a0ed28e22f5b687541782dda9
2024-10-14 06:30:59 -07:00
Rob Hogan
06fc6f2beb Use metro-babel-register for additive Babel registration (#45295)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45295

When registering Babel to run RN from source, we currently call `babel/register` directly from `scripts/babel-register.js`.

This has the effect of overwriting any previous registration, which causes problems in the FB monorepo because RN also loads other projects that lie outside this registration (like Metro) from source - possibly requiring different configurations.

Moreover, if Metro is subsequently loaded from source, its own registration clobbers RN's.

Instead, this diff runs the registration through `metro-babel-register`, which maintains a cumulative list of registration directories and applies a uniform transform.

Note that this means we're not using exactly the same transform at build/publish time as for running from source - to fix that, we ought to move everything to a central `babel.config.js`, but that's a much bigger change, and this gets us close enough to unblock.

Changelog: [Internal]

Reviewed By: hoxyq

Differential Revision: D59376984

fbshipit-source-id: 0dbb00970ac87dbe40ec8904bf51ef4b1fee5e0f
2024-07-05 04:48:30 -07:00
Moti Zilberman
e0fcf2983a Use hermes-parser when building Node packages (#45041)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45041

Changelog: [Internal]

TSIA

Reviewed By: huntie

Differential Revision: D58734657

fbshipit-source-id: d5b17bc0ee8f36bae7c594d99ce70ecea9bf4a40
2024-06-18 09:43:20 -07:00
Blake Friedman
b38f80aeb6 update shared build to strip Babel wrapper (#44339)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44339

We require the wrapper code for in repository calls to these packages directly from node (i.e. using CommonJS).  This wrapper code typically sits at the entrypoint of the build packages (i.e. `index.js`).

NOTE: This unblocks an issue preventing me from landing further work on the `helloworld` cli replacing the community template.

## Problem:
The [flow-api-translator](https://www.npmjs.com/package/flow-api-translator) library doesn't allow CommonJS `module.exports` when generating TypeScript Type Defintions.

## Change
1. At the built time, this strips out our wrapper code and sets up the dist/ folder appropriately for npm distribution.
2. Updated the `package.json` files to consistently share Flow types

Changelog: [Internal] refactor build packages output to remove wrapper.

NOTE: Added better error messages when users deviated from the current pattern:
{F1501571608}

Reviewed By: huntie

Differential Revision: D56762162

fbshipit-source-id: f110b31e4ad780998dbc81a2482891ac8d8c6458
2024-05-02 04:00:21 -07:00
Blake Friedman
9d1f951ab0 remove from build script (#44233)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44233

The package was added to our build scripts, but shouldn't have been.  We're not exporting this package or making it public.

Changelog: [Internal]

This should unblock our OSS CI.

Reviewed By: cipolleschi

Differential Revision: D56513694

fbshipit-source-id: f37c75871253b2570fb933175165d8f0a9593a16
2024-04-24 03:53:34 -07:00
Blake Friedman
995d5b832d Add an internal HelloWorld template
Summary:
This is a copy of the current packages/react-native/template that we exclusively use internally for testing.

Changelog: [Internal]

NOTE: Best contribution would be to scan the file list and ensure there isn't anything that shouldn't be in there.

bypass-github-export-checks

Reviewed By: cortinico, cipolleschi

Differential Revision: D56242484

fbshipit-source-id: 0913ff7acff9b0314b49f48e986674b77dbb908e
2024-04-23 15:07:59 -07:00
Blake Friedman
f855de7fed Add @react-native/core-cli-utils to build script
Summary:
Changelog: [Internal]

Use our build script for packages and to generate the TypeScript types.

bypass-github-export-checks

Reviewed By: huntie

Differential Revision: D54428870

fbshipit-source-id: 2a1666d30ac472300979b2be078a906d390e919a
2024-03-13 11:07:12 -07:00
Alex Hunt
30b088d245 Adjust output package.json formatting in scripts (#43133)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43133

I noticed inconsistent handling of terminating newlines in D54006327@V1, and had also been noticing `yarn build` reformatting unrelated sections of `package.json` files.

For now, this logic isn't moved to a shared util, since there will likely be a higher level abstraction for the release scripts in the next batch of improvements.

Changelog: [Internal]

Reviewed By: lunaleaps, cipolleschi

Differential Revision: D54007565

fbshipit-source-id: 74d58362a85be4fae2f9e058b6c6622a026ff0a0
2024-02-23 02:44:10 -08:00
Alex Hunt
80ec096e7c Create shared consts module for scripts
Summary:
Tidy up, removes risk of breakage when relocating inner modules.

Changelog: [Internal]

Reviewed By: lunaleaps

Differential Revision: D53813025

fbshipit-source-id: 43dc4a57191363546d6858cff311e260d416192f
2024-02-16 03:21:25 -08:00
Alex Hunt
b41a33ede9 Move metro-config package into monorepo build, enable TS generation (#41836)
Summary:
This adds `react-native/metro-config` to the monorepo build tool and emits the missing typescript declarations.

Right now, we do have typescript declarations on `metro-config`, but not `react-native/metro-config`. Which makes everything a bit harder extend from "[the default React Native metro config](https://github.com/facebook/react-native/pull/36502)" in Expo.

> Note, I also added the same `exports` block from `react-native/dev-middleware` for conformity.

One open question here is, why aren't we exporting _all_ helper functions from `metro-config`? To me, its a bit weird that we need both `metro-config` _and_ `react-native/metro-config` as `loadConfig` isn't exported.

## Changelog:

[INTERNAL] [FIXED] - Emit typescript declaration files for `react-native/metro-config`

Pull Request resolved: https://github.com/facebook/react-native/pull/41836

Test Plan:
Run the build tool, and check if the typescript declarations are emitted for `react-native/metro-config`.

```
yarn build metro-config
```

Reviewed By: hoxyq

Differential Revision: D51943453

Pulled By: huntie

fbshipit-source-id: cfaffe5660053fc9a9fcbe3dacf7f6ccc2bde01b
2024-02-12 05:20:38 -08:00
Rob Hogan
41b256a776 Babel-ignore node_modules on Windows consistently with others (#42681)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42681

CI failures in Windows JS tests recently (https://github.com/facebook/react-native/pull/41463) were caused by the triggering of Babel registration during tests, due to an import of `packages/dev-middleware` (index), breaking subsequent transformation of other tests.

## Root cause
Example of a problematic import:
a5d8ea4579/packages/dev-middleware/src/__tests__/ServerUtils.js (L15)

..which triggers a Babel registration:
a5d8ea4579/packages/dev-middleware/src/index.js (L16-L18)

That registration behaves differently on Windows due to the `ignore: [/\/node_modules\/\]`, which doesn't match against Windows path separators - Babel matches against system separators.

In particular, this changed whether `node_modules/flow-parser` was transformed when loading the RN Babel transformer. Transforming this file causes a `console.warn` from Babel due to its size:
> [BABEL] Note: The code generator has deoptimised the styling of /Users/robhogan/workspace/react-native/node_modules/flow-parser/flow_parser.js as it exceeds the max of 500KB.

This throws due to our setup:
a5d8ea4579/packages/react-native/jest/local-setup.js (L27)

This all manifests as the first test following a Babel registration (within the same Jest worker) that requires the RN Babel transformer throwing during script transformation.

## This change
This is the minimally disruptive change that makes Babel registration behaviour consistent between Windows and other platforms. The more durable solution here would be *not* to rely on any Babel registration for Jest, which has its own `ScriptTransformer` mechanism for running code from source. Given the fragile way our internal+OSS Babel set up hangs together that's a higher-risk change, so I'll follow up separately.

Changelog: [Internal]

Reviewed By: huntie

Differential Revision: D53124578

fbshipit-source-id: 074a8e139e506a5dceec13f07d412599fb292d92
2024-01-27 06:49:15 -08:00
Gijs Weterings
73411f4e08 Back out "Restore InspectorProxy tests - fix Windows by Babel-ignoring node_modules consistently"
Summary: Changelog: Internal

Reviewed By: gsathya

Differential Revision: D51447333

fbshipit-source-id: 56eb839671ca62269d59d48fd3c83df4f42a5111
2023-11-18 02:04:44 -08:00
Rob Hogan
59a2282b29 Restore InspectorProxy tests - fix Windows by Babel-ignoring node_modules consistently (#41526)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41526

CI failures in Windows JS tests recently (https://github.com/facebook/react-native/pull/41463) were caused by the triggering of Babel registration during tests, due to an import of `packages/dev-middleware` (index), breaking subsequent transformation of other tests.

## Root cause
Example of a problematic import:
a5d8ea4579/packages/dev-middleware/src/__tests__/ServerUtils.js (L15)

..which triggers a Babel registration:
a5d8ea4579/packages/dev-middleware/src/index.js (L16-L18)

That registration behaves differently on Windows due to the `ignore: [/\/node_modules\/\]`, which doesn't match against Windows path separators - Babel matches against system separators.

In particular, this changed whether `node_modules/flow-parser` was transformed when loading the RN Babel transformer. Transforming this file causes a `console.warn` from Babel due to its size:
> [BABEL] Note: The code generator has deoptimised the styling of /Users/robhogan/workspace/react-native/node_modules/flow-parser/flow_parser.js as it exceeds the max of 500KB.

This throws due to our setup:
a5d8ea4579/packages/react-native/jest/local-setup.js (L27)

This all manifests as the first test following a Babel registration (within the same Jest worker) that requires the RN Babel transformer throwing during script transformation.

## This change
This is the minimally disruptive change that makes Babel registration behaviour consistent between Windows and other platforms. The more durable solution here would be *not* to rely on any Babel registration for Jest, which has its own `ScriptTransformer` mechanism for running code from source. Given the fragile way our internal+OSS Babel set up hangs together that's a higher-risk change, so I'll follow up separately.

Changelog: [Internal]

Reviewed By: huntie

Differential Revision: D51424802

fbshipit-source-id: 8b733c0c159ee84690aef04abced682d126c6d27
2023-11-17 08:53:14 -08:00
Moti Zilberman
d6e0bc714a Enable lint/sort-imports everywhere (#41334)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41334

TSIA.

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D51025812

fbshipit-source-id: e10d437be775a6b80946483aa96460f34927f870
2023-11-06 12:59:38 -08:00
Gijs Weterings
d4ad19c969 Revert D49370200: Migrate codegen to shared build setup, remove package build pre-step from RNTester
Differential Revision:
D49370200

Original commit changeset: 992913155169

Original Phabricator Diff: D49370200

fbshipit-source-id: e8232c97c22065fb54ac940ee2351b2155eb51e0
2023-10-23 12:00:54 -07:00
Alex Hunt
4db31a3110 Migrate codegen to shared build setup, remove package build pre-step from RNTester (#39540)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39540

This simplifies the use of Codegen when creating dev builds of `rn-tester` in the monorepo. It now runs from source for this internal scenario, and this package is now built using the shared monorepo build setup.

Changes:
- Migrate `packages/react-native-codegen` to the shared `yarn build` setup.
    - Update package to use `"exports"` field and wrap entry point modules with `babel-register` (NOTE: This is only required for each entry point internally used in the monorepo).
- Fixup small Flow syntax quirks that fail under `hermes-parser`.
- Remove `BuildCodegenCLITask` task from Android build.
- Remove Codegen `build.sh` call from iOS build, use `require.resolve` for `combine-js-to-schema-cli.js` entry point.

Externally significant FYIs:
- `react-native/codegen` is converted to use the `"exports"` field — it should export all `.js` files, as before.
- `codegenPath` is now ignored and marked as deprecated on `ReactExtensions.kt`.

NOTE: TypeScript auto-generation is not yet enabled on this package, since it uses CommonJS `module.exports` syntax (unsupported by `flow-api-translator`).

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D49370200

fbshipit-source-id: 992913155169912ea1a3cb24cb26efbd3f783058
2023-10-23 08:32:33 -07:00
Gijs Weterings
67c1a806e6 Flow strictify xplat/js/react-native-github where possible (#41051)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41051

Strictifies flow to flow strict-local in files where doing that doesn't cause new flow errors.

Changelog: Internal

Reviewed By: yungsters

Differential Revision: D50369011

fbshipit-source-id: b4a5a26b839b7327a3178e6f5b35246dea365a38
2023-10-18 05:36:33 -07:00
Rob Hogan
cda1cf9349 Prevent Babel registration from transforming plugins with arbitrary config (#39429)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39429

Using `require` after `const registerFn = require('babel/register')` but before `registerFn(config)` causes Babel to transform required code with the default configuration (ie, using a nearby `babel.config.js`, if available).

This was causing the Babel plugins loaded by `metro-babel-register` to be (unnecessarily) transformed according to `babel.config.js`, which actually fails if the plugins/presets referenced in `babel.config.js` themselves require transformation.

This ensures no code is loaded in between registering Babel as a side effect of requiring Babel register, and replacing that hook with something explicitly configured.

## React Native
Changelog: [Internal]

## Metro
```
* **[Fix]:** `metro-babel-register` prevent arbitrary transformation of Babel plugins during registration setup
```

Reviewed By: dmytrorykun

Differential Revision: D49238671

fbshipit-source-id: 52a55b1b5dbd127171558c056f16ab04e8fa8232
2023-09-14 03:57:11 -07:00
Moti Zilberman
be469c1b86 Resolve absolute paths of Babel presets/plugins in Node build config (#39079)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39079

Changelog: [Internal]

The Babel config for Node packages in the RN repo, added in D47760330 / D48312463, relies on [name normalization](https://babeljs.io/docs/options#name-normalization) to resolve the Babel presets and plugins that it references. This works for OSS but can cause other integrations to fail to resolve the packages, depending on the details of how they're installed. Here we eagerly resolve the packages when constructing the Babel config.

Reviewed By: huntie

Differential Revision: D48469386

fbshipit-source-id: d9c15883169e30984d93fc6a5d9544752db5d2c8
2023-08-18 07:32:46 -07:00
Alex Hunt
a978d343a6 Add auto-generation of TypeScript definitions on build (#38990)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38990

This PR adds auto-generation of Typescript definitions from Flow source code for packages using the shared monorepo build setup (https://github.com/facebook/react-native/pull/38718).

Today, these are the following Node.js packages:

- `packages/community-cli-plugin`
- `packages/dev-middleware` (⬅️ `emitTypeScriptDefs` enabled)

This also improves emitted Flow definitions (`.js.flow`), by using [`flow-api-translator`](https://www.npmjs.com/package/flow-api-translator) to strip implementations.

**All changes**

- Include `flow-api-translator` and configure this to emit type definitions as part of `yarn build`.
    - Add translation from Flow source to TypeScript definitions (`.d.ts`) adjacent to each built file.
    - Improve emitted Flow definitions (`.js.flow`), by using `flow-api-translator` to strip implementations (previously, source files were copied). The Flow and TS defs now mirror each other.
-  Add `emitFlowDefs` and `emitTypeScriptDefs` options to build config to configure the above.
- Integrate TypeScript compiler to perform program validation on emitted `.d.ts` files.
     - This is based on this guide: https://github.com/microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md#a-minimal-compiler.
- Throw an exception on the `rewritePackageExports` step if a package does not define an `"exports"` field.
- Add minimal `flow-typed` definitions for `typescript` 😄.

**Notes on [`flow-api-translator`](https://www.npmjs.com/package/flow-api-translator)**

This project is experimental but is in a more mature state than when we evaluated it earlier in 2023.
- It's now possible to run this tool on our new Node.js packages, since they are exclusively authored using `import`/`export` syntax (a requirement of the tool).
- As a safety net, we run the TypeScript compiler against the generated program, which will fail the build.

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D48312463

fbshipit-source-id: 817edb35f911f52fa987946f2d8fc1a319078c9d
2023-08-14 12:12:10 -07:00
Alex Hunt
e1998806b7 Migrate cli-plugin-metro into repo (#38795)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38795

## Context

RFC: Decoupling Flipper from React Native core: https://github.com/react-native-community/discussions-and-proposals/pull/641

## Changes

Inits new package `react-native/community-cli-plugin`. This migrates [`react-native-community/cli-plugin-metro`](https://github.com/react-native-community/cli/tree/main/packages/cli-plugin-metro) into the React Native repo, to enable faster iteration by the React Native core team. Specifically:

- This package contains several `metro` dependencies, which when removed from CLI will no longer require us to ship new CLI releases to get Metro patches and features to users.
- This package contains the `start`, `bundle`, and `ram-bundle` commands (central to the React Native development experience), for which we have incoming debugging-related changes.
- This package now **only** exports commands to be attached via a RN CLI plugin. With this move, we're aiming to **internalise** the default implementations of these dev commands within React Native — other RN CLI plugins can continue to override these, but must do so wholesale. (See also the recent fix for this: https://github.com/react-native-community/cli/pull/1999.)

In V15:
- (Microsoft feedback) Re-export  `unstable_buildBundleWithConfig`, marking as unstable. This gives us a time buffer to consider how we repackage this functionality in future.

The package source has been converted from TypeScript to Flow, with a number of new `flow-typed/` defs added to meet type coverage requirements.

## To dos

- For now, we aren't removing the existing [`react-native-community/cli-plugin-metro`](https://github.com/react-native-community/cli/tree/main/packages/cli-plugin-metro) source — until later PRs consolidate this move by changing dependencies in the `react-native` package.
- **Exported API is reduced!**: I'm working with szymonrybczak to decouple references from RN CLI packages https://github.com/react-native-community/cli/pull/2021.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D46801501

fbshipit-source-id: 7f6b72941a69f487fb437768cdba125a9aa3418d
2023-08-10 11:34:36 -07:00
Alex Hunt
cd8f5d176a Add shared monorepo build setup (#38718)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38718

> NOTE: Replaces https://github.com/facebook/react-native/pull/38240

## Context

RFC: Decoupling Flipper from React Native core: https://github.com/react-native-community/discussions-and-proposals/pull/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`](1f019afdcd/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]

Reviewed By: NickGerleman

Differential Revision: D47760330

fbshipit-source-id: 38ec94708ce3d9946a197d80885781e9707c5841
2023-08-03 04:42:30 -07:00