|
12 | 12 | import type {Server} from 'connect';
|
13 | 13 | import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter';
|
14 | 14 |
|
| 15 | +import {typeof createDevServerMiddleware as CreateDevServerMiddleware} from '@react-native-community/cli-server-api'; |
| 16 | + |
15 | 17 | const debug = require('debug')('ReactNative:CommunityCliPlugin');
|
16 | 18 |
|
17 | 19 | type MiddlewareReturn = {
|
@@ -66,9 +68,23 @@ const communityMiddlewareFallback = {
|
66 | 68 | // Attempt to use the community middleware if it exists, but fallback to
|
67 | 69 | // the stubs if it doesn't.
|
68 | 70 | try {
|
69 |
| - const community = require('@react-native-community/cli-server-api'); |
70 |
| - communityMiddlewareFallback.createDevServerMiddleware = |
71 |
| - community.createDevServerMiddleware; |
| 71 | + // `@react-native-community/cli` is an optional peer dependency of this |
| 72 | + // package, and should be a dev dependency of the host project (via the |
| 73 | + // community template's package.json). |
| 74 | + const communityCliPath = require.resolve('@react-native-community/cli'); |
| 75 | + |
| 76 | + // Until https://github.com/react-native-community/cli/pull/2605 lands, |
| 77 | + // we need to find `@react-native-community/cli-server-api` via |
| 78 | + // `@react-native-community/cli`. Once that lands, we can simply |
| 79 | + // require('@react-native-community/cli'). |
| 80 | + const communityCliServerApiPath = require.resolve( |
| 81 | + '@react-native-community/cli-server-api', |
| 82 | + {paths: [communityCliPath]}, |
| 83 | + ); |
| 84 | + // $FlowIgnore[unsupported-syntax] dynamic import |
| 85 | + communityMiddlewareFallback.createDevServerMiddleware = require( |
| 86 | + communityCliServerApiPath, |
| 87 | + ).createDevServerMiddleware as CreateDevServerMiddleware; |
72 | 88 | } catch {
|
73 | 89 | debug(`⚠️ Unable to find @react-native-community/cli-server-api
|
74 | 90 | Starting the server without the community middleware.`);
|
|
0 commit comments