9
9
* @oncall react_native
10
10
*/
11
11
12
- import type { NextHandleFunction , Server } from 'connect' ;
12
+ import typeof * as CLIServerAPI from '@react-native-community/cli-server-api' ;
13
+ import type { Server } from 'connect' ;
13
14
import type { TerminalReportableEvent } from 'metro/src/lib/TerminalReporter' ;
14
15
15
16
const debug = require ( 'debug' ) ( 'ReactNative:CommunityCliPlugin' ) ;
@@ -30,10 +31,6 @@ type MiddlewareReturn = {
30
31
...
31
32
} ;
32
33
33
- const noopNextHandle : NextHandleFunction = ( req , res , next ) => {
34
- next ( ) ;
35
- } ;
36
-
37
34
// $FlowFixMe
38
35
const unusedStubWSServer : ws$WebSocketServer = { } ;
39
36
// $FlowFixMe
@@ -45,6 +42,12 @@ const communityMiddlewareFallback = {
45
42
port : number ,
46
43
watchFolders : $ReadOnlyArray < string > ,
47
44
} ) : MiddlewareReturn => ( {
45
+ // FIXME: Several features will break without community middleware and
46
+ // should be migrated into core.
47
+ // e.g. used by Libraries/Core/Devtools:
48
+ // - /open-stack-frame
49
+ // - /open-url
50
+ // - /symbolicate
48
51
middleware : unusedMiddlewareStub ,
49
52
websocketEndpoints : { } ,
50
53
messageSocketEndpoint : {
@@ -59,23 +62,34 @@ const communityMiddlewareFallback = {
59
62
reportEvent : ( event : TerminalReportableEvent ) => { } ,
60
63
} ,
61
64
} ) ,
62
- indexPageMiddleware : noopNextHandle ,
63
65
} ;
64
66
65
67
// Attempt to use the community middleware if it exists, but fallback to
66
68
// the stubs if it doesn't.
67
69
try {
68
- const community = require ( '@react-native-community/cli-server-api' ) ;
69
- communityMiddlewareFallback . indexPageMiddleware =
70
- community . indexPageMiddleware ;
70
+ // `@react-native-community/cli` is an optional peer dependency of this
71
+ // package, and should be a dev dependency of the host project (via the
72
+ // community template's package.json).
73
+ const communityCliPath = require . resolve ( '@react-native-community/cli' ) ;
74
+
75
+ // `@react-native-community/cli-server-api` is a dependency of
76
+ // `@react-native-community/cli`, but is not re-exported by it, so we need
77
+ // to resolve the former through the latter.
78
+ const communityCliServerApiPath = require . resolve (
79
+ '@react-native-community/cli-server-api' ,
80
+ { paths : [ communityCliPath ] } ,
81
+ ) ;
82
+ // $FlowIgnore[unsupported-syntax] dynamic import
83
+ const communityCliServerApi : CLIServerAPI = require (
84
+ communityCliServerApiPath ,
85
+ ) ;
86
+ // $FlowIgnore[unsupported-syntax] dynamic import
71
87
communityMiddlewareFallback . createDevServerMiddleware =
72
- community . createDevServerMiddleware ;
88
+ communityCliServerApi . createDevServerMiddleware ;
73
89
} catch {
74
90
debug ( `⚠️ Unable to find @react-native-community/cli-server-api
75
91
Starting the server without the community middleware.` ) ;
76
92
}
77
93
78
94
export const createDevServerMiddleware =
79
95
communityMiddlewareFallback . createDevServerMiddleware ;
80
- export const indexPageMiddleware =
81
- communityMiddlewareFallback . indexPageMiddleware ;
0 commit comments