Skip to content

Commit f5989cc

Browse files
committed
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 665212e commit f5989cc

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

packages/community-cli-plugin/package.json

Lines changed: 2 additions & 2 deletions
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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @oncall react_native
1010
*/
1111

12+
import typeof * as CLIServerAPI from '@react-native-community/cli-server-api';
1213
import type {Server} from 'connect';
1314
import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter';
1415

@@ -66,9 +67,25 @@ const communityMiddlewareFallback = {
6667
// Attempt to use the community middleware if it exists, but fallback to
6768
// the stubs if it doesn't.
6869
try {
69-
const community = require('@react-native-community/cli-server-api');
70+
// `@react-native-community/cli` is an optional peer dependency of this
71+
// package, and should be a dev dependency of the host project (via the
72+
// community template's package.json).
73+
const communityCliPath = require.resolve('@react-native-community/cli');
74+
75+
// `@react-native-community/cli-server-api` is a dependency of
76+
// `@react-native-community/cli`, but is not re-exported by it, so we need
77+
// to resolve the former through the latter.
78+
const communityCliServerApiPath = require.resolve(
79+
'@react-native-community/cli-server-api',
80+
{paths: [communityCliPath]},
81+
);
82+
// $FlowIgnore[unsupported-syntax] dynamic import
83+
const communityCliServerApi: CLIServerAPI = require(
84+
communityCliServerApiPath,
85+
);
86+
// $FlowIgnore[unsupported-syntax] dynamic import
7087
communityMiddlewareFallback.createDevServerMiddleware =
71-
community.createDevServerMiddleware;
88+
communityCliServerApi.createDevServerMiddleware;
7289
} catch {
7390
debug(`⚠️ Unable to find @react-native-community/cli-server-api
7491
Starting the server without the community middleware.`);

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,11 @@ try {
4444

4545
const commands = [];
4646

47-
try {
48-
const {
49-
bundleCommand,
50-
startCommand,
51-
} = require('@react-native/community-cli-plugin');
52-
commands.push(bundleCommand, startCommand);
53-
} catch (e) {
54-
const known =
55-
e.code === 'MODULE_NOT_FOUND' &&
56-
e.message.includes('@react-native-community/cli-server-api');
57-
58-
if (!known) {
59-
throw e;
60-
}
61-
62-
if (verbose) {
63-
console.warn(
64-
'@react-native-community/cli-server-api not found, the react-native.config.js may be unusable.',
65-
);
66-
}
67-
}
47+
const {
48+
bundleCommand,
49+
startCommand,
50+
} = require('@react-native/community-cli-plugin');
51+
commands.push(bundleCommand, startCommand);
6852

6953
const codegenCommand = {
7054
name: 'codegen',

0 commit comments

Comments
 (0)