mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46769 (Further refactors to logging after D63255296.) Fully decouples `community-cli-plugin` from the unlisted optional dependency on `react-native-community/cli-tools'`. This is motivated by changes in https://github.com/facebook/react-native/pull/46627 which switch to using Metro's `TerminalReporter` API for emitting logs safely. - Swaps out logs in the dev server for the `unstable_server_log` Metro reporter event. - Swaps out `logger.debug()` calls for the `debug` package, currently used by Metro and `dev-middleware`. - Swaps out other logs in the `bundle` command for `console`. - (Also specify missing `semver` dep.) Changelog: [Internal] Reviewed By: hoxyq Differential Revision: D63328268 fbshipit-source-id: f552748ecc3456bd5fb8870c3a51d744a6bf3e70
122 lines
3.5 KiB
JavaScript
122 lines
3.5 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
|
|
* @oncall react_native
|
|
*/
|
|
|
|
import type {Config} from '@react-native-community/cli-types';
|
|
import type {ConfigT, InputConfigT, YargArguments} from 'metro-config';
|
|
|
|
import {CLIError} from './errors';
|
|
import {reactNativePlatformResolver} from './metroPlatformResolver';
|
|
import {loadConfig, mergeConfig, resolveConfig} from 'metro-config';
|
|
import path from 'path';
|
|
|
|
const debug = require('debug')('ReactNative:CommunityCliPlugin');
|
|
|
|
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 getOverrideConfig(
|
|
ctx: ConfigLoadingContext,
|
|
config: ConfigT,
|
|
): InputConfigT {
|
|
const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
|
|
platform => ctx.platforms[platform].npmPackageName,
|
|
);
|
|
const resolver: Partial<{...ConfigT['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: YargArguments = {},
|
|
): Promise<ConfigT> {
|
|
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);
|
|
}
|
|
}
|
|
|
|
const config = await loadConfig({
|
|
cwd,
|
|
...options,
|
|
});
|
|
|
|
const overrideConfig = getOverrideConfig(ctx, config);
|
|
|
|
return mergeConfig(config, overrideConfig);
|
|
}
|