Relocate CLI plugin server modules (#57654)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57654

Reorganise these modules under a `src/dev-server/` directory (out of `src/(commands|utils)/`, more clearly identifying this unit of the codebase.

No public API change: the package still exports `startCommand` unchanged.

Renames inside this directory:

- `middleware.js` → `loadCommunityMiddleware.js`
- `runServer.js` → `runDevServer.js`

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D113412985

fbshipit-source-id: 9e27c7a02f057a37d702d5f13e55271244573d35
This commit is contained in:
Alex Hunt authored and meta-codesync[bot] committed 2026-07-25 10:32:46 -07:00
1 parent cec7c453fb
commit aee0ec3bd5
9 files changed
+107 -110

No files matched your search

@@ -10,14 +10,12 @@
import type {Command} from '@react-native-community/cli-types';
import runServer from './runServer';
import runDevServer from '../dev-server/runDevServer';
import path from 'node:path';
export type {StartCommandArgs} from './runServer';
const startCommand: Command = {
name: 'start',
func: runServer,
func: runDevServer,
description: 'Start the React Native development server.',
options: [
{
@@ -1,96 +0,0 @@
/**
* 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.
*
* @flow strict-local
* @format
*/
import type {Server} from 'connect';
import type {TerminalReportableEvent} from 'metro';
import {typeof createDevServerMiddleware as CreateDevServerMiddleware} from '@react-native-community/cli-server-api';
const debug = require('debug')('ReactNative:CommunityCliPlugin');
type MiddlewareReturn = {
middleware: Server,
websocketEndpoints: {
[path: string]: ws$WebSocketServer,
},
messageSocketEndpoint: {
server: ws$WebSocketServer,
broadcast: (
method: string,
params?: Record<string, unknown> | null,
) => void,
},
eventsSocketEndpoint: {
server: ws$WebSocketServer,
reportEvent: (event: TerminalReportableEvent) => void,
},
...
};
// $FlowFixMe[incompatible-type]
const unusedStubWSServer: ws$WebSocketServer = {};
// $FlowFixMe[incompatible-type]
const unusedMiddlewareStub: Server = {};
const communityMiddlewareFallback = {
createDevServerMiddleware: (params: {
host?: string,
port: number,
watchFolders: ReadonlyArray<string>,
}): MiddlewareReturn => ({
// FIXME: Several features will break without community middleware and
// should be migrated into core.
// e.g. used by Libraries/Core/Devtools:
// - /open-stack-frame
// - /open-url
// - /symbolicate
middleware: unusedMiddlewareStub,
websocketEndpoints: {},
messageSocketEndpoint: {
server: unusedStubWSServer,
broadcast: (
method: string,
_params?: Record<string, unknown> | null,
): void => {},
},
eventsSocketEndpoint: {
server: unusedStubWSServer,
reportEvent: (event: TerminalReportableEvent) => {},
},
}),
};
// Attempt to use the community middleware if it exists, but fallback to
// the stubs if it doesn't.
try {
// `@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');
// Until https://github.com/react-native-community/cli/pull/2605 lands,
// we need to find `@react-native-community/cli-server-api` via
// `@react-native-community/cli`. Once that lands, we can simply
// require('@react-native-community/cli').
const communityCliServerApiPath = require.resolve(
'@react-native-community/cli-server-api',
{paths: [communityCliPath]},
);
// $FlowFixMe[unsupported-syntax] dynamic import
communityMiddlewareFallback.createDevServerMiddleware = require(
communityCliServerApiPath,
).createDevServerMiddleware as CreateDevServerMiddleware;
} catch {
debug(`⚠️ Unable to find @react-native-community/cli-server-api
Starting the server without the community middleware.`);
}
export const createDevServerMiddleware =
communityMiddlewareFallback.createDevServerMiddleware;
@@ -33,6 +33,7 @@ export default async function isDevServerRunning(
return 'not_running';
}
// FIXME: Depends on @react-native-community/cli-server-api
const statusResponse = await fetch(`${devServerUrl}/status`);
const body = await statusResponse.text();
@@ -0,0 +1,93 @@
/**
* 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.
*
* @flow strict-local
* @format
*/
import type {Server} from 'connect';
import type {TerminalReportableEvent} from 'metro';
const debug = require('debug')('ReactNative:CommunityCliPlugin');
type DevServerMiddlewareFactory = (params: {
host?: string,
port: number,
watchFolders: ReadonlyArray<string>,
}) => {
middleware: Server,
websocketEndpoints: {[path: string]: ws$WebSocketServer},
messageSocketEndpoint: {
server: ws$WebSocketServer,
broadcast: (
method: string,
params?: Record<string, unknown> | null,
) => void,
},
eventsSocketEndpoint: {
server: ws$WebSocketServer,
reportEvent: (event: TerminalReportableEvent) => void,
},
...
};
// $FlowFixMe[incompatible-type]
const unusedStubWSServer: ws$WebSocketServer = {};
// $FlowFixMe[incompatible-type]
const unusedMiddlewareStub: Server = {};
// FIXME: Several features will break without community middleware
// (@react-native-community/cli-server-api) and should be migrated into core.
// e.g. used by packages/react-native/Libraries/Core/Devtools/:
// - /open-stack-frame
// - /open-url
// - /symbolicate
// e.g. used by ./isDevServerRunning.js:
// - /status
const communityMiddlewareFallback: DevServerMiddlewareFactory = () => ({
middleware: unusedMiddlewareStub,
websocketEndpoints: {},
messageSocketEndpoint: {
server: unusedStubWSServer,
broadcast: (
method: string,
_params?: Record<string, unknown> | null,
): void => {},
},
eventsSocketEndpoint: {
server: unusedStubWSServer,
reportEvent: (event: TerminalReportableEvent) => {},
},
});
/**
* Attempt to load the `createDevServerMiddleware` factory from
* `@react-native-community/cli` (an optional peer dependency). If it cannot be
* found, return a factory that produces stub middleware instead.
*/
export default function loadCommunityMiddleware(): DevServerMiddlewareFactory {
try {
// `@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');
// Until https://github.com/react-native-community/cli/pull/2605 lands,
// we need to find `@react-native-community/cli-server-api` via
// `@react-native-community/cli`. Once that lands, we can simply
// require('@react-native-community/cli').
const communityCliServerApiPath = require.resolve(
'@react-native-community/cli-server-api',
{paths: [communityCliPath]},
);
// $FlowFixMe[unsupported-syntax] dynamic import
return require(communityCliServerApiPath).createDevServerMiddleware;
} catch {
debug(`⚠️ Unable to find @react-native-community/cli-server-api
Starting the server without the community middleware.`);
return communityMiddlewareFallback;
}
}
@@ -11,19 +11,19 @@
import type {Config} from '@react-native-community/cli-types';
import type {Reporter, TerminalReportableEvent, TerminalReporter} from 'metro';
import createDevMiddlewareLogger from '../../utils/createDevMiddlewareLogger';
import isDevServerRunning from '../../utils/isDevServerRunning';
import loadMetroConfig from '../../utils/loadMetroConfig';
import * as version from '../../utils/version';
import loadMetroConfig from '../utils/loadMetroConfig';
import attachKeyHandlers from './attachKeyHandlers';
import {createDevServerMiddleware} from './middleware';
import createDevMiddlewareLogger from './createDevMiddlewareLogger';
import isDevServerRunning from './isDevServerRunning';
import loadCommunityMiddleware from './loadCommunityMiddleware';
import * as version from './version';
import {createDevMiddleware} from '@react-native/dev-middleware';
import * as Metro from 'metro';
import path from 'node:path';
import url from 'node:url';
import {styleText} from 'node:util';
export type StartCommandArgs = {
export type DevServerOptions = {
assetPlugins?: string[],
cert?: string,
customLogReporterPath?: string,
@@ -43,10 +43,10 @@ export type StartCommandArgs = {
clientLogs: boolean,
};
async function runServer(
async function runDevServer(
_argv: Array<string>,
cliConfig: Config,
args: StartCommandArgs,
args: DevServerOptions,
) {
const metroConfig = await loadMetroConfig(cliConfig, {
config: args.config,
@@ -107,12 +107,13 @@ async function runServer(
const ReporterImpl = getReporterImpl(args.customLogReporterPath);
const terminalReporter = new ReporterImpl(terminal);
const createCommunityMiddleware = loadCommunityMiddleware();
const {
middleware: communityMiddleware,
websocketEndpoints: communityWebsocketEndpoints,
messageSocketEndpoint,
eventsSocketEndpoint,
} = createDevServerMiddleware({
} = createCommunityMiddleware({
host: hostname,
port,
watchFolders,
@@ -186,4 +187,4 @@ function getReporterImpl(
}
}
export default runServer;
export default runDevServer;