From 3968788b2cedf850bbd3316c3c44234013eb86fb Mon Sep 17 00:00:00 2001 From: lazerg Date: Mon, 16 Nov 2026 19:20:21 +0500 Subject: [PATCH 1/2] Memoize the default Babel config per transform options in metro-babel-transformer --- .../src/__tests__/inline-platform-integration-test.js | 11 +++++++++++ packages/react-native-babel-transformer/src/index.js | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) 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..e31cb524c8c6 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 @@ -185,6 +185,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 00a626926757..806ac288ddcd 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; From 4e1cdebf383d0bfae8953de6f650d09dd21a68f7 Mon Sep 17 00:00:00 2001 From: lazerg Date: Mon, 16 Nov 2026 19:23:16 +0500 Subject: [PATCH 2/2] Drop unused Flow suppressions and stale memoization comments --- .../src/__tests__/inline-platform-integration-test.js | 9 +++------ packages/react-native-babel-transformer/src/index.js | 4 ---- 2 files changed, 3 insertions(+), 10 deletions(-) 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 e31cb524c8c6..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}); diff --git a/packages/react-native-babel-transformer/src/index.js b/packages/react-native-babel-transformer/src/index.js index 806ac288ddcd..ecc77d0003c7 100644 --- a/packages/react-native-babel-transformer/src/index.js +++ b/packages/react-native-babel-transformer/src/index.js @@ -95,15 +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. - // $FlowFixMe[incompatible-type] `extends` is missing in null or undefined. if (!babelRC.extends) { const { babelRuntimeModuleName, @@ -112,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'),