Skip to content

Commit

Permalink
Prevent Babel registration from transforming plugins with arbitrary c…
Browse files Browse the repository at this point in the history
…onfig (#39429)

Summary:

X-link: facebook/metro#1082

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
```

Differential Revision: D49238671
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 13, 2023
1 parent 5a926c5 commit 9375d35
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions scripts/build/babel-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ function registerForMonorepo() {
function registerPackage(packageName /*: string */) {
const packageDir = path.join(PACKAGES_DIR, packageName);

require('@babel/register')({
// Prepare the config object before calling `require('@babel/register')` to
// prevent `require` calls within `getBabelConfig` triggering Babel to
// attempt to load its config from a babel.config file.
const registerConfig = {
...getBabelConfig(packageName),
root: packageDir,
ignore: [/\/node_modules\//],
});
};

require('@babel/register')(registerConfig);
}

module.exports = {registerForMonorepo};

0 comments on commit 9375d35

Please sign in to comment.