Skip to content

Commit e9267dc

Browse files
committed
fix: Allow soft fallback to internal RN Metro defaults
1 parent 6c31bfc commit e9267dc

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

packages/cli-plugin-metro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"@react-native-community/cli-server-api": "^11.0.1",
1111
"@react-native-community/cli-tools": "^11.0.1",
12+
"@react-native/metro-config": "^0.72.1",
1213
"chalk": "^4.1.2",
1314
"execa": "^5.0.0",
1415
"metro": "0.76.0",

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
import fs from 'fs';
12
import path from 'path';
2-
import {ConfigT, InputConfigT, loadConfig, resolveConfig} from 'metro-config';
3+
import {
4+
ConfigT,
5+
InputConfigT,
6+
loadConfig,
7+
mergeConfig,
8+
resolveConfig,
9+
} from 'metro-config';
10+
import {getDefaultConfig} from '@react-native/metro-config';
311
import {CLIError, logger} from '@react-native-community/cli-tools';
412
import type {Config} from '@react-native-community/cli-types';
513
import {reactNativePlatformResolver} from './metroPlatformResolver';
@@ -89,17 +97,29 @@ export default async function loadMetroConfig(
8997
// @ts-ignore resolveConfig return value is mistyped
9098
logger.debug(`Reading Metro config from ${projectConfig.filepath}`);
9199

92-
try {
93-
require.resolve('@react-native/metro-config', {
94-
paths: [ctx.root],
95-
});
96-
} catch (e) {
100+
const loadedProjectConfig = await loadConfig({cwd: ctx.root, ...options});
101+
102+
if (
103+
!/['"']@react-native\/metro-config['"']/.test(
104+
fs
105+
// @ts-ignore resolveConfig return value is mistyped
106+
.readFileSync(projectConfig.filepath, 'utf8'),
107+
)
108+
) {
97109
logger.warn(
98-
"From React Native 0.72, your 'metro.config.js' file should " +
99-
"extend '@react-native/metro-config', however it's not present in your " +
100-
"project's devDependencies. Please install '@react-native/metro-config'.",
110+
'From React Native 0.72, your metro.config.js file should extend' +
111+
"'@react-native/metro-config'. Please see the React Native 0.72 " +
112+
'changelog, or copy the template at:\n' +
113+
'https://github.com/facebook/react-native/blob/main/packages/react-native/template/metro.config.js',
114+
);
115+
logger.warn('Falling back to internal defaults.');
116+
117+
return mergeConfig(
118+
loadedProjectConfig,
119+
getDefaultConfig(ctx.root),
120+
overrideConfig,
101121
);
102122
}
103123

104-
return loadConfig({cwd: ctx.root, ...options}, overrideConfig);
124+
return mergeConfig(loadedProjectConfig, overrideConfig);
105125
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module '@react-native/metro-config' {
2+
import type {ConfigT} from 'metro-config';
3+
4+
export function getDefaultConfig(projectRoot: string): ConfigT;
5+
}

yarn.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,21 @@
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+
26762691
"@sinonjs/commons@^1.7.0":
26772692
version "1.7.1"
26782693
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.7.1.tgz#da5fd19a5f71177a53778073978873964f49acf1"
@@ -8916,6 +8931,19 @@ [email protected]:
89168931
babel-plugin-transform-flow-enums "^0.0.2"
89178932
react-refresh "^0.4.0"
89188933

8934+
8935+
version "0.76.0"
8936+
resolved "https://registry.yarnpkg.com/metro-react-native-babel-transformer/-/metro-react-native-babel-transformer-0.76.0.tgz#8c8872f0d3a0ec9dad2480df53c92c10eac92c79"
8937+
integrity sha512-mLyUiGq2qPoEwV3oncD82HOtM4wAl8YmXtGY17D4iqH6/5pE32lRnDDYt0WnJYACZDs3RB3MhTjGCM7rJNwn/A==
8938+
dependencies:
8939+
"@babel/core" "^7.20.0"
8940+
babel-preset-fbjs "^3.4.0"
8941+
hermes-parser "0.8.0"
8942+
metro-babel-transformer "0.76.0"
8943+
metro-react-native-babel-preset "0.76.0"
8944+
metro-source-map "0.76.0"
8945+
nullthrows "^1.1.1"
8946+
89198947
89208948
version "0.76.0"
89218949
resolved "https://registry.yarnpkg.com/metro-resolver/-/metro-resolver-0.76.0.tgz#3fa778adbab30859023a89e7a1241f4eb68171f2"

0 commit comments

Comments
 (0)