From dad840cb42186b41f31436ad10a6576013beba40 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Tue, 18 Mar 2025 13:18:42 +0000 Subject: [PATCH] community-cli-plugin: resolve cli-server-api via peer dependency on cli --- 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 c4c2b3b3f496..39c7d3e4a8e4 100644 --- a/packages/community-cli-plugin/package.json +++ b/packages/community-cli-plugin/package.json @@ -37,10 +37,10 @@ "metro-resolver": "^0.81.0-alpha.2" }, "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 bd2235c3c7f5..3a28911c07ba 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 3b572bb1e9c6..60fc2b395ae8 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -44,27 +44,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',