Skip to content

Commit fe9124e

Browse files
committed
update metro config
1 parent af30904 commit fe9124e

File tree

3 files changed

+106
-53
lines changed

3 files changed

+106
-53
lines changed

vnext/template/metro.config.js

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
2-
1+
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
32
const fs = require('fs');
43
const path = require('path');
4+
const escape = require('escape-string-regexp');
55
const exclusionList = require('metro-config/src/defaults/exclusionList');
6+
const pack = require('../package.json');
7+
8+
const root = path.resolve(__dirname, '..');
9+
const modules = Object.keys({ ...pack.peerDependencies });
610

711
const rnwPath = fs.realpathSync(
812
path.resolve(require.resolve('react-native-windows/package.json'), '..'),
@@ -19,32 +23,60 @@ const rnwPackages = path.resolve(rnwPath, '..', 'packages');
1923
*
2024
* @type {import('metro-config').MetroConfig}
2125
*/
22-
2326
const config = {
24-
//{{#devMode}} [devMode
25-
watchFolders: [rnwPath, rnwRootNodeModules, rnwPackages],
26-
// devMode]{{/devMode}}
27-
resolver: {
28-
blockList: exclusionList([
29-
// This stops "npx @react-native-community/cli run-windows" from causing the metro server to crash if its already running
30-
new RegExp(
31-
`${path.resolve(__dirname, 'windows').replace(/[/\\]/g, '/')}.*`,
32-
),
33-
// This prevents "npx @react-native-community/cli run-windows" from hitting: EBUSY: resource busy or locked, open msbuild.ProjectImports.zip or other files produced by msbuild
34-
new RegExp(`${rnwPath}/build/.*`),
35-
new RegExp(`${rnwPath}/target/.*`),
36-
/.*\.ProjectImports\.zip/,
37-
]),
27+
watchFolders: [
28+
root,
3829
//{{#devMode}} [devMode
39-
extraNodeModules: {
40-
'react-native-windows': rnwPath,
41-
},
30+
rnwPath,
31+
rnwRootNodeModules,
32+
rnwPackages,
4233
// devMode]{{/devMode}}
34+
],
35+
36+
// Ensure only one version of peerDependencies is loaded:
37+
// Block peers in the repo root and alias them to the example's node_modules.
38+
resolver: {
39+
blockList: exclusionList(
40+
modules
41+
.map(
42+
(m) =>
43+
new RegExp(`^${escape(path.join(root, 'node_modules', m))}\\/.*$`),
44+
)
45+
.concat([
46+
// Prevent Metro crash if run-windows starts when Metro is already running
47+
new RegExp(
48+
`${path.resolve(__dirname, 'windows').replace(/[/\\]/g, '/')}.*`,
49+
),
50+
// Avoid EBUSY locks for MSBuild-produced artifacts
51+
new RegExp(`${rnwPath}/build/.*`),
52+
new RegExp(`${rnwPath}/target/.*`),
53+
/.*\.ProjectImports\.zip/,
54+
]),
55+
),
56+
57+
// Prefer explicit platform list so .windows.* sources resolve predictably
58+
platforms: ['ios', 'android', 'native', 'windows'],
59+
60+
// Important for packages publishing via modern Node "exports"
61+
unstable_enablePackageExports: true,
62+
63+
extraNodeModules: modules.reduce(
64+
(acc, name) => {
65+
acc[name] = path.join(__dirname, 'node_modules', name);
66+
return acc;
67+
},
68+
{
69+
//{{#devMode}} [devMode
70+
'react-native-windows': rnwPath,
71+
// devMode]{{/devMode}}
72+
},
73+
),
4374
},
75+
4476
transformer: {
77+
// Keep inlineRequires; drop experimentalImportSupport (not needed with RN preset)
4578
getTransformOptions: async () => ({
4679
transform: {
47-
experimentalImportSupport: false,
4880
inlineRequires: true,
4981
},
5082
}),

vnext/templates/cpp-app/metro.config.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
23

34
const fs = require('fs');
@@ -26,25 +27,33 @@ const config = {
2627
// devMode]{{/devMode}}
2728
resolver: {
2829
blockList: exclusionList([
29-
// This stops "npx @react-native-community/cli run-windows" from causing the metro server to crash if its already running
30+
// This stops "npx @react-native-community/cli run-windows" from causing the metro server to crash if it's already running
3031
new RegExp(
31-
`${path.resolve(__dirname, 'windows').replace(/[/\\]/g, '/')}.*`,
32+
`${path.resolve(__dirname, 'windows').replace(/[\/\\]/g, '/') }.*`,
3233
),
3334
// This prevents "npx @react-native-community/cli run-windows" from hitting: EBUSY: resource busy or locked, open msbuild.ProjectImports.zip or other files produced by msbuild
3435
new RegExp(`${rnwPath}/build/.*`),
3536
new RegExp(`${rnwPath}/target/.*`),
3637
/.*\.ProjectImports\.zip/,
3738
]),
39+
40+
// Prefer explicit platform list to ensure .windows.* files resolve predictably
41+
platforms: ['ios', 'android', 'native', 'windows'],
42+
43+
// Important for packages that publish using "exports" (RN 0.8x era)
44+
unstable_enablePackageExports: true,
45+
3846
//{{#devMode}} [devMode
3947
extraNodeModules: {
4048
'react-native-windows': rnwPath,
4149
},
4250
// devMode]{{/devMode}}
4351
},
4452
transformer: {
53+
// Keep inlineRequires to improve startup on some apps.
54+
// experimentalImportSupport is not needed when extending the RN preset.
4555
getTransformOptions: async () => ({
4656
transform: {
47-
experimentalImportSupport: false,
4857
inlineRequires: true,
4958
},
5059
}),

vnext/templates/cpp-lib/example/metro.config.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,63 @@ const rnwPackages = path.resolve(rnwPath, '..', 'packages');
2424
* @type {import('metro-config').MetroConfig}
2525
*/
2626
const config = {
27-
watchFolders: [root,
27+
watchFolders: [
28+
root,
2829
//{{#devMode}} [devMode
29-
rnwPath, rnwRootNodeModules, rnwPackages
30+
rnwPath,
31+
rnwRootNodeModules,
32+
rnwPackages,
3033
// devMode]{{/devMode}}
3134
],
3235

33-
// We need to make sure that only one version is loaded for peerDependencies
34-
// So we block them at the root, and alias them to the versions in example's node_modules
36+
// Ensure only one version of peerDependencies is loaded:
37+
// Block peers in the repo root and alias them to the example's node_modules.
3538
resolver: {
36-
blacklistRE: exclusionList(
37-
modules.map(
38-
(m) =>
39-
new RegExp(`^${escape(path.join(root, 'node_modules', m))}\\/.*$`)
40-
).concat([
41-
// This stops "npx @react-native-community/cli run-windows" from causing the metro server to crash if its already running
42-
new RegExp(
43-
`${path.resolve(__dirname, 'windows').replace(/[/\\]/g, '/')}.*`,
44-
),
45-
// This prevents "npx @react-native-community/cli run-windows" from hitting: EBUSY: resource busy or locked, open msbuild.ProjectImports.zip or other files produced by msbuild
46-
new RegExp(`${rnwPath}/build/.*`),
47-
new RegExp(`${rnwPath}/target/.*`),
48-
/.*\.ProjectImports\.zip/,
49-
])
39+
blockList: exclusionList(
40+
modules
41+
.map(
42+
(m) =>
43+
new RegExp(`^${escape(path.join(root, 'node_modules', m))}\\/.*$`),
44+
)
45+
.concat([
46+
// Prevent Metro crash if run-windows starts when Metro is already running
47+
new RegExp(
48+
`${path.resolve(__dirname, 'windows').replace(/[/\\]/g, '/')}.*`,
49+
),
50+
// Avoid EBUSY locks for MSBuild-produced artifacts
51+
new RegExp(`${rnwPath}/build/.*`),
52+
new RegExp(`${rnwPath}/target/.*`),
53+
/.*\.ProjectImports\.zip/,
54+
]),
5055
),
5156

52-
extraNodeModules: modules.reduce((acc, name) => {
53-
acc[name] = path.join(__dirname, 'node_modules', name);
54-
return acc;
55-
},
56-
{
57-
//{{#devMode}} [devMode
58-
'react-native-windows': rnwPath,
59-
// devMode]{{/devMode}}
60-
}
57+
// Prefer explicit platform list so .windows.* sources resolve predictably
58+
platforms: ['ios', 'android', 'native', 'windows'],
59+
60+
// Important for packages publishing via modern Node "exports"
61+
unstable_enablePackageExports: true,
62+
63+
extraNodeModules: modules.reduce(
64+
(acc, name) => {
65+
acc[name] = path.join(__dirname, 'node_modules', name);
66+
return acc;
67+
},
68+
{
69+
//{{#devMode}} [devMode
70+
'react-native-windows': rnwPath,
71+
// devMode]{{/devMode}}
72+
},
6173
),
6274
},
6375

6476
transformer: {
77+
// Keep inlineRequires; drop experimentalImportSupport (not needed with RN preset)
6578
getTransformOptions: async () => ({
6679
transform: {
67-
experimentalImportSupport: false,
6880
inlineRequires: true,
6981
},
7082
}),
7183
},
7284
};
7385

74-
module.exports = mergeConfig(getDefaultConfig(__dirname), config);
86+
module.exports = mergeConfig(getDefaultConfig(__dirname), config);

0 commit comments

Comments
 (0)