Files
react-native/packages/react-native/react-native.config.js
T
Rob Hogan cdaa1aa9aa community-cli-plugin: resolve cli-server-api via peer dependency on cli (#49518)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49518

`react-native/community-cli-plugin` depends on `createDevServerMiddleware` from `react-native-community/cli-server-api`.

`react-native/community-cli-plugin` currently [declares an optional peer dependency](https://github.com/facebook/react-native/blob/bae895500052bda2f55e1832b0c8a63a1b449de3/packages/community-cli-plugin/package.json#L39-L45) on `react-native-community/cli-server-api`, however because the latter isn't a dependency of `react-native` or the community template, the peer dependency is not available to package managers that enforce isolated node_modules - see https://github.com/facebook/react-native/issues/47309.

Rather than add an unnecessary dependency to the template (like [this](https://github.com/react-native-community/template/pull/105)), my proposal is to switch to a peer dependency on only `react-native-community/cli`, because that *is* a dependency of the community template and therefore will be resolvable.

Because `react-native-community/cli` doesn't re-export `createDevServerMiddleware` from its dependency on `cli-server-api`, we need to resolve the latter through the former. This can be cleaned up once a re-export lands - https://github.com/react-native-community/cli/pull/2605.

Changelog:
[GENERAL][FIXED] Fix registering of `start` and `bundle` commands with community CLI and isolated node_modules.

Reviewed By: huntie

Differential Revision: D69848688

fbshipit-source-id: 009b8ffd43b2ab2d84fcc71e9e48382eb8950bb1
2025-02-19 10:11:03 -08:00

127 lines
3.3 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.
*
* @format
*/
'use strict';
// React Native shouldn't be exporting itself like this, the Community Template should be be directly
// depending on and injecting:
// - @react-native-community/cli-platform-android
// - @react-native-community/cli-platform-ios
// - @react-native/community-cli-plugin (via the @react-native/core-cli-utils package)
// - codegen command should be inhoused into @react-native-community/cli
//
// This is a temporary workaround.
const verbose = process.env.DEBUG && process.env.DEBUG.includes('react-native');
function findCommunityPlatformPackage(spec, startDir = process.cwd()) {
// In monorepos, we cannot make any assumptions on where
// `@react-native-community/*` gets installed. The safest way to find it
// (barring adding an optional peer dependency) is to start from the project
// root.
//
// Note that we're assuming that the current working directory is the project
// root. This is also what `@react-native-community/cli` assumes (see
// https://github.com/react-native-community/cli/blob/14.x/packages/cli-tools/src/findProjectRoot.ts).
const main = require.resolve(spec, {paths: [startDir]});
return require(main);
}
let android;
try {
android = findCommunityPlatformPackage(
'@react-native-community/cli-platform-android',
);
} catch {
if (verbose) {
console.warn(
'@react-native-community/cli-platform-android not found, the react-native.config.js may be unusable.',
);
}
}
let ios;
try {
ios = findCommunityPlatformPackage(
'@react-native-community/cli-platform-ios',
);
} catch {
if (verbose) {
console.warn(
'@react-native-community/cli-platform-ios not found, the react-native.config.js may be unusable.',
);
}
}
const commands = [];
const {
bundleCommand,
startCommand,
} = require('@react-native/community-cli-plugin');
commands.push(bundleCommand, startCommand);
const codegenCommand = {
name: 'codegen',
options: [
{
name: '--path <path>',
description: 'Path to the React Native project root.',
default: process.cwd(),
},
{
name: '--platform <string>',
description:
'Target platform. Supported values: "android", "ios", "all".',
default: 'all',
},
{
name: '--outputPath <path>',
description: 'Path where generated artifacts will be output to.',
},
{
name: '--source <string>',
description: 'Whether the script is invoked from an `app` or a `library`',
default: 'app',
},
],
func: (argv, config, args) =>
require('./scripts/codegen/generate-artifacts-executor').execute(
args.path,
args.platform,
args.outputPath,
args.source,
),
};
commands.push(codegenCommand);
const config = {
commands,
platforms: {},
};
if (ios != null) {
config.commands.push(...ios.commands);
config.platforms.ios = {
projectConfig: ios.projectConfig,
dependencyConfig: ios.dependencyConfig,
};
}
if (android != null) {
config.commands.push(...android.commands);
config.platforms.android = {
projectConfig: android.projectConfig,
dependencyConfig: android.dependencyConfig,
};
}
module.exports = config;