Skip to content

Commit f2ebab8

Browse files
robhoganfacebook-github-bot
authored andcommitted
Babel transformer: Use envName to hint default value of dev, don't mutate process.env.BABEL_ENV
Summary: After #55805, the RN Babel preset no longer reads `process.env.BABEL_ENV` directly, instead using Babel's `env()` API, which is both cache-aware and properly respects explicit `envName` config. See: - https://babeljs.io/docs/config-files#apienv - https://babeljs.io/docs/options#envname Now, instead of mutating `process.env.BABEL_ENV` to force the environment for a transform, we can specify `envName` instead. Changelog: [Internal] Differential Revision: D94662935
1 parent 622b5e9 commit f2ebab8

File tree

1 file changed

+45
-60
lines changed
  • packages/react-native-babel-transformer/src

1 file changed

+45
-60
lines changed

packages/react-native-babel-transformer/src/index.js

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ function buildBabelConfig(
141141
: true,
142142
code: false,
143143
cwd: options.projectRoot,
144+
envName: options.dev
145+
? 'development'
146+
: process.env.BABEL_ENV || 'production',
144147
filename,
145148
highlightCode: true,
146149
};
@@ -183,67 +186,49 @@ const transform /*: BabelTransformer['transform'] */ = ({
183186
src,
184187
plugins,
185188
}) => {
186-
const OLD_BABEL_ENV = process.env.BABEL_ENV;
187-
process.env.BABEL_ENV = options.dev
188-
? 'development'
189-
: process.env.BABEL_ENV || 'production';
190-
191-
try {
192-
const babelConfig /*: BabelCoreOptions */ = {
193-
// ES modules require sourceType='module' but OSS may not always want that
194-
sourceType: 'unambiguous',
195-
...buildBabelConfig(filename, options, plugins),
196-
caller: {
197-
// Varies Babel's config cache - presets will be re-initialized
198-
// if they use caller information.
199-
name: 'metro',
200-
bundler: 'metro',
201-
platform: options.platform,
202-
unstable_transformProfile: options.unstable_transformProfile,
203-
},
204-
ast: true,
205-
206-
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate
207-
// Hermes parsing, but this defaults to cloning the AST which increases
208-
// the transformation time by a fair amount.
209-
// You get this behavior by default when using Babel's `transform` method directly.
210-
cloneInputAst: false,
211-
};
212-
const sourceAst /*: BabelNodeFile */ =
213-
isTypeScriptSource(filename) ||
214-
isTSXSource(filename) ||
215-
!options.hermesParser
216-
? parseSync(src, babelConfig)
217-
: // $FlowFixMe[incompatible-exact]
218-
require('hermes-parser').parse(src, {
219-
babel: true,
220-
reactRuntimeTarget: '19',
221-
sourceType: babelConfig.sourceType,
222-
});
223-
224-
const result /*: TransformResult<MetroBabelFileMetadata> */ =
225-
transformFromAstSync(sourceAst, src, babelConfig);
226-
227-
// The result from `transformFromAstSync` can be null (if the file is ignored)
228-
if (!result) {
229-
/* $FlowFixMe[incompatible-type] BabelTransformer specifies that the `ast` can never be null but
230-
* the function returns here. Discovered when typing `BabelNode`. */
231-
return {ast: null};
232-
}
233-
234-
return {ast: nullthrows(result.ast), metadata: result.metadata};
235-
} finally {
236-
// Restore the old process.env.BABEL_ENV
237-
if (OLD_BABEL_ENV == null) {
238-
// We have to treat this as a special case because writing undefined to
239-
// an environment variable coerces it to the string 'undefined'. To
240-
// unset it, we must delete it.
241-
// See https://github.com/facebook/metro/pull/446
242-
delete process.env.BABEL_ENV;
243-
} else {
244-
process.env.BABEL_ENV = OLD_BABEL_ENV;
245-
}
189+
const babelConfig /*: BabelCoreOptions */ = {
190+
// ES modules require sourceType='module' but OSS may not always want that
191+
sourceType: 'unambiguous',
192+
...buildBabelConfig(filename, options, plugins),
193+
caller: {
194+
// Varies Babel's config cache - presets will be re-initialized
195+
// if they use caller information.
196+
name: 'metro',
197+
bundler: 'metro',
198+
platform: options.platform,
199+
unstable_transformProfile: options.unstable_transformProfile,
200+
},
201+
ast: true,
202+
203+
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate
204+
// Hermes parsing, but this defaults to cloning the AST which increases
205+
// the transformation time by a fair amount.
206+
// You get this behavior by default when using Babel's `transform` method directly.
207+
cloneInputAst: false,
208+
};
209+
const sourceAst /*: BabelNodeFile */ =
210+
isTypeScriptSource(filename) ||
211+
isTSXSource(filename) ||
212+
!options.hermesParser
213+
? parseSync(src, babelConfig)
214+
: // $FlowFixMe[incompatible-exact]
215+
require('hermes-parser').parse(src, {
216+
babel: true,
217+
reactRuntimeTarget: '19',
218+
sourceType: babelConfig.sourceType,
219+
});
220+
221+
const result /*: TransformResult<MetroBabelFileMetadata> */ =
222+
transformFromAstSync(sourceAst, src, babelConfig);
223+
224+
// The result from `transformFromAstSync` can be null (if the file is ignored)
225+
if (!result) {
226+
/* $FlowFixMe[incompatible-type] BabelTransformer specifies that the `ast` can never be null but
227+
* the function returns here. Discovered when typing `BabelNode`. */
228+
return {ast: null};
246229
}
230+
231+
return {ast: nullthrows(result.ast), metadata: result.metadata};
247232
};
248233

249234
function getCacheKey() {

0 commit comments

Comments
 (0)