Skip to content

Commit 6088cb0

Browse files
authored
Ensure InitializeCore is run before app code (#4814)
* Ensure InitializeCore is run before app code * Change files
1 parent 0458337 commit 6088cb0

File tree

10 files changed

+164
-24
lines changed

10 files changed

+164
-24
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Ensure InitializeCore is run before app code",
4+
"packageName": "@office-iss/react-native-win32",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch",
7+
"date": "2020-05-06T21:33:58.938Z"
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Ensure InitializeCore is run before app code",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch",
7+
"date": "2020-05-06T21:34:00.993Z"
8+
}

packages/E2ETest/metro.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
const path = require('path');
88
const blacklist = require('metro-config/src/defaults/blacklist');
9+
const {
10+
getModulesRunBeforeMainModule,
11+
reactNativePlatformResolver,
12+
} = require('react-native-windows/metro-react-native-platform');
913

1014
const rnwPath = path.resolve(__dirname, '../../vnext');
1115

@@ -19,9 +23,9 @@ module.exports = {
1923
],
2024

2125
resolver: {
22-
resolveRequest: require('react-native-windows/metro-react-native-platform').reactNativePlatformResolver(
23-
{ windows: 'react-native-windows' }
24-
),
26+
resolveRequest: reactNativePlatformResolver({
27+
windows: 'react-native-windows',
28+
}),
2529
extraNodeModules: {
2630
// Redirect metro to rnwPath instead of node_modules/react-native-windows, since metro doesn't like symlinks
2731
'react-native-windows': rnwPath,
@@ -34,6 +38,9 @@ module.exports = {
3438
),
3539
]),
3640
},
41+
serializer: {
42+
getModulesRunBeforeMainModule,
43+
},
3744
transformer: {
3845
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
3946
// Hopefully we can fix the default in the future

packages/microsoft-reactnative-sampleapps/metro.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
const path = require('path');
88
const blacklist = require('metro-config/src/defaults/blacklist');
9+
const {
10+
getModulesRunBeforeMainModule,
11+
reactNativePlatformResolver,
12+
} = require('react-native-windows/metro-react-native-platform');
913

1014
const rnwPath = path.resolve(__dirname, '../../vnext');
1115

@@ -19,9 +23,9 @@ module.exports = {
1923
],
2024

2125
resolver: {
22-
resolveRequest: require('react-native-windows/metro-react-native-platform').reactNativePlatformResolver(
23-
{windows: 'react-native-windows'},
24-
),
26+
resolveRequest: reactNativePlatformResolver({
27+
windows: 'react-native-windows',
28+
}),
2529
extraNodeModules: {
2630
// Redirect metro to rnwPath instead of node_modules/react-native-windows, since metro doesn't like symlinks
2731
'react-native-windows': rnwPath,
@@ -36,6 +40,9 @@ module.exports = {
3640
),
3741
]),
3842
},
43+
serializer: {
44+
getModulesRunBeforeMainModule,
45+
},
3946
transformer: {
4047
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
4148
// Hopefully we can fix the default in the future

packages/playground/metro.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
const fs = require('fs');
88
const path = require('path');
99
const blacklist = require('metro-config/src/defaults/blacklist');
10+
const {
11+
getModulesRunBeforeMainModule,
12+
reactNativePlatformResolver,
13+
} = require('react-native-windows/metro-react-native-platform');
1014

1115
const rnwPath = fs.realpathSync(
1216
path.resolve(require.resolve('react-native-windows/package.json'), '..'),
@@ -22,9 +26,9 @@ module.exports = {
2226
],
2327

2428
resolver: {
25-
resolveRequest: require('react-native-windows/metro-react-native-platform').reactNativePlatformResolver(
26-
{windows: 'react-native-windows'},
27-
),
29+
resolveRequest: reactNativePlatformResolver({
30+
windows: 'react-native-windows',
31+
}),
2832
extraNodeModules: {
2933
// Redirect react-native-windows to avoid symlink (metro doesn't like symlinks)
3034
'react-native-windows': rnwPath,
@@ -36,6 +40,9 @@ module.exports = {
3640
),
3741
]),
3842
},
43+
serializer: {
44+
getModulesRunBeforeMainModule,
45+
},
3946
transformer: {
4047
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
4148
// Hopefully we can fix the default in the future

packages/react-native-win32/metro-react-native-platform.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,49 @@ function reactNativePlatformResolver(platformImplementations) {
3838
};
3939
}
4040

41-
module.exports = {reactNativePlatformResolver};
41+
/**
42+
* The CLI will get a more complete implementation of this in https://github.com/react-native-community/cli/pull/1115
43+
* but until then, use a solution that supports having react-native-win32 and/or react-native-windows and/or react-native-macos
44+
*/
45+
const getModulesRunBeforeMainModule = () => {
46+
const options = {
47+
paths: [process.cwd()],
48+
};
49+
const modules = [
50+
require.resolve('react-native/Libraries/Core/InitializeCore', options),
51+
];
52+
53+
try {
54+
modules.push(
55+
require.resolve(
56+
'@office-iss/react-native-win32/Libraries/Core/InitializeCore',
57+
options,
58+
),
59+
);
60+
} catch {}
61+
62+
try {
63+
modules.push(
64+
require.resolve(
65+
'react-native-windows/Libraries/Core/InitializeCore',
66+
options,
67+
),
68+
);
69+
} catch {}
70+
71+
try {
72+
modules.push(
73+
require.resolve(
74+
'react-native-macos/Libraries/Core/InitializeCore',
75+
options,
76+
),
77+
);
78+
} catch {}
79+
80+
return modules;
81+
};
82+
83+
module.exports = {
84+
getModulesRunBeforeMainModule,
85+
reactNativePlatformResolver,
86+
};

packages/react-native-win32/metro.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*/
44
const fs = require('fs');
55
const path = require('path');
6+
const {
7+
getModulesRunBeforeMainModule,
8+
reactNativePlatformResolver,
9+
} = require('./metro-react-native-platform');
610

711
module.exports = {
812
// WatchFolders is only needed due to the yarn workspace layout of node_modules, we need to watch the symlinked locations separately
@@ -12,9 +16,12 @@ module.exports = {
1216
],
1317

1418
resolver: {
15-
resolveRequest: require('./metro-react-native-platform').reactNativePlatformResolver(
16-
{win32: '@office-iss/react-native-win32'},
17-
),
19+
resolveRequest: reactNativePlatformResolver({
20+
win32: '@office-iss/react-native-win32',
21+
}),
22+
},
23+
serializer: {
24+
getModulesRunBeforeMainModule,
1825
},
1926
transformer: {
2027
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above

vnext/local-cli/generator-windows/templates/metro.config.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
const path = require('path');
88
const blacklist = require('metro-config/src/defaults/blacklist');
99

10+
const {
11+
getModulesRunBeforeMainModule,
12+
reactNativePlatformResolver,
13+
} = require('react-native-windows/metro-react-native-platform');
14+
1015
module.exports = {
1116
resolver: {
12-
resolveRequest: require('react-native-windows/metro-react-native-platform').reactNativePlatformResolver(
13-
{windows: 'react-native-windows'},
14-
),
17+
resolveRequest: reactNativePlatformResolver({
18+
windows: 'react-native-windows',
19+
}),
1520
blacklistRE: blacklist([
1621
// This stops "react-native run-windows" from causing the metro server to crash if its already running
1722
new RegExp(
@@ -25,6 +30,9 @@ module.exports = {
2530
),
2631
]),
2732
},
33+
serializer: {
34+
getModulesRunBeforeMainModule,
35+
},
2836
transformer: {
2937
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
3038
// Hopefully we can fix the default in the future

vnext/metro-react-native-platform.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,40 @@ function reactNativePlatformResolver(platformImplementations) {
3838
};
3939
}
4040

41-
module.exports = {reactNativePlatformResolver};
41+
/**
42+
* The CLI will get a more complete implementation of this in https://github.com/react-native-community/cli/pull/1115
43+
* but until then, use a solution that supports having react-native-windows and/or react-native-macos
44+
*/
45+
const getModulesRunBeforeMainModule = () => {
46+
const options = {
47+
paths: [process.cwd()],
48+
};
49+
const modules = [
50+
require.resolve('react-native/Libraries/Core/InitializeCore', options),
51+
];
52+
53+
try {
54+
modules.push(
55+
require.resolve(
56+
'react-native-windows/Libraries/Core/InitializeCore',
57+
options,
58+
),
59+
);
60+
} catch {}
61+
62+
try {
63+
modules.push(
64+
require.resolve(
65+
'react-native-macos/Libraries/Core/InitializeCore',
66+
options,
67+
),
68+
);
69+
} catch {}
70+
71+
return modules;
72+
};
73+
74+
module.exports = {
75+
getModulesRunBeforeMainModule,
76+
reactNativePlatformResolver,
77+
};

vnext/metro.config.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
*/
44
const fs = require('fs');
55
const path = require('path');
6-
const blacklist = require('metro-config/src/defaults/blacklist');
6+
7+
const {
8+
getModulesRunBeforeMainModule,
9+
reactNativePlatformResolver,
10+
} = require('react-native-windows/metro-react-native-platform');
711

812
const rnwPath = __dirname;
913

@@ -15,17 +19,20 @@ module.exports = {
1519
],
1620

1721
resolver: {
18-
resolveRequest: require('./metro-react-native-platform').reactNativePlatformResolver(
19-
{
20-
windesktop: 'react-native-windows',
21-
windows: 'react-native-windows',
22-
},
23-
),
22+
resolveRequest: reactNativePlatformResolver({
23+
windesktop: 'react-native-windows',
24+
windows: 'react-native-windows',
25+
}),
2426
extraNodeModules: {
2527
// Redirect react-native-windows to this folder
2628
'react-native-windows': rnwPath,
2729
},
2830
},
31+
32+
serializer: {
33+
getModulesRunBeforeMainModule,
34+
},
35+
2936
transformer: {
3037
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
3138
// Hopefully we can fix the default in the future

0 commit comments

Comments
 (0)