Skip to content

Commit 34bee77

Browse files
huntiefacebook-github-bot
authored andcommitted
Integrate dev-middleware into start command (#39059)
Summary: Pull Request resolved: #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: 892691568dd191ca4702460d75415a79de3673d3
1 parent eb3d5a4 commit 34bee77

File tree

9 files changed

+46
-21
lines changed

9 files changed

+46
-21
lines changed

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ munge_underscores=true
4747

4848
module.name_mapper='^react-native$' -> '<PROJECT_ROOT>/packages/react-native/index.js'
4949
module.name_mapper='^react-native/\(.*\)$' -> '<PROJECT_ROOT>/packages/react-native\1'
50+
module.name_mapper='^@react-native/dev-middleware$' -> '<PROJECT_ROOT>/packages/dev-middleware'
5051
module.name_mapper='^@?[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '<PROJECT_ROOT>/packages/react-native/Libraries/Image/RelativeImageStub'
5152

5253
suppress_type=$FlowIssue

.flowconfig.android

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ munge_underscores=true
4747

4848
module.name_mapper='^react-native$' -> '<PROJECT_ROOT>/packages/react-native/index.js'
4949
module.name_mapper='^react-native/\(.*\)$' -> '<PROJECT_ROOT>/packages/react-native\1'
50+
module.name_mapper='^@react-native/dev-middleware$' -> '<PROJECT_ROOT>/packages/dev-middleware'
5051
module.name_mapper='^@?[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '<PROJECT_ROOT>/packages/react-native/Libraries/Image/RelativeImageStub'
5152

5253
suppress_type=$FlowIssue

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'),

scripts/run-ci-e2e-tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ try {
8888
describe('Set up Verdaccio');
8989
VERDACCIO_PID = setupVerdaccio(ROOT, VERDACCIO_CONFIG_PATH);
9090

91-
describe('Publish packages');
91+
describe('Build and publish packages');
92+
exec('node ./scripts/build/build.js', {cwd: ROOT});
9293
forEachPackage(
9394
(packageAbsolutePath, packageRelativePathFromRoot, packageManifest) => {
9495
if (packageManifest.private) {

scripts/template/initialize.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const yargs = require('yargs');
1313
const {execSync} = require('child_process');
14+
const path = require('path');
1415

1516
const forEachPackage = require('../monorepo/for-each-package');
1617
const setupVerdaccio = require('../setup-verdaccio');
@@ -41,6 +42,7 @@ const {argv} = yargs
4142

4243
const {reactNativeRootPath, templateName, templateConfigPath, directory} = argv;
4344

45+
const REPO_ROOT = path.resolve(__dirname, '../..');
4446
const VERDACCIO_CONFIG_PATH = `${reactNativeRootPath}/.circleci/verdaccio.yml`;
4547

4648
async function install() {
@@ -51,6 +53,12 @@ async function install() {
5153
try {
5254
process.stdout.write('Bootstrapped Verdaccio \u2705\n');
5355

56+
process.stdout.write('Building packages...\n');
57+
execSync('node ./scripts/build/build.js', {
58+
cwd: REPO_ROOT,
59+
stdio: [process.stdin, process.stdout, process.stderr],
60+
});
61+
5462
process.stdout.write('Starting to publish every package...\n');
5563
forEachPackage(
5664
(packageAbsolutePath, packageRelativePathFromRoot, packageManifest) => {

0 commit comments

Comments
 (0)