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:
Pull Request resolved: #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
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 14, 2023
1 parent b29b393 commit cda1cf9
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 cda1cf9

Please sign in to comment.