2015-10-12 23:46:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-05-17 11:55:59 +00:00
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
* @format
|
2015-10-12 23:46:52 +00:00
|
|
|
*/
|
2017-05-17 11:55:59 +00:00
|
|
|
|
2015-10-12 23:46:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-24 18:38:10 +00:00
|
|
|
require('../../setupBabel')();
|
2017-12-15 14:21:09 +00:00
|
|
|
|
2017-11-29 12:37:22 +00:00
|
|
|
const Metro = require('metro');
|
2017-11-08 17:07:41 +00:00
|
|
|
|
2017-12-15 14:21:09 +00:00
|
|
|
const {Terminal} = require('metro-core');
|
2016-10-22 13:07:01 +00:00
|
|
|
|
2018-07-03 20:44:54 +00:00
|
|
|
const morgan = require('morgan');
|
2015-10-12 23:46:52 +00:00
|
|
|
const path = require('path');
|
2018-07-02 16:33:08 +00:00
|
|
|
const MiddlewareManager = require('./middleware/MiddlewareManager');
|
2017-07-25 00:04:38 +00:00
|
|
|
|
2017-11-29 11:00:49 +00:00
|
|
|
import type {ConfigT} from 'metro';
|
2017-05-17 11:55:59 +00:00
|
|
|
|
2017-05-17 11:56:02 +00:00
|
|
|
export type Args = {|
|
2017-05-17 11:55:59 +00:00
|
|
|
+assetExts: $ReadOnlyArray<string>,
|
2018-07-02 16:33:08 +00:00
|
|
|
+cert: string,
|
|
|
|
+customLogReporterPath?: string,
|
2017-05-17 11:55:59 +00:00
|
|
|
+host: string,
|
2018-07-02 16:33:08 +00:00
|
|
|
+https: boolean,
|
2017-06-13 16:11:57 +00:00
|
|
|
+maxWorkers: number,
|
2018-07-02 16:33:08 +00:00
|
|
|
+key: string,
|
2017-05-17 11:55:59 +00:00
|
|
|
+nonPersistent: boolean,
|
|
|
|
+platforms: $ReadOnlyArray<string>,
|
|
|
|
+port: number,
|
2018-06-22 15:09:48 +00:00
|
|
|
+projectRoot: string,
|
2018-07-02 16:33:08 +00:00
|
|
|
+providesModuleNodeModules: Array<string>,
|
2017-05-17 11:55:59 +00:00
|
|
|
+resetCache: boolean,
|
|
|
|
+sourceExts: $ReadOnlyArray<string>,
|
2018-07-02 16:33:08 +00:00
|
|
|
+transformer?: string,
|
2017-05-17 11:55:59 +00:00
|
|
|
+verbose: boolean,
|
2018-06-19 20:45:17 +00:00
|
|
|
+watchFolders: $ReadOnlyArray<string>,
|
2017-05-17 11:55:59 +00:00
|
|
|
|};
|
|
|
|
|
2018-07-02 16:33:08 +00:00
|
|
|
async function runServer(args: Args, config: ConfigT) {
|
2017-09-08 14:52:24 +00:00
|
|
|
const terminal = new Terminal(process.stdout);
|
|
|
|
const ReporterImpl = getReporterImpl(args.customLogReporterPath || null);
|
|
|
|
const reporter = new ReporterImpl(terminal);
|
2018-07-02 16:33:08 +00:00
|
|
|
const middlewareManager = new MiddlewareManager(args);
|
|
|
|
|
2018-07-03 20:44:54 +00:00
|
|
|
middlewareManager.getConnectInstance().use(morgan('combined'));
|
|
|
|
|
2018-07-02 16:33:08 +00:00
|
|
|
args.watchFolders.forEach(middlewareManager.serveStatic);
|
|
|
|
|
|
|
|
const serverInstance = await Metro.runServer({
|
|
|
|
config: {
|
|
|
|
...config,
|
|
|
|
hmrEnabled: true,
|
|
|
|
maxWorkers: args.maxWorkers,
|
|
|
|
reporter,
|
|
|
|
secure: args.https,
|
|
|
|
secureKey: args.key,
|
|
|
|
secureCert: args.cert,
|
|
|
|
transformModulePath: args.transformer
|
|
|
|
? path.resolve(args.transformer)
|
|
|
|
: config.getTransformModulePath(),
|
|
|
|
watch: !args.nonPersistent,
|
|
|
|
},
|
2018-07-04 06:45:28 +00:00
|
|
|
host: args.host,
|
|
|
|
port: args.port,
|
2017-06-12 23:06:36 +00:00
|
|
|
});
|
2018-07-02 16:33:08 +00:00
|
|
|
|
|
|
|
// In Node 8, the default keep-alive for an HTTP connection is 5 seconds. In
|
|
|
|
// early versions of Node 8, this was implemented in a buggy way which caused
|
|
|
|
// some HTTP responses (like those containing large JS bundles) to be
|
|
|
|
// terminated early.
|
|
|
|
//
|
|
|
|
// As a workaround, arbitrarily increase the keep-alive from 5 to 30 seconds,
|
|
|
|
// which should be enough to send even the largest of JS bundles.
|
|
|
|
//
|
|
|
|
// For more info: https://github.com/nodejs/node/issues/13391
|
|
|
|
//
|
2018-07-09 15:06:58 +00:00
|
|
|
// $FlowFixMe (site=react_native_fb)
|
2018-07-02 16:33:08 +00:00
|
|
|
serverInstance.keepAliveTimeout = 30000;
|
2015-10-12 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 14:52:24 +00:00
|
|
|
function getReporterImpl(customLogReporterPath: ?string) {
|
|
|
|
if (customLogReporterPath == null) {
|
2017-11-29 11:00:49 +00:00
|
|
|
return require('metro/src/lib/TerminalReporter');
|
2017-09-08 14:52:24 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
// First we let require resolve it, so we can require packages in node_modules
|
|
|
|
// as expected. eg: require('my-package/reporter');
|
|
|
|
/* $FlowFixMe: can't type dynamic require */
|
|
|
|
return require(customLogReporterPath);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
// If that doesn't work, then we next try relative to the cwd, eg:
|
|
|
|
// require('./reporter');
|
|
|
|
/* $FlowFixMe: can't type dynamic require */
|
|
|
|
return require(path.resolve(customLogReporterPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:46:52 +00:00
|
|
|
module.exports = runServer;
|