From 904f0aeb66abb484ad876d5f99e8a8cdf7f4a414 Mon Sep 17 00:00:00 2001 From: Emerson Berlik Date: Tue, 7 Jul 2026 12:55:32 -0700 Subject: [PATCH] Revert D110890810: Expose 'react-native/setup-env' to replace InitializeCore Differential Revision: D110890810 Original commit changeset: 3afe25efa455 Original Phabricator Diff: D110890810 fbshipit-source-id: 551c04d1e6c66e1d29938be3c2a7a57db475d589 --- .flowconfig | 1 - .../src/utils/loadMetroConfig.js | 13 +++--- .../__tests__/no-deep-imports-test.js | 30 +------------ .../no-deep-imports.js | 44 +++++-------------- packages/eslint-plugin-react-native/utils.js | 7 --- packages/jest-preset/jest-preset.js | 5 --- packages/jest-preset/jest/setup.js | 1 - packages/metro-config/src/index.flow.js | 2 +- .../plugin-warn-on-deep-imports-test.js | 14 ++++++ .../src/plugin-warn-on-deep-imports.js | 17 +++++-- .../Libraries/Core/InitializeCore.js | 1 - packages/react-native/package.json | 1 - packages/react-native/src/asset-registry.js | 2 +- packages/react-native/src/setup-env.js | 22 ---------- .../IntegrationTests/IntegrationTestsApp.js | 3 +- 15 files changed, 50 insertions(+), 113 deletions(-) delete mode 100644 packages/react-native/src/setup-env.js diff --git a/.flowconfig b/.flowconfig index 915f8b340ad..4d01ae0c0e4 100644 --- a/.flowconfig +++ b/.flowconfig @@ -56,7 +56,6 @@ experimental.multi_platform.extensions=.android munge_underscores=true module.name_mapper='^react-native$' -> '/packages/react-native/index.js' -module.name_mapper='^react-native/setup-env$' -> '/packages/react-native/src/setup-env.js' module.name_mapper='^react-native/\(.*\)$' -> '/packages/react-native/\1' module.name_mapper='^@react-native/dev-middleware$' -> '/packages/dev-middleware' module.name_mapper='^@?[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\|xml\|ktx\|heic\|heif\)$' -> '/packages/react-native/Libraries/Image/RelativeImageStub' diff --git a/packages/community-cli-plugin/src/utils/loadMetroConfig.js b/packages/community-cli-plugin/src/utils/loadMetroConfig.js index a41b028fa29..43d9e4126af 100644 --- a/packages/community-cli-plugin/src/utils/loadMetroConfig.js +++ b/packages/community-cli-plugin/src/utils/loadMetroConfig.js @@ -60,17 +60,16 @@ function getCommunityCliDefaultConfig( return { resolver, serializer: { - // We can include multiple copies of setup-env here because Metro will + // We can include multiple copies of InitializeCore here because metro will // only add ones that are already part of the bundle getModulesRunBeforeMainModule: () => [ - // NOTE: ctx.reactNativePath is an absolute path, therefore we need to - // reference setup-env.js here by exact path specifier. - require.resolve(path.join(ctx.reactNativePath, 'src/setup-env.js'), { - paths: [ctx.root], - }), + require.resolve( + path.join(ctx.reactNativePath, 'Libraries/Core/InitializeCore'), + {paths: [ctx.root]}, + ), ...outOfTreePlatforms.map(platform => require.resolve( - `${ctx.platforms[platform].npmPackageName}/setup-env`, + `${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`, {paths: [ctx.root]}, ), ), diff --git a/packages/eslint-plugin-react-native/__tests__/no-deep-imports-test.js b/packages/eslint-plugin-react-native/__tests__/no-deep-imports-test.js index bf14e917f7b..d28b33f4583 100644 --- a/packages/eslint-plugin-react-native/__tests__/no-deep-imports-test.js +++ b/packages/eslint-plugin-react-native/__tests__/no-deep-imports-test.js @@ -29,10 +29,10 @@ eslintTester.run('../no-deep-imports', rule, { "import Foo from 'react-native-foo';", "import Foo from 'react-native-foo/Foo';", "import Foo from 'react/native/Foo';", + "import 'react-native/Libraries/Core/InitializeCore';", + "require('react-native/Libraries/Core/InitializeCore');", "import Foo from 'react-native/src/fb_internal/Foo'", "require('react-native/src/fb_internal/Foo')", - "import 'react-native/setup-env';", - "require('react-native/setup-env');", ], invalid: [ { @@ -125,31 +125,5 @@ eslintTester.run('../no-deep-imports', rule, { ], output: null, }, - { - code: "import 'react-native/Libraries/Core/InitializeCore';", - errors: [ - { - messageId: 'useReplacementSource', - data: { - importPath: 'react-native/Libraries/Core/InitializeCore', - replacementSource: 'react-native/setup-env', - }, - }, - ], - output: "import 'react-native/setup-env';", - }, - { - code: "require('react-native/Libraries/Core/InitializeCore');", - errors: [ - { - messageId: 'useReplacementSource', - data: { - importPath: 'react-native/Libraries/Core/InitializeCore', - replacementSource: 'react-native/setup-env', - }, - }, - ], - output: "require('react-native/setup-env');", - }, ], }); diff --git a/packages/eslint-plugin-react-native/no-deep-imports.js b/packages/eslint-plugin-react-native/no-deep-imports.js index e84a1f24d45..6446c70ba6a 100644 --- a/packages/eslint-plugin-react-native/no-deep-imports.js +++ b/packages/eslint-plugin-react-native/no-deep-imports.js @@ -21,8 +21,6 @@ module.exports = { messages: { deepImport: "'{{importPath}}' React Native deep imports are deprecated. Please use the top level import instead.", - useReplacementSource: - "'{{importPath}}' is deprecated. Please import '{{replacementSource}}' instead.", }, schema: [], fixable: 'code', @@ -33,14 +31,12 @@ module.exports = { ImportDeclaration(node) { if ( !isDeepReactNativeImport(node.source) || + isInitializeCoreImport(node.source) || isSecondaryEntryPoint(node.source) || isFbInternalImport(node.source) ) { return; } - if (reportReplacementSource(node.source)) { - return; - } if (isDefaultImport(node)) { const reactNativeSource = node.source.value.slice( 'react-native/'.length, @@ -92,16 +88,13 @@ module.exports = { CallExpression(node) { if ( !isDeepRequire(node) || + isInitializeCoreImport(node.arguments[0]) || isSecondaryEntryPoint(node.arguments[0]) || isFbInternalImport(node.arguments[0]) ) { return; } - if (reportReplacementSource(node.arguments[0])) { - return; - } - const parent = node.parent; const importPath = node.arguments[0].value; @@ -130,26 +123,6 @@ module.exports = { }, }; - function reportReplacementSource(source) { - const reactNativeSource = source.value.slice('react-native/'.length); - const mapping = publicAPIMapping[reactNativeSource]; - if (!mapping || !mapping.replacementSource) { - return false; - } - context.report({ - node: source, - messageId: 'useReplacementSource', - data: { - importPath: source.value, - replacementSource: mapping.replacementSource, - }, - fix(fixer) { - return fixer.replaceText(source, `'${mapping.replacementSource}'`); - }, - }); - return true; - } - function getStandardReport(source) { return { node: source, @@ -194,15 +167,20 @@ module.exports = { return parts.length > 1 && parts[0] === 'react-native'; } + function isInitializeCoreImport(source) { + if (source.type !== 'Literal' || typeof source.value !== 'string') { + return false; + } + + return source.value === 'react-native/Libraries/Core/InitializeCore'; + } + function isSecondaryEntryPoint(source) { if (source.type !== 'Literal' || typeof source.value !== 'string') { return false; } - return ( - source.value === 'react-native/asset-registry' || - source.value === 'react-native/setup-env' - ); + return source.value === 'react-native/asset-registry'; } function isFbInternalImport(source) { diff --git a/packages/eslint-plugin-react-native/utils.js b/packages/eslint-plugin-react-native/utils.js index 728cc26d6ab..98d1e90ca0b 100644 --- a/packages/eslint-plugin-react-native/utils.js +++ b/packages/eslint-plugin-react-native/utils.js @@ -37,13 +37,6 @@ const publicAPIMapping = { default: 'experimental_LayoutConformance', types: ['LayoutConformanceProps'], }, - 'Libraries/Core/InitializeCore': { - // `InitializeCore` has no public named export; the deep import must be - // swapped for the `react-native/setup-env` entry point entirely. - default: null, - types: null, - replacementSource: 'react-native/setup-env', - }, 'Libraries/Lists/FlatList': { default: 'FlatList', types: ['FlatListProps'], diff --git a/packages/jest-preset/jest-preset.js b/packages/jest-preset/jest-preset.js index bfe131904a2..5632cc7fe5c 100644 --- a/packages/jest-preset/jest-preset.js +++ b/packages/jest-preset/jest-preset.js @@ -18,11 +18,6 @@ module.exports = { platforms: ['android', 'ios', 'native'], }, moduleNameMapper: { - // `setup-env` is a secondary entry point exposed via the package's - // `exports`, but `./jest/resolver.js` strips `exports` and the generic - // mapper below resolves subpaths as literal directory paths. Alias it - // explicitly so it resolves to its `src/` implementation. - '^react-native/setup-env$': `${path.dirname(require.resolve('react-native'))}/src/setup-env.js`, '^react-native($|/.*)': `${path.dirname(require.resolve('react-native'))}/$1`, }, resolver: require.resolve('./jest/resolver.js'), diff --git a/packages/jest-preset/jest/setup.js b/packages/jest-preset/jest/setup.js index 483f14cad8f..29f86698a12 100644 --- a/packages/jest-preset/jest/setup.js +++ b/packages/jest-preset/jest/setup.js @@ -133,7 +133,6 @@ mock( 'm#react-native/Libraries/Core/InitializeCore', 'm#./mocks/InitializeCore', ); -mock('m#react-native/setup-env', 'm#./mocks/InitializeCore'); mock('m#react-native/Libraries/Core/NativeExceptionsManager'); mock('m#react-native/Libraries/Image/Image', 'm#./mocks/Image'); mock( diff --git a/packages/metro-config/src/index.flow.js b/packages/metro-config/src/index.flow.js index dcac214b397..03a9aed5ace 100644 --- a/packages/metro-config/src/index.flow.js +++ b/packages/metro-config/src/index.flow.js @@ -61,7 +61,7 @@ export function getDefaultConfig(projectRoot: string): ConfigT { serializer: { // Note: This option is overridden in cli-plugin-metro (getOverrideConfig) getModulesRunBeforeMainModule: () => [ - require.resolve('react-native/setup-env'), + require.resolve('react-native/Libraries/Core/InitializeCore'), ], getPolyfills: () => require('@react-native/js-polyfills')(), isThirdPartyModule({path: modulePath}: Readonly<{path: string, ...}>) { diff --git a/packages/react-native-babel-preset/src/__tests__/plugin-warn-on-deep-imports-test.js b/packages/react-native-babel-preset/src/__tests__/plugin-warn-on-deep-imports-test.js index 66f247d7112..820fc9c59ea 100644 --- a/packages/react-native-babel-preset/src/__tests__/plugin-warn-on-deep-imports-test.js +++ b/packages/react-native-babel-preset/src/__tests__/plugin-warn-on-deep-imports-test.js @@ -82,3 +82,17 @@ test('import from other package', () => { `"import { foo } from 'react-native-foo';"`, ); }); + +test('import react-native/Libraries/Core/InitializeCore', () => { + const code = ` + import 'react-native/Libraries/Core/InitializeCore'; + require('react-native/Libraries/Core/InitializeCore'); + export * from 'react-native/Libraries/Core/InitializeCore'; + `; + + expect(transform(code, [rnDeepImportsWarningPlugin])).toMatchInlineSnapshot(` + "import 'react-native/Libraries/Core/InitializeCore'; + require('react-native/Libraries/Core/InitializeCore'); + export * from 'react-native/Libraries/Core/InitializeCore';" + `); +}); diff --git a/packages/react-native-babel-preset/src/plugin-warn-on-deep-imports.js b/packages/react-native-babel-preset/src/plugin-warn-on-deep-imports.js index 5e98ed59ad7..98f317586fa 100644 --- a/packages/react-native-babel-preset/src/plugin-warn-on-deep-imports.js +++ b/packages/react-native-babel-preset/src/plugin-warn-on-deep-imports.js @@ -38,6 +38,10 @@ function isDeepReactNativeImport(source) { return parts.length > 1 && parts[0] === 'react-native'; } +function isInitializeCoreImport(source) { + return source === 'react-native/Libraries/Core/InitializeCore'; +} + function withLocation(node, loc) { if (!node.loc) { return {...node, loc}; @@ -51,7 +55,7 @@ module.exports = ({types: t}) => ({ ImportDeclaration(path, state) { const source = path.node.source.value; - if (isDeepReactNativeImport(source)) { + if (isDeepReactNativeImport(source) && !isInitializeCoreImport(source)) { const loc = path.node.loc; state.import.push({source, loc}); } @@ -67,7 +71,10 @@ module.exports = ({types: t}) => ({ ) { const source = args[0].node.type === 'StringLiteral' ? args[0].node.value : ''; - if (isDeepReactNativeImport(source)) { + if ( + isDeepReactNativeImport(source) && + !isInitializeCoreImport(source) + ) { const loc = path.node.loc; state.require.push({source, loc}); } @@ -76,7 +83,11 @@ module.exports = ({types: t}) => ({ ExportNamedDeclaration(path, state) { const source = path.node.source; - if (source && isDeepReactNativeImport(source.value)) { + if ( + source && + isDeepReactNativeImport(source.value) && + !isInitializeCoreImport(source) + ) { const loc = path.node.loc; state.export.push({source: source.value, loc}); } diff --git a/packages/react-native/Libraries/Core/InitializeCore.js b/packages/react-native/Libraries/Core/InitializeCore.js index 8846ce4dea5..c3ba8331837 100644 --- a/packages/react-native/Libraries/Core/InitializeCore.js +++ b/packages/react-native/Libraries/Core/InitializeCore.js @@ -22,7 +22,6 @@ * 1. Require system. * 2. Bridged modules. * - * @deprecated Since 0.87. Use `'react-native/setup-env'` instead. */ 'use strict'; diff --git a/packages/react-native/package.json b/packages/react-native/package.json index d3adca8a16c..fe57f01f910 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -52,7 +52,6 @@ "types": null, "default": "./src/asset-registry.js" }, - "./setup-env": "./src/setup-env.js", "./jest-preset": "./jest-preset.js", "./rn-get-polyfills": "./rn-get-polyfills.js", "./src/fb_internal/*": "./src/fb_internal/*", diff --git a/packages/react-native/src/asset-registry.js b/packages/react-native/src/asset-registry.js index 6d3a4cdf34b..e98f442e769 100644 --- a/packages/react-native/src/asset-registry.js +++ b/packages/react-native/src/asset-registry.js @@ -11,7 +11,7 @@ 'use strict'; // ---------------------------------------------------------------------------- -// react-native/asset-registry +// Secondary react-native/asset-registry entry point. // // This is an untyped secondary entry point intended to be referenced from // Metro's `transformer.assetRegistryPath` config option. This entry point may diff --git a/packages/react-native/src/setup-env.js b/packages/react-native/src/setup-env.js deleted file mode 100644 index fbe7f9178b3..00000000000 --- a/packages/react-native/src/setup-env.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict-local - * @format - */ - -'use strict'; -'use client'; - -// ---------------------------------------------------------------------------- -// react-native/setup-env -// -// Side-effectful module that sets up the core React Native JavaScript -// environment. This includes global timers (`setTimeout` etc), the global -// `console` object, and hooks for printing stack traces with source maps. -// ---------------------------------------------------------------------------- - -require('./private/setup/setUpDefaultReactNativeEnvironment').default(); diff --git a/packages/rn-tester/IntegrationTests/IntegrationTestsApp.js b/packages/rn-tester/IntegrationTests/IntegrationTestsApp.js index 1cbc4335348..c1f9cf2c760 100644 --- a/packages/rn-tester/IntegrationTests/IntegrationTestsApp.js +++ b/packages/rn-tester/IntegrationTests/IntegrationTestsApp.js @@ -10,8 +10,7 @@ 'use strict'; -require('react-native/setup-env'); - +require('react-native/Libraries/Core/InitializeCore'); const React = require('react'); const ReactNative = require('react-native');