Skip to content

Commit de59fcc

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Fix transformer changing process.env.BABEL_ENV as a side-effect, transform tests non-determinism
Summary: X-link: facebook/react-native#55557 Currently, the RN Babel transformer that is used both by Metro and Jest, and the base transformer in Metro, set [`process.env.BABEL_ENV`](https://babeljs.io/docs/options?utm_source=chatgpt.com#envname) during transformation, presumably to ensure that Babel always uses the `development` configuration when multiple are configured. It attempts to set it back in a `finally` block - but that's guarded in the old value being truthy, which was introduced as a fix in #446 - setting `process.env.BABEL_ENV = undefined` actually gives it the string value `'undefined'`. However, the fix wasn't quite right, because it just leaves a previously-unset `BABEL_ENV` at its new value. So if `BABEL_ENV` is initially `undefined`, using the transformer sets it to `'development'`. This has a knock-on effect on `react-native/babel-preset`, which sets `dev: true` on the preset if `BABEL_ENV` is `'development'`. Changelog: [General][Fixed] Fix Babel transformer changing `BABEL_ENV` as a side effect Reviewed By: huntie Differential Revision: D93328555 fbshipit-source-id: e47f51028e60efc1c346bea44d1d8f2af264ca60
1 parent 1c2986d commit de59fcc

File tree

1 file changed

+8
-1
lines changed
  • packages/metro-babel-transformer/src

1 file changed

+8
-1
lines changed

packages/metro-babel-transformer/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ function transform({
130130
metadata: transformResult.metadata,
131131
};
132132
} finally {
133-
if (OLD_BABEL_ENV) {
133+
// Restore the old process.env.BABEL_ENV
134+
if (OLD_BABEL_ENV == null) {
135+
// We have to treat this as a special case because writing undefined to
136+
// an environment variable coerces it to the string 'undefined'. To
137+
// unset it, we must delete it.
138+
// See https://github.com/facebook/metro/pull/446
139+
delete process.env.BABEL_ENV;
140+
} else {
134141
process.env.BABEL_ENV = OLD_BABEL_ENV;
135142
}
136143
}

0 commit comments

Comments
 (0)