Skip to content

Commit

Permalink
fix: Add runtime error when Metro config cannot be resolved (#1889)
Browse files Browse the repository at this point in the history
* fix: Add runtime error when Metro config cannot be resolved

* Apply suggestions from code review

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

---------

Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
  • Loading branch information
huntie and thymikee authored Mar 29, 2023
1 parent 54871b2 commit e29c2e9
Showing 1 changed file with 28 additions and 4 deletions.
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);

// @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) {
logger.warn(
"From React Native 0.72, your 'metro.config.js' file should " +
"extend '@react-native/metro-config', however it's not present in your " +
"project's devDependencies. Please install '@react-native/metro-config'.",
);
}

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

0 comments on commit e29c2e9

Please sign in to comment.