From b9c4095e40faed0f3ea06f1981b9a53c54a08291 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 24 Mar 2025 20:12:52 +0000 Subject: [PATCH] community-cli-plugin: resolve cli-server-api via peer dependency on cli (#50095) --- packages/community-cli-plugin/package.json | 4 +-- .../src/commands/start/middleware.js | 25 +++++++++++++++--- packages/react-native/react-native.config.js | 26 ++++--------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/packages/community-cli-plugin/package.json b/packages/community-cli-plugin/package.json index d37692d9beb..9fb67e767f5 100644 --- a/packages/community-cli-plugin/package.json +++ b/packages/community-cli-plugin/package.json @@ -38,10 +38,10 @@ "metro-resolver": "^0.81.0" }, "peerDependencies": { - "@react-native-community/cli-server-api": "*" + "@react-native-community/cli": "*" }, "peerDependenciesMeta": { - "@react-native-community/cli-server-api": { + "@react-native-community/cli": { "optional": true } }, diff --git a/packages/community-cli-plugin/src/commands/start/middleware.js b/packages/community-cli-plugin/src/commands/start/middleware.js index bd2235c3c7f..3a28911c07b 100644 --- a/packages/community-cli-plugin/src/commands/start/middleware.js +++ b/packages/community-cli-plugin/src/commands/start/middleware.js @@ -12,6 +12,8 @@ import type {NextHandleFunction, Server} from 'connect'; import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter'; +import typeof * as CLIServerAPI from '@react-native-community/cli-server-api'; + import {logger} from '../../utils/logger'; type MiddlewareReturn = { @@ -65,11 +67,26 @@ const communityMiddlewareFallback = { // Attempt to use the community middleware if it exists, but fallback to // the stubs if it doesn't. try { - const community = require('@react-native-community/cli-server-api'); - communityMiddlewareFallback.indexPageMiddleware = - community.indexPageMiddleware; + // `@react-native-community/cli` is an optional peer dependency of this + // package, and should be a dev dependency of the host project (via the + // community template's package.json). + const communityCliPath = require.resolve('@react-native-community/cli'); + + // `@react-native-community/cli-server-api` is a dependency of + // `@react-native-community/cli`, but is not re-exported by it, so we need + // to resolve the former through the latter. + const communityCliServerApiPath = require.resolve( + '@react-native-community/cli-server-api', + {paths: [communityCliPath]}, + ); + // $FlowIgnore[unsupported-syntax] dynamic import + const communityCliServerApi: CLIServerAPI = require( + communityCliServerApiPath, + ); communityMiddlewareFallback.createDevServerMiddleware = - community.createDevServerMiddleware; + communityCliServerApi.createDevServerMiddleware; + communityMiddlewareFallback.indexPageMiddleware = + communityCliServerApi.indexPageMiddleware; } catch { logger.debug(`⚠️ Unable to find @react-native-community/cli-server-api Starting the server without the community middleware.`); diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 5c17fd5c887..55f8ac17e47 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -61,27 +61,11 @@ try { const commands = []; -try { - const { - bundleCommand, - startCommand, - } = require('@react-native/community-cli-plugin'); - commands.push(bundleCommand, startCommand); -} catch (e) { - const known = - e.code === 'MODULE_NOT_FOUND' && - e.message.includes('@react-native-community/cli-server-api'); - - if (!known) { - throw e; - } - - if (verbose) { - console.warn( - '@react-native-community/cli-server-api not found, the react-native.config.js may be unusable.', - ); - } -} +const { + bundleCommand, + startCommand, +} = require('@react-native/community-cli-plugin'); +commands.push(bundleCommand, startCommand); const codegenCommand = { name: 'codegen',