Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add runtime error when Metro config cannot be resolved #1889

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions packages/cli-plugin-metro/src/tools/loadMetroConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import {ConfigT, InputConfigT, loadConfig} from 'metro-config';
import {ConfigT, InputConfigT, loadConfig, resolveConfig} from 'metro-config';
import {CLIError, logger} from '@react-native-community/cli-tools';
import type {Config} from '@react-native-community/cli-types';
import {reactNativePlatformResolver} from './metroPlatformResolver';

Expand Down Expand Up @@ -69,13 +70,36 @@ export interface ConfigOptionsT {
* Allows the CLI to override certain defaults in the base `metro.config.js`
* based on dynamic user options in `ctx`.
*/
export default function loadMetroConfig(
export default async function loadMetroConfig(
ctx: ConfigLoadingContext,
options?: ConfigOptionsT,
options: ConfigOptionsT = {},
): Promise<ConfigT> {
const overrideConfig = getOverrideConfig(ctx);
if (options && options.reporter) {
if (options.reporter) {
overrideConfig.reporter = options.reporter;
}

const projectConfig = await resolveConfig(undefined, ctx.root);
huntie marked this conversation as resolved.
Show resolved Hide resolved

// @ts-ignore resolveConfig return value is mistyped
if (projectConfig.isEmpty) {
throw new CLIError(`No metro config found in ${ctx.root}`);
}

// @ts-ignore resolveConfig return value is mistyped
logger.debug(`Reading Metro config from ${projectConfig.filepath}`);

try {
require.resolve('@react-native/metro-config', {
paths: [ctx.root],
});
} catch (e) {
console.warn(
'warning: From React Native 0.72, your metro.config.js file should ' +
thymikee marked this conversation as resolved.
Show resolved Hide resolved
"extend '@react-native/metro-config', however it's not present in your " +
"project's devDependencies. Please install '@react-native/metro-config'.",
);
}
huntie marked this conversation as resolved.
Show resolved Hide resolved

return loadConfig({cwd: ctx.root, ...options}, overrideConfig);
}