Skip to content

Commit 57d8a92

Browse files
motiz88facebook-github-bot
authored andcommittedOct 4, 2021
Add waitForBundler option to API
Summary: Adds the `waitForBundler` option to `runServer()` and `runMetro()`. If set to `true`, this option causes Metro to fully initialise the bundler (crawling the filesystem, creating the transformer, etc) before setting up the HTTP server. If `false` (the default), the HTTP server and bundler are initialised in parallel, which means the first bundle request might need to wait for initialisation to finish. This option is primarily useful for "prewarming" the bundler (in particular the dependency graph / Haste map) by programmatically creating a short-lived server instance. Reviewed By: feedthejim Differential Revision: D31354827 fbshipit-source-id: c24ae44c7724a5849eac9a92dab592915a99c4d3
1 parent bb7f96f commit 57d8a92

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎docs/GettingStarted.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ We recommend using `runMetro` instead of `runServer`, `runMetro` calls this func
8787
* `secureKey (string)`: **DEPRECATED** The key to use for `https` when `secure` is on.
8888
* `secureCert (string)`: **DEPRECATED** The cert to use for `https` when `secure` is on.
8989
* `secureServerOptions (Object)`: The options object to pass to the Metro's https server. The presence of this object will make Metro's server run on `https`. Refer to the [nodejs docs](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener) for valid options.
90+
* `waitForBundler (boolean)`: Whether to wait for the bundler to finish initializing before returning the server instance.
9091

9192
```js
9293
const config = await Metro.loadConfig();

‎packages/metro/src/index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ type MetroMiddleWare = {|
4747
middleware: Middleware,
4848
|};
4949

50+
export type RunMetroOptions = {
51+
...ServerOptions,
52+
waitForBundler?: boolean,
53+
};
54+
5055
export type RunServerOptions = {|
5156
hasReducedPerformance?: boolean,
5257
host?: string,
@@ -57,6 +62,7 @@ export type RunServerOptions = {|
5762
secure?: boolean, // deprecated
5863
secureCert?: string, // deprecated
5964
secureKey?: string, // deprecated
65+
waitForBundler?: boolean,
6066
websocketEndpoints?: {
6167
[path: string]: typeof ws.Server,
6268
},
@@ -115,7 +121,7 @@ async function getConfig(config: InputConfigT): Promise<ConfigT> {
115121

116122
async function runMetro(
117123
config: InputConfigT,
118-
options?: ServerOptions,
124+
options?: RunMetroOptions,
119125
): Promise<MetroServer> {
120126
const mergedConfig = await getConfig(config);
121127
const {
@@ -131,9 +137,10 @@ async function runMetro(
131137
type: 'initialize_started',
132138
});
133139

134-
const server = new MetroServer(mergedConfig, options);
140+
const {waitForBundler = false, ...serverOptions} = options ?? {};
141+
const server = new MetroServer(mergedConfig, serverOptions);
135142

136-
server
143+
const readyPromise = server
137144
.ready()
138145
.then(() => {
139146
reporter.update({
@@ -148,6 +155,9 @@ async function runMetro(
148155
error,
149156
});
150157
});
158+
if (waitForBundler) {
159+
await readyPromise;
160+
}
151161

152162
return server;
153163
}
@@ -157,7 +167,7 @@ exports.loadConfig = loadConfig;
157167

158168
exports.createConnectMiddleware = async function(
159169
config: ConfigT,
160-
options?: ServerOptions,
170+
options?: RunMetroOptions,
161171
): Promise<MetroMiddleWare> {
162172
const metroServer = await runMetro(config, options);
163173

@@ -210,6 +220,7 @@ exports.runServer = async (
210220
secure, //deprecated
211221
secureCert, // deprecated
212222
secureKey, // deprecated
223+
waitForBundler = false,
213224
websocketEndpoints = {},
214225
}: RunServerOptions,
215226
): Promise<HttpServer | HttpsServer> => {
@@ -231,6 +242,7 @@ exports.runServer = async (
231242
config,
232243
{
233244
hasReducedPerformance,
245+
waitForBundler,
234246
},
235247
);
236248

0 commit comments

Comments
 (0)
Please sign in to comment.