Skip to content

Commit

Permalink
import correct babel register
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/metro#1371

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

### The bug
In the pre-fix setup `babel/register` was installed twice:

Once in-
* **`xplat/js/node_modules`**
  * version `7.22.5` forced in `js/tools/metro/packages/metro-babel-register/package.json`.

And once in-
* **`xplat/js/react-native-github/node_modules`**
  * Version `7.24.6` as a dependency of `"metro-babel-register": "^0.81.0-alpha.2",`

When `js1 run` is launched, there's a call to
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/tools/metro/packages/metro/src/index.js?lines=19
and from there to
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/tools/metro/packages/metro-babel-register/src/babel-register.js?lines=164-171
which calls
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/tools/babel-register/index.js?lines=23-29
and registers the three paths:
* `xplat/js/react-native-github/packages`
* `xplat/js/RKJSModules`
* `xplat/js/tools/(?!node_modules/)`

by calling
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/tools/metro/packages/metro-babel-register/src/babel-register.js?lines=50
---
---
Later, when Metro is setting up `react-native/dev-middleware` in `fbsource`, it was calling-
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/react-native-github/packages/dev-middleware/src/index.js?lines=16-18
which was calling
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/react-native-github/scripts/build/babel-register.js?lines=28-36
which was setting up `babel/register` to transpile only one path instead of the three that were already set up:
`PACKAGES_DIR == `**`"xplat/js/react-native-github/packages"`**
by calling
https://www.internalfb.com/code/fbsource/[ba7f25a7faa2]/xplat/js/tools/metro/packages/metro-babel-register/src/babel-register.js?lines=50

---
---

Now the only reason it worked when `babel/register` was installed twice in `node_modules` of different packages was that this latter call to `babel/register` **was COMPLETELY IGNORED** and we kept using the real `babel/register` that was properly set-up with three paths before.

This means that when we previously tried to update `babel/register` and dedupe it, this call was not ignored. It was instead actually re-writing the paths that `babel/register` was transpiling to include only **`"xplat/js/react-native-github/packages"`** so we saw error coming out of the other two paths.

This means that if we update `xplat/js/react-native-github` to use the latest `metro-babel-register` the issue will come back. This is because both the version specified and the version in fbsource will have the same version of `babel/register` (the pinned `7.22.5`).

### The fix
1. Update and dedupe `babel/register`.
1. Make `js/react-native-github/scripts/build/babel-register.js` that is responsible for calling `babel/register` respect `process.env.FBSOURCE_ENV` and call the `xplat/js` monorepo instead of the `react-native-github` monorepo only.

Changelog: [Internal]

Differential Revision: D64245277
  • Loading branch information
vzaidman authored and facebook-github-bot committed Oct 11, 2024
1 parent c86b5c1 commit 00ff705
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion scripts/build/babel-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ function registerForMonorepo() {
return;
}

require('metro-babel-register')([PACKAGES_DIR]);
if (process.env.FBSOURCE_ENV === '1') {
require('@fb-tools/babel-register');
} else {
require('metro-babel-register')([PACKAGES_DIR]);
}

isRegisteredForMonorepo = true;
}
Expand Down

0 comments on commit 00ff705

Please sign in to comment.