Skip to content

Commit cdaa1aa

Browse files
robhoganfacebook-github-bot
authored andcommittedFeb 19, 2025·
community-cli-plugin: resolve cli-server-api via peer dependency on cli (#49518)
Summary: Pull Request resolved: #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 #47309. Rather than add an unnecessary dependency to the template (like [this](react-native-community/template#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 - react-native-community/cli#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
1 parent 0f8ef32 commit cdaa1aa

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed
 

‎packages/community-cli-plugin/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
"metro-resolver": "^0.81.0"
3838
},
3939
"peerDependencies": {
40-
"@react-native-community/cli-server-api": "*"
40+
"@react-native-community/cli": "*"
4141
},
4242
"peerDependenciesMeta": {
43-
"@react-native-community/cli-server-api": {
43+
"@react-native-community/cli": {
4444
"optional": true
4545
}
4646
},

‎packages/community-cli-plugin/src/commands/start/middleware.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import type {Server} from 'connect';
1313
import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter';
1414

15+
import {typeof createDevServerMiddleware as CreateDevServerMiddleware} from '@react-native-community/cli-server-api';
16+
1517
const debug = require('debug')('ReactNative:CommunityCliPlugin');
1618

1719
type MiddlewareReturn = {
@@ -66,9 +68,23 @@ const communityMiddlewareFallback = {
6668
// Attempt to use the community middleware if it exists, but fallback to
6769
// the stubs if it doesn't.
6870
try {
69-
const community = require('@react-native-community/cli-server-api');
70-
communityMiddlewareFallback.createDevServerMiddleware =
71-
community.createDevServerMiddleware;
71+
// `@react-native-community/cli` is an optional peer dependency of this
72+
// package, and should be a dev dependency of the host project (via the
73+
// community template's package.json).
74+
const communityCliPath = require.resolve('@react-native-community/cli');
75+
76+
// Until https://github.com/react-native-community/cli/pull/2605 lands,
77+
// we need to find `@react-native-community/cli-server-api` via
78+
// `@react-native-community/cli`. Once that lands, we can simply
79+
// require('@react-native-community/cli').
80+
const communityCliServerApiPath = require.resolve(
81+
'@react-native-community/cli-server-api',
82+
{paths: [communityCliPath]},
83+
);
84+
// $FlowIgnore[unsupported-syntax] dynamic import
85+
communityMiddlewareFallback.createDevServerMiddleware = require(
86+
communityCliServerApiPath,
87+
).createDevServerMiddleware as CreateDevServerMiddleware;
7288
} catch {
7389
debug(`⚠️ Unable to find @react-native-community/cli-server-api
7490
Starting the server without the community middleware.`);

‎packages/react-native/react-native.config.js

+5-21
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,11 @@ try {
6161

6262
const commands = [];
6363

64-
try {
65-
const {
66-
bundleCommand,
67-
startCommand,
68-
} = require('@react-native/community-cli-plugin');
69-
commands.push(bundleCommand, startCommand);
70-
} catch (e) {
71-
const known =
72-
e.code === 'MODULE_NOT_FOUND' &&
73-
e.message.includes('@react-native-community/cli-server-api');
74-
75-
if (!known) {
76-
throw e;
77-
}
78-
79-
if (verbose) {
80-
console.warn(
81-
'@react-native-community/cli-server-api not found, the react-native.config.js may be unusable.',
82-
);
83-
}
84-
}
64+
const {
65+
bundleCommand,
66+
startCommand,
67+
} = require('@react-native/community-cli-plugin');
68+
commands.push(bundleCommand, startCommand);
8569

8670
const codegenCommand = {
8771
name: 'codegen',

0 commit comments

Comments
 (0)
Please sign in to comment.