diff --git a/packages/react-native-babel-transformer/src/__tests__/inline-platform-integration-test.js b/packages/react-native-babel-transformer/src/__tests__/inline-platform-integration-test.js index dfbf52083153..f42f2a79da37 100644 --- a/packages/react-native-babel-transformer/src/__tests__/inline-platform-integration-test.js +++ b/packages/react-native-babel-transformer/src/__tests__/inline-platform-integration-test.js @@ -17,9 +17,7 @@ const PROJECT_ROOT = path.sep === '/' ? '/my/project' : 'C:\\my\\project'; const RN_ROOT = path.join(PROJECT_ROOT, 'node_modules', 'react-native'); // The transformer memoizes its resolved Babel config in a module-level -// closure, keyed on nothing - so a fresh module instance is required for every -// distinct `options` shape, or later variants silently reuse the first -// variant's config. +// closure, so a fresh module instance keeps each test independent. beforeEach(() => { jest.resetModules(); }); @@ -171,9 +169,8 @@ describe.each([false, true])( ); test('the two import-support modes really do produce different output', () => { - // Guards the test setup itself: without a module reset between variants the - // memoized config leaks and the parameterized suite above would silently run - // the same configuration twice. + // Guards the test setup itself: the parameterized suite above must really + // run two different configurations. const src = "import {Platform} from 'react-native';\nconst x = Other.thing;"; const lowered = transformToCode(src, {experimentalImportSupport: false}); @@ -185,6 +182,17 @@ test('the two import-support modes really do produce different output', () => { expect(preserved).not.toContain('require'); }); +test('inlines each platform from one module instance', () => { + const src = "import {Platform} from 'react-native';\nconst os = Platform.OS;"; + + const ios = transformToCode(src, {platform: 'ios'}); + const android = transformToCode(src, {platform: 'android'}); + + expect(ios).toContain('"ios"'); + expect(android).toContain('"android"'); + expect(android).not.toContain('"ios"'); +}); + test('inlines before the preset lowers ESM to CommonJS interop', () => { // Guards the ordering contract: if the plugin ran after the RN preset's // import transform, it would see `_reactNative.Platform.OS` and the diff --git a/packages/react-native-babel-transformer/src/index.js b/packages/react-native-babel-transformer/src/index.js index 1d42cc894d9f..ecc77d0003c7 100644 --- a/packages/react-native-babel-transformer/src/index.js +++ b/packages/react-native-babel-transformer/src/index.js @@ -48,7 +48,7 @@ function isTSXSource(fileName /*: string */) { * default RN babelrc file and uses that. */ const getBabelRC = (function () { - let babelRC /*: ?BabelCoreOptions */ = null; + const babelRCs /*: Map */ = new Map(); /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's * LTI update could not be added via codemod */ @@ -57,6 +57,8 @@ const getBabelRC = (function () { extendsBabelConfigPath, ...options }) { + const cacheKey = JSON.stringify(options); + let babelRC = babelRCs.get(cacheKey); if (babelRC != null) { return babelRC; } @@ -65,6 +67,7 @@ const getBabelRC = (function () { plugins: [], extends: extendsBabelConfigPath, }; + babelRCs.set(cacheKey, babelRC); if (extendsBabelConfigPath) { return babelRC; @@ -92,14 +95,12 @@ const getBabelRC = (function () { // If we found a babel config file, extend our config off of it // otherwise the default config will be used if (fs.existsSync(projectBabelRCPath)) { - // $FlowFixMe[incompatible-use] `extends` is missing in null or undefined. babelRC.extends = projectBabelRCPath; } } // If a babel config file doesn't exist in the project then // the default preset for react-native will be used instead. - // $FlowFixMe[incompatible-use] `extends` is missing in null or undefined. if (!babelRC.extends) { const { babelRuntimeModuleName, @@ -108,7 +109,6 @@ const getBabelRC = (function () { ...presetOptions } = options; - // $FlowFixMe[incompatible-use] `presets` is missing in null or undefined. babelRC.presets = [ [ require('@react-native/babel-preset'),