Skip to content

Commit 904f0ae

Browse files
EmersonBmeta-codesync[bot]
authored andcommitted
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
1 parent 5d6776b commit 904f0ae

15 files changed

Lines changed: 50 additions & 113 deletions

File tree

‎.flowconfig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ experimental.multi_platform.extensions=.android
5656
munge_underscores=true
5757

5858
module.name_mapper='^react-native$' -> '<PROJECT_ROOT>/packages/react-native/index.js'
59-
module.name_mapper='^react-native/setup-env$' -> '<PROJECT_ROOT>/packages/react-native/src/setup-env.js'
6059
module.name_mapper='^react-native/\(.*\)$' -> '<PROJECT_ROOT>/packages/react-native/\1'
6160
module.name_mapper='^@react-native/dev-middleware$' -> '<PROJECT_ROOT>/packages/dev-middleware'
6261
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\)$' -> '<PROJECT_ROOT>/packages/react-native/Libraries/Image/RelativeImageStub'

‎packages/community-cli-plugin/src/utils/loadMetroConfig.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ function getCommunityCliDefaultConfig(
6060
return {
6161
resolver,
6262
serializer: {
63-
// We can include multiple copies of setup-env here because Metro will
63+
// We can include multiple copies of InitializeCore here because metro will
6464
// only add ones that are already part of the bundle
6565
getModulesRunBeforeMainModule: () => [
66-
// NOTE: ctx.reactNativePath is an absolute path, therefore we need to
67-
// reference setup-env.js here by exact path specifier.
68-
require.resolve(path.join(ctx.reactNativePath, 'src/setup-env.js'), {
69-
paths: [ctx.root],
70-
}),
66+
require.resolve(
67+
path.join(ctx.reactNativePath, 'Libraries/Core/InitializeCore'),
68+
{paths: [ctx.root]},
69+
),
7170
...outOfTreePlatforms.map(platform =>
7271
require.resolve(
73-
`${ctx.platforms[platform].npmPackageName}/setup-env`,
72+
`${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`,
7473
{paths: [ctx.root]},
7574
),
7675
),

‎packages/eslint-plugin-react-native/__tests__/no-deep-imports-test.js‎

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ eslintTester.run('../no-deep-imports', rule, {
2929
"import Foo from 'react-native-foo';",
3030
"import Foo from 'react-native-foo/Foo';",
3131
"import Foo from 'react/native/Foo';",
32+
"import 'react-native/Libraries/Core/InitializeCore';",
33+
"require('react-native/Libraries/Core/InitializeCore');",
3234
"import Foo from 'react-native/src/fb_internal/Foo'",
3335
"require('react-native/src/fb_internal/Foo')",
34-
"import 'react-native/setup-env';",
35-
"require('react-native/setup-env');",
3636
],
3737
invalid: [
3838
{
@@ -125,31 +125,5 @@ eslintTester.run('../no-deep-imports', rule, {
125125
],
126126
output: null,
127127
},
128-
{
129-
code: "import 'react-native/Libraries/Core/InitializeCore';",
130-
errors: [
131-
{
132-
messageId: 'useReplacementSource',
133-
data: {
134-
importPath: 'react-native/Libraries/Core/InitializeCore',
135-
replacementSource: 'react-native/setup-env',
136-
},
137-
},
138-
],
139-
output: "import 'react-native/setup-env';",
140-
},
141-
{
142-
code: "require('react-native/Libraries/Core/InitializeCore');",
143-
errors: [
144-
{
145-
messageId: 'useReplacementSource',
146-
data: {
147-
importPath: 'react-native/Libraries/Core/InitializeCore',
148-
replacementSource: 'react-native/setup-env',
149-
},
150-
},
151-
],
152-
output: "require('react-native/setup-env');",
153-
},
154128
],
155129
});

‎packages/eslint-plugin-react-native/no-deep-imports.js‎

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ module.exports = {
2121
messages: {
2222
deepImport:
2323
"'{{importPath}}' React Native deep imports are deprecated. Please use the top level import instead.",
24-
useReplacementSource:
25-
"'{{importPath}}' is deprecated. Please import '{{replacementSource}}' instead.",
2624
},
2725
schema: [],
2826
fixable: 'code',
@@ -33,14 +31,12 @@ module.exports = {
3331
ImportDeclaration(node) {
3432
if (
3533
!isDeepReactNativeImport(node.source) ||
34+
isInitializeCoreImport(node.source) ||
3635
isSecondaryEntryPoint(node.source) ||
3736
isFbInternalImport(node.source)
3837
) {
3938
return;
4039
}
41-
if (reportReplacementSource(node.source)) {
42-
return;
43-
}
4440
if (isDefaultImport(node)) {
4541
const reactNativeSource = node.source.value.slice(
4642
'react-native/'.length,
@@ -92,16 +88,13 @@ module.exports = {
9288
CallExpression(node) {
9389
if (
9490
!isDeepRequire(node) ||
91+
isInitializeCoreImport(node.arguments[0]) ||
9592
isSecondaryEntryPoint(node.arguments[0]) ||
9693
isFbInternalImport(node.arguments[0])
9794
) {
9895
return;
9996
}
10097

101-
if (reportReplacementSource(node.arguments[0])) {
102-
return;
103-
}
104-
10598
const parent = node.parent;
10699
const importPath = node.arguments[0].value;
107100

@@ -130,26 +123,6 @@ module.exports = {
130123
},
131124
};
132125

133-
function reportReplacementSource(source) {
134-
const reactNativeSource = source.value.slice('react-native/'.length);
135-
const mapping = publicAPIMapping[reactNativeSource];
136-
if (!mapping || !mapping.replacementSource) {
137-
return false;
138-
}
139-
context.report({
140-
node: source,
141-
messageId: 'useReplacementSource',
142-
data: {
143-
importPath: source.value,
144-
replacementSource: mapping.replacementSource,
145-
},
146-
fix(fixer) {
147-
return fixer.replaceText(source, `'${mapping.replacementSource}'`);
148-
},
149-
});
150-
return true;
151-
}
152-
153126
function getStandardReport(source) {
154127
return {
155128
node: source,
@@ -194,15 +167,20 @@ module.exports = {
194167
return parts.length > 1 && parts[0] === 'react-native';
195168
}
196169

170+
function isInitializeCoreImport(source) {
171+
if (source.type !== 'Literal' || typeof source.value !== 'string') {
172+
return false;
173+
}
174+
175+
return source.value === 'react-native/Libraries/Core/InitializeCore';
176+
}
177+
197178
function isSecondaryEntryPoint(source) {
198179
if (source.type !== 'Literal' || typeof source.value !== 'string') {
199180
return false;
200181
}
201182

202-
return (
203-
source.value === 'react-native/asset-registry' ||
204-
source.value === 'react-native/setup-env'
205-
);
183+
return source.value === 'react-native/asset-registry';
206184
}
207185

208186
function isFbInternalImport(source) {

‎packages/eslint-plugin-react-native/utils.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ const publicAPIMapping = {
3737
default: 'experimental_LayoutConformance',
3838
types: ['LayoutConformanceProps'],
3939
},
40-
'Libraries/Core/InitializeCore': {
41-
// `InitializeCore` has no public named export; the deep import must be
42-
// swapped for the `react-native/setup-env` entry point entirely.
43-
default: null,
44-
types: null,
45-
replacementSource: 'react-native/setup-env',
46-
},
4740
'Libraries/Lists/FlatList': {
4841
default: 'FlatList',
4942
types: ['FlatListProps'],

‎packages/jest-preset/jest-preset.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ module.exports = {
1818
platforms: ['android', 'ios', 'native'],
1919
},
2020
moduleNameMapper: {
21-
// `setup-env` is a secondary entry point exposed via the package's
22-
// `exports`, but `./jest/resolver.js` strips `exports` and the generic
23-
// mapper below resolves subpaths as literal directory paths. Alias it
24-
// explicitly so it resolves to its `src/` implementation.
25-
'^react-native/setup-env$': `${path.dirname(require.resolve('react-native'))}/src/setup-env.js`,
2621
'^react-native($|/.*)': `${path.dirname(require.resolve('react-native'))}/$1`,
2722
},
2823
resolver: require.resolve('./jest/resolver.js'),

‎packages/jest-preset/jest/setup.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ mock(
133133
'm#react-native/Libraries/Core/InitializeCore',
134134
'm#./mocks/InitializeCore',
135135
);
136-
mock('m#react-native/setup-env', 'm#./mocks/InitializeCore');
137136
mock('m#react-native/Libraries/Core/NativeExceptionsManager');
138137
mock('m#react-native/Libraries/Image/Image', 'm#./mocks/Image');
139138
mock(

‎packages/metro-config/src/index.flow.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function getDefaultConfig(projectRoot: string): ConfigT {
6161
serializer: {
6262
// Note: This option is overridden in cli-plugin-metro (getOverrideConfig)
6363
getModulesRunBeforeMainModule: () => [
64-
require.resolve('react-native/setup-env'),
64+
require.resolve('react-native/Libraries/Core/InitializeCore'),
6565
],
6666
getPolyfills: () => require('@react-native/js-polyfills')(),
6767
isThirdPartyModule({path: modulePath}: Readonly<{path: string, ...}>) {

‎packages/react-native-babel-preset/src/__tests__/plugin-warn-on-deep-imports-test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,17 @@ test('import from other package', () => {
8282
`"import { foo } from 'react-native-foo';"`,
8383
);
8484
});
85+
86+
test('import react-native/Libraries/Core/InitializeCore', () => {
87+
const code = `
88+
import 'react-native/Libraries/Core/InitializeCore';
89+
require('react-native/Libraries/Core/InitializeCore');
90+
export * from 'react-native/Libraries/Core/InitializeCore';
91+
`;
92+
93+
expect(transform(code, [rnDeepImportsWarningPlugin])).toMatchInlineSnapshot(`
94+
"import 'react-native/Libraries/Core/InitializeCore';
95+
require('react-native/Libraries/Core/InitializeCore');
96+
export * from 'react-native/Libraries/Core/InitializeCore';"
97+
`);
98+
});

‎packages/react-native-babel-preset/src/plugin-warn-on-deep-imports.js‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function isDeepReactNativeImport(source) {
3838
return parts.length > 1 && parts[0] === 'react-native';
3939
}
4040

41+
function isInitializeCoreImport(source) {
42+
return source === 'react-native/Libraries/Core/InitializeCore';
43+
}
44+
4145
function withLocation(node, loc) {
4246
if (!node.loc) {
4347
return {...node, loc};
@@ -51,7 +55,7 @@ module.exports = ({types: t}) => ({
5155
ImportDeclaration(path, state) {
5256
const source = path.node.source.value;
5357

54-
if (isDeepReactNativeImport(source)) {
58+
if (isDeepReactNativeImport(source) && !isInitializeCoreImport(source)) {
5559
const loc = path.node.loc;
5660
state.import.push({source, loc});
5761
}
@@ -67,7 +71,10 @@ module.exports = ({types: t}) => ({
6771
) {
6872
const source =
6973
args[0].node.type === 'StringLiteral' ? args[0].node.value : '';
70-
if (isDeepReactNativeImport(source)) {
74+
if (
75+
isDeepReactNativeImport(source) &&
76+
!isInitializeCoreImport(source)
77+
) {
7178
const loc = path.node.loc;
7279
state.require.push({source, loc});
7380
}
@@ -76,7 +83,11 @@ module.exports = ({types: t}) => ({
7683
ExportNamedDeclaration(path, state) {
7784
const source = path.node.source;
7885

79-
if (source && isDeepReactNativeImport(source.value)) {
86+
if (
87+
source &&
88+
isDeepReactNativeImport(source.value) &&
89+
!isInitializeCoreImport(source)
90+
) {
8091
const loc = path.node.loc;
8192
state.export.push({source: source.value, loc});
8293
}

0 commit comments

Comments
 (0)