diff --git a/packages/react-native-babel-transformer/src/index.js b/packages/react-native-babel-transformer/src/index.js index 56b8a9a0a1a7..d2297ea461b7 100644 --- a/packages/react-native-babel-transformer/src/index.js +++ b/packages/react-native-babel-transformer/src/index.js @@ -141,6 +141,9 @@ function buildBabelConfig( : true, code: false, cwd: options.projectRoot, + envName: options.dev + ? 'development' + : process.env.BABEL_ENV || 'production', filename, highlightCode: true, }; @@ -183,67 +186,49 @@ const transform /*: BabelTransformer['transform'] */ = ({ src, plugins, }) => { - const OLD_BABEL_ENV = process.env.BABEL_ENV; - process.env.BABEL_ENV = options.dev - ? 'development' - : process.env.BABEL_ENV || 'production'; - - try { - const babelConfig /*: BabelCoreOptions */ = { - // ES modules require sourceType='module' but OSS may not always want that - sourceType: 'unambiguous', - ...buildBabelConfig(filename, options, plugins), - caller: { - // Varies Babel's config cache - presets will be re-initialized - // if they use caller information. - name: 'metro', - bundler: 'metro', - platform: options.platform, - unstable_transformProfile: options.unstable_transformProfile, - }, - ast: true, - - // NOTE(EvanBacon): We split the parse/transform steps up to accommodate - // Hermes parsing, but this defaults to cloning the AST which increases - // the transformation time by a fair amount. - // You get this behavior by default when using Babel's `transform` method directly. - cloneInputAst: false, - }; - const sourceAst /*: BabelNodeFile */ = - isTypeScriptSource(filename) || - isTSXSource(filename) || - !options.hermesParser - ? parseSync(src, babelConfig) - : // $FlowFixMe[incompatible-exact] - require('hermes-parser').parse(src, { - babel: true, - reactRuntimeTarget: '19', - sourceType: babelConfig.sourceType, - }); - - const result /*: TransformResult */ = - transformFromAstSync(sourceAst, src, babelConfig); - - // The result from `transformFromAstSync` can be null (if the file is ignored) - if (!result) { - /* $FlowFixMe[incompatible-type] BabelTransformer specifies that the `ast` can never be null but - * the function returns here. Discovered when typing `BabelNode`. */ - return {ast: null}; - } - - return {ast: nullthrows(result.ast), metadata: result.metadata}; - } finally { - // Restore the old process.env.BABEL_ENV - if (OLD_BABEL_ENV == null) { - // We have to treat this as a special case because writing undefined to - // an environment variable coerces it to the string 'undefined'. To - // unset it, we must delete it. - // See https://github.com/facebook/metro/pull/446 - delete process.env.BABEL_ENV; - } else { - process.env.BABEL_ENV = OLD_BABEL_ENV; - } + const babelConfig /*: BabelCoreOptions */ = { + // ES modules require sourceType='module' but OSS may not always want that + sourceType: 'unambiguous', + ...buildBabelConfig(filename, options, plugins), + caller: { + // Varies Babel's config cache - presets will be re-initialized + // if they use caller information. + name: 'metro', + bundler: 'metro', + platform: options.platform, + unstable_transformProfile: options.unstable_transformProfile, + }, + ast: true, + + // NOTE(EvanBacon): We split the parse/transform steps up to accommodate + // Hermes parsing, but this defaults to cloning the AST which increases + // the transformation time by a fair amount. + // You get this behavior by default when using Babel's `transform` method directly. + cloneInputAst: false, + }; + const sourceAst /*: BabelNodeFile */ = + isTypeScriptSource(filename) || + isTSXSource(filename) || + !options.hermesParser + ? parseSync(src, babelConfig) + : // $FlowFixMe[incompatible-exact] + require('hermes-parser').parse(src, { + babel: true, + reactRuntimeTarget: '19', + sourceType: babelConfig.sourceType, + }); + + const result /*: TransformResult */ = + transformFromAstSync(sourceAst, src, babelConfig); + + // The result from `transformFromAstSync` can be null (if the file is ignored) + if (!result) { + /* $FlowFixMe[incompatible-type] BabelTransformer specifies that the `ast` can never be null but + * the function returns here. Discovered when typing `BabelNode`. */ + return {ast: null}; } + + return {ast: nullthrows(result.ast), metadata: result.metadata}; }; function getCacheKey() {