Files
react-native/packages/community-cli-plugin/src/utils/loadMetroConfig.js
T
Rob Hogan 014fe8b520 Refactor imports from Metro to avoid deprecated styles and prefer main package (#57378)
Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57378

Refactor Metro imports to:
 - Remove use of deprecated default object export, prefer named/namespace exports.
 - Prefer imports from `metro`, remove public dependencies on `metro-core` and `metro-config` from `react-native/community-cli-plugin`.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D110177050

fbshipit-source-id: f21a561d40d13f354bc56cdc50d6e6d6843877cc
2026-06-30 09:42:04 -07:00

144 lines
4.4 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {Config} from '@react-native-community/cli-types';
import type {MetroConfig} from 'metro';
import {CLIError} from './errors';
import {reactNativePlatformResolver} from './metroPlatformResolver';
import {loadConfig, resolveConfig} from 'metro';
import path from 'path';
const debug = require('debug')('ReactNative:CommunityCliPlugin');
type HydratedMetroConfig = Awaited<ReturnType<typeof loadConfig>>;
type ArgvInput = Parameters<typeof loadConfig>[0];
export type {Config};
export type ConfigLoadingContext = Readonly<{
root: Config['root'],
reactNativePath: Config['reactNativePath'],
platforms: Config['platforms'],
...
}>;
/**
* Get the config options to override based on RN CLI inputs.
*/
function getCommunityCliDefaultConfig(
ctx: ConfigLoadingContext,
config: HydratedMetroConfig,
): MetroConfig {
const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
platform => ctx.platforms[platform].npmPackageName,
);
const resolver: Partial<{...HydratedMetroConfig['resolver']}> = {
platforms: [...Object.keys(ctx.platforms), 'native'],
};
if (outOfTreePlatforms.length) {
resolver.resolveRequest = reactNativePlatformResolver(
outOfTreePlatforms.reduce<{[platform: string]: string}>(
(result, platform) => {
result[platform] = ctx.platforms[platform].npmPackageName;
return result;
},
{},
),
config.resolver?.resolveRequest,
);
}
return {
resolver,
serializer: {
// We can include multiple copies of InitializeCore here because metro will
// only add ones that are already part of the bundle
getModulesRunBeforeMainModule: () => [
require.resolve(
path.join(ctx.reactNativePath, 'Libraries/Core/InitializeCore'),
{paths: [ctx.root]},
),
...outOfTreePlatforms.map(platform =>
require.resolve(
`${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`,
{paths: [ctx.root]},
),
),
],
},
};
}
/**
* Load Metro config.
*
* Allows the CLI to override select values in `metro.config.js` based on
* dynamic user options in `ctx`.
*/
export default async function loadMetroConfig(
ctx: ConfigLoadingContext,
options: NonNullable<ArgvInput> = {},
): Promise<HydratedMetroConfig> {
let RNMetroConfig = null;
try {
RNMetroConfig = require('@react-native/metro-config');
} catch (e) {
throw new Error(
"Cannot resolve `@react-native/metro-config`. Ensure it is listed in your project's `devDependencies`.",
);
}
// Get the RN defaults before our customisations
const defaultConfig = RNMetroConfig.getDefaultConfig(ctx.root);
// Unflag the config as being loaded - it must be loaded again in userland.
global.__REACT_NATIVE_METRO_CONFIG_LOADED = false;
// Add our defaults to `@react-native/metro-config` before the user config
// loads them.
if (typeof RNMetroConfig.setFrameworkDefaults !== 'function') {
throw new Error(
'`@react-native/metro-config` does not have the expected API. Ensure it matches your React Native version.',
);
}
RNMetroConfig.setFrameworkDefaults(
getCommunityCliDefaultConfig(ctx, defaultConfig),
);
const cwd = ctx.root;
const projectConfig = await resolveConfig(options.config, cwd);
if (projectConfig.isEmpty) {
throw new CLIError(`No Metro config found in ${cwd}`);
}
debug(`Reading Metro config from ${projectConfig.filepath}`);
if (!global.__REACT_NATIVE_METRO_CONFIG_LOADED) {
const warning = `
=================================================================================================
From React Native 0.73, your project's Metro config should extend '@react-native/metro-config'
or it will fail to build. Please copy the template at:
https://github.com/react-native-community/template/blob/main/template/metro.config.js
This warning will be removed in future (https://github.com/facebook/metro/issues/1018).
=================================================================================================
`;
for (const line of warning.trim().split('\n')) {
console.warn(line);
}
}
return loadConfig({
cwd,
...options,
});
}