mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51261 Cleanup to use the new public type / API exports from `metro` landed in Metro 0.82.3, which is RN's minimum as of D74181990 / [PR](https://github.com/facebook/react-native/pull/51122), in preference to deep imports. Changelog: [Internal] Reviewed By: huntie Differential Revision: D74141939 fbshipit-source-id: 9405f88a85e248abe8a92be1dd5a5f1ea6ceeb87
94 lines
3.0 KiB
JavaScript
94 lines
3.0 KiB
JavaScript
/**
|
|
* 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, mixed> | null) => void,
|
|
},
|
|
eventsSocketEndpoint: {
|
|
server: ws$WebSocketServer,
|
|
reportEvent: (event: TerminalReportableEvent) => void,
|
|
},
|
|
...
|
|
};
|
|
|
|
// $FlowFixMe
|
|
const unusedStubWSServer: ws$WebSocketServer = {};
|
|
// $FlowFixMe
|
|
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, mixed> | 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]},
|
|
);
|
|
// $FlowIgnore[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;
|