Skip to content

Commit 2b2bd21

Browse files
authored
fix: Drop @react-native/metro-config dep for RN 0.72 config fallback (#1901)
1 parent f599671 commit 2b2bd21

File tree

4 files changed

+92
-19
lines changed

4 files changed

+92
-19
lines changed

packages/cli-plugin-metro/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
"dependencies": {
1010
"@react-native-community/cli-server-api": "11.1.1",
1111
"@react-native-community/cli-tools": "11.1.1",
12-
"@react-native/metro-config": "^0.72.1",
1312
"chalk": "^4.1.2",
1413
"execa": "^5.0.0",
1514
"metro": "0.76.0",
1615
"metro-config": "0.76.0",
1716
"metro-core": "0.76.0",
17+
"metro-react-native-babel-transformer": "0.76.0",
1818
"metro-resolver": "0.76.0",
19+
"metro-runtime": "0.76.0",
1920
"readline": "^1.3.0"
2021
},
2122
"devDependencies": {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type {InputConfigT} from 'metro-config';
2+
import path from 'path';
3+
import type {ConfigLoadingContext} from './loadMetroConfig';
4+
5+
/**
6+
* This module reproduces defaults from the @react-native/metro-config package,
7+
* used in ./loadMetroConfig.js to provide a soft upgrade process when upgrading
8+
* to React Native 0.72.
9+
*
10+
* These values will be used when:
11+
* - RN CLI 11.x or greater is present in a project (from React Native 0.72).
12+
* - The project has not yet followed the upgrade instructions to update
13+
* metro.config.js to extend '@react-native/metro-config'.
14+
*
15+
* Until we remove this file in a future release, updates should be made both
16+
* here and in '@react-native/metro-config'. (Note: Updates to these values are
17+
* generally rare.)
18+
*
19+
* TODO(@huntie): Remove this file in a future React Native release.
20+
*/
21+
22+
/**
23+
* @deprecated (React Native 0.72.0) Defaults should be updated here and in
24+
* https://github.com/facebook/react-native/tree/main/package/metro-config/index.js
25+
*/
26+
const INTERNAL_CALLSITES_REGEX = new RegExp(
27+
[
28+
'/Libraries/Renderer/implementations/.+\\.js$',
29+
'/Libraries/BatchedBridge/MessageQueue\\.js$',
30+
'/Libraries/YellowBox/.+\\.js$',
31+
'/Libraries/LogBox/.+\\.js$',
32+
'/Libraries/Core/Timers/.+\\.js$',
33+
'/Libraries/WebSocket/.+\\.js$',
34+
'/Libraries/vendor/.+\\.js$',
35+
'/node_modules/react-devtools-core/.+\\.js$',
36+
'/node_modules/react-refresh/.+\\.js$',
37+
'/node_modules/scheduler/.+\\.js$',
38+
'/node_modules/event-target-shim/.+\\.js$',
39+
'/node_modules/invariant/.+\\.js$',
40+
'/node_modules/react-native/index.js$',
41+
'/metro-runtime/.+\\.js$',
42+
'^\\[native code\\]$',
43+
].join('|'),
44+
);
45+
46+
/**
47+
* Get the static Metro config defaults for a React Native project.
48+
*
49+
* @deprecated (React Native 0.72.0) Defaults should be updated here and in
50+
* https://github.com/facebook/react-native/tree/main/package/metro-config/index.js
51+
*/
52+
export default function getDefaultMetroConfig(
53+
ctx: ConfigLoadingContext,
54+
): InputConfigT {
55+
return {
56+
resolver: {
57+
resolverMainFields: ['react-native', 'browser', 'main'],
58+
unstable_conditionNames: ['require', 'react-native'],
59+
},
60+
serializer: {
61+
getPolyfills: () =>
62+
require(path.join(ctx.reactNativePath, 'rn-get-polyfills'))(),
63+
},
64+
server: {
65+
port: Number(process.env.RCT_METRO_PORT) || 8081,
66+
},
67+
symbolicator: {
68+
customizeFrame: (frame: {file?: string}) => {
69+
const collapse = Boolean(
70+
frame.file && INTERNAL_CALLSITES_REGEX.test(frame.file),
71+
);
72+
return {collapse};
73+
},
74+
},
75+
transformer: {
76+
allowOptionalDependencies: true,
77+
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
78+
asyncRequireModulePath: require.resolve(
79+
'metro-runtime/src/modules/asyncRequire',
80+
),
81+
babelTransformerPath: require.resolve(
82+
'metro-react-native-babel-transformer',
83+
),
84+
},
85+
watchFolders: [],
86+
};
87+
}

packages/cli-plugin-metro/src/tools/loadMetroConfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
mergeConfig,
88
resolveConfig,
99
} from 'metro-config';
10-
import {getDefaultConfig} from '@react-native/metro-config';
1110
import {CLIError, logger} from '@react-native-community/cli-tools';
1211
import type {Config} from '@react-native-community/cli-types';
12+
import getDefaultMetroConfig from './getDefaultMetroConfig';
1313
import {reactNativePlatformResolver} from './metroPlatformResolver';
1414

1515
export type {Config};
@@ -114,8 +114,8 @@ export default async function loadMetroConfig(
114114

115115
const loadedConfig = await loadConfig(
116116
{cwd: ctx.root, ...options},
117-
// Provide @react-native/metro-config defaults on top of Metro defaults
118-
getDefaultConfig(ctx.root),
117+
// Provide React Native defaults on top of Metro defaults
118+
getDefaultMetroConfig(ctx),
119119
);
120120

121121
return mergeConfig(loadedConfig, overrideConfig);

yarn.lock

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,21 +2673,6 @@
26732673
resolved "https://registry.yarnpkg.com/@react-native-community/eslint-plugin/-/eslint-plugin-1.1.0.tgz#e42b1bef12d2415411519fd528e64b593b1363dc"
26742674
integrity sha512-W/J0fNYVO01tioHjvYWQ9m6RgndVtbElzYozBq1ZPrHO/iCzlqoySHl4gO/fpCl9QEFjvJfjPgtPMTMlsoq5DQ==
26752675

2676-
"@react-native/js-polyfills@^0.72.1":
2677-
version "0.72.1"
2678-
resolved "https://registry.yarnpkg.com/@react-native/js-polyfills/-/js-polyfills-0.72.1.tgz#905343ef0c51256f128256330fccbdb35b922291"
2679-
integrity sha512-cRPZh2rBswFnGt5X5EUEPs0r+pAsXxYsifv/fgy9ZLQokuT52bPH+9xjDR+7TafRua5CttGW83wP4TntRcWNDA==
2680-
2681-
"@react-native/metro-config@^0.72.1":
2682-
version "0.72.1"
2683-
resolved "https://registry.yarnpkg.com/@react-native/metro-config/-/metro-config-0.72.1.tgz#57f212700db2d160e8beff6163558310c2c82220"
2684-
integrity sha512-BxGfuMK/cXwJxChE4/T6nE4qOdwGLM9iUFFKpcyh9Nks0702qTTvwpjkFbJvlkpe4yulAZh8CKLJrRDVzGGbfQ==
2685-
dependencies:
2686-
"@react-native/js-polyfills" "^0.72.1"
2687-
metro-config "0.76.0"
2688-
metro-react-native-babel-transformer "0.76.0"
2689-
metro-runtime "0.76.0"
2690-
26912676
"@sinonjs/commons@^1.7.0":
26922677
version "1.7.1"
26932678
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.7.1.tgz#da5fd19a5f71177a53778073978873964f49acf1"

0 commit comments

Comments
 (0)