Skip to content

Commit 886a5bc

Browse files
huntiefacebook-github-bot
authored andcommitted
Integrate dev-middleware into start command (facebook#39059)
Summary: Pull Request resolved: facebook#39059 ## Context RFC: Decoupling Flipper from React Native core: react-native-community/discussions-and-proposals#641 ## Changes This change: - Links the new `react-native/dev-middleware` endpoints into the recently migrated `react-native start` command. - Adds `react-native/community-cli-plugin` (the migrated [`cli-plugin-metro`](https://github.com/react-native-community/cli/tree/main/packages/cli-plugin-metro)) as a dependency of `react-native`, and hooks in these versions of the `start`, `bundle`, and `ram-bundle` commands via `react-native.config.js`. Functionally, this means that the new `/open-debugger` endpoint is available on the dev server started by `react-native start` (not yet linked into any UI). After this PR is merged, the new `community-cli-plugin` package is "linked" and we can remove `cli-plugin-metro` from `react-native-community/cli`: react-native-community/cli#2055. Changelog: [Internal] Reviewed By: motiz88 Differential Revision: D47226421 fbshipit-source-id: 2b96b50591cd0529e3b7f73732f1f8057034860f
1 parent c82cf64 commit 886a5bc

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

packages/community-cli-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dist"
2323
],
2424
"dependencies": {
25+
"@react-native/dev-middleware": "^0.73.0",
2526
"@react-native-community/cli-server-api": "12.0.0-alpha.9",
2627
"@react-native-community/cli-tools": "12.0.0-alpha.9",
2728
"@react-native/metro-babel-transformer": "^0.73.11",

packages/community-cli-plugin/src/commands/start/runServer.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ import type {Config} from '@react-native-community/cli-types';
1313
import type {Reporter} from 'metro/src/lib/reporting';
1414
import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter';
1515
import typeof TerminalReporter from 'metro/src/lib/TerminalReporter';
16-
import type Server from 'metro/src/Server';
17-
import type {Middleware} from 'metro-config';
1816

1917
import chalk from 'chalk';
2018
import Metro from 'metro';
2119
import {Terminal} from 'metro-core';
2220
import path from 'path';
21+
import {createDevMiddleware} from '@react-native/dev-middleware';
2322
import {
2423
createDevServerMiddleware,
2524
indexPageMiddleware,
@@ -99,28 +98,21 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
9998
}
10099

101100
const {
102-
middleware,
103-
websocketEndpoints,
101+
middleware: communityMiddleware,
102+
websocketEndpoints: communityWebsocketEndpoints,
104103
messageSocketEndpoint,
105104
eventsSocketEndpoint,
106105
} = createDevServerMiddleware({
107106
host: args.host,
108107
port: metroConfig.server.port,
109108
watchFolders: metroConfig.watchFolders,
110109
});
111-
middleware.use(indexPageMiddleware);
112-
113-
const customEnhanceMiddleware = metroConfig.server.enhanceMiddleware;
114-
// $FlowIgnore[cannot-write] Assigning to readonly property
115-
metroConfig.server.enhanceMiddleware = (
116-
metroMiddleware: Middleware,
117-
server: Server,
118-
) => {
119-
if (customEnhanceMiddleware) {
120-
return middleware.use(customEnhanceMiddleware(metroMiddleware, server));
121-
}
122-
return middleware.use(metroMiddleware);
123-
};
110+
const {middleware, websocketEndpoints} = createDevMiddleware({
111+
host: args.host?.length ? args.host : 'localhost',
112+
port: metroConfig.server.port,
113+
projectRoot: metroConfig.projectRoot,
114+
logger,
115+
});
124116

125117
let reportEvent: (event: TerminalReportableEvent) => void;
126118
const terminal = new Terminal(process.stdout);
@@ -145,8 +137,15 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
145137
secure: args.https,
146138
secureCert: args.cert,
147139
secureKey: args.key,
148-
// $FlowFixMe[incompatible-call] Incompatibly defined WebSocketServer type
149-
websocketEndpoints,
140+
unstable_extraMiddleware: [
141+
communityMiddleware,
142+
indexPageMiddleware,
143+
middleware,
144+
],
145+
websocketEndpoints: {
146+
...communityWebsocketEndpoints,
147+
...websocketEndpoints,
148+
},
150149
});
151150

152151
reportEvent = eventsSocketEndpoint.reportEvent;

packages/react-native/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"@react-native-community/cli-platform-android": "12.0.0-alpha.9",
9797
"@react-native-community/cli-platform-ios": "12.0.0-alpha.9",
9898
"@react-native/assets-registry": "^0.73.0",
99+
"@react-native/community-cli-plugin": "^0.73.0",
99100
"@react-native/codegen": "^0.73.0",
100101
"@react-native/gradle-plugin": "^0.73.0",
101102
"@react-native/js-polyfills": "^0.73.0",

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@
1111

1212
const ios = require('@react-native-community/cli-platform-ios');
1313
const android = require('@react-native-community/cli-platform-android');
14+
const {
15+
bundleCommand,
16+
ramBundleCommand,
17+
startCommand,
18+
} = require('@react-native/community-cli-plugin');
1419

1520
module.exports = {
16-
commands: [...ios.commands, ...android.commands],
21+
commands: [
22+
...ios.commands,
23+
...android.commands,
24+
bundleCommand,
25+
ramBundleCommand,
26+
startCommand,
27+
],
1728
platforms: {
1829
ios: {
1930
projectConfig: ios.projectConfig,

packages/rn-tester/metro.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const config = {
2424
watchFolders: [
2525
path.resolve(__dirname, '../../node_modules'),
2626
path.resolve(__dirname, '../assets'),
27+
path.resolve(__dirname, '../community-cli-plugin'),
28+
path.resolve(__dirname, '../dev-middleware'),
2729
path.resolve(__dirname, '../normalize-color'),
2830
path.resolve(__dirname, '../polyfills'),
2931
path.resolve(__dirname, '../react-native'),

0 commit comments

Comments
 (0)