mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
c4a66a89a2
Summary: Scope of the diff: 1. Middleware `react-native-github/local-cli` and `react-native-internal-cli` uses a very similar set of middlewares (internal cli extends github version), so I decided to move it to a standalone file (middleware manager) in order to remove duplications and increase readability. 2. Types Seems that after Flow upgrade to version 0.68 there were many type issues to resolve, so all of them were auto-mocked. This is fine, but I'd like to see Flow assists me with `Metro.createServer` -> `Metro.runServer` migration. Hence, I decided to resolve flow mocks, related to runServer. 3. `runServer` signature In `react-native-github` repo I cleaned up `runServer` signature by removing `startCallback` and `readyCallback` from the function parameters and moved them to `runServer` instead. 4. Replace `createServer` by `runServer` In `react-native-github` repo, `createServer` has been replaced by `runServer`. __Some of arguments are not mapped__. Note that this diff will partially break argument mapping. This is intentional. @[100000044482482:ives] will fix it with a new config package. Reviewed By: mjesun Differential Revision: D8711717 fbshipit-source-id: a843ab576360ff7242099910d8f25a9cb0a388c0
103 lines
2.9 KiB
JavaScript
103 lines
2.9 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
require('../../setupBabel')();
|
|
|
|
const Metro = require('metro');
|
|
|
|
const {Terminal} = require('metro-core');
|
|
|
|
const path = require('path');
|
|
const MiddlewareManager = require('./middleware/MiddlewareManager');
|
|
|
|
import type {ConfigT} from 'metro';
|
|
|
|
export type Args = {|
|
|
+assetExts: $ReadOnlyArray<string>,
|
|
+cert: string,
|
|
+customLogReporterPath?: string,
|
|
+host: string,
|
|
+https: boolean,
|
|
+maxWorkers: number,
|
|
+key: string,
|
|
+nonPersistent: boolean,
|
|
+platforms: $ReadOnlyArray<string>,
|
|
+port: number,
|
|
+projectRoot: string,
|
|
+providesModuleNodeModules: Array<string>,
|
|
+resetCache: boolean,
|
|
+sourceExts: $ReadOnlyArray<string>,
|
|
+transformer?: string,
|
|
+verbose: boolean,
|
|
+watchFolders: $ReadOnlyArray<string>,
|
|
|};
|
|
|
|
async function runServer(args: Args, config: ConfigT) {
|
|
const terminal = new Terminal(process.stdout);
|
|
const ReporterImpl = getReporterImpl(args.customLogReporterPath || null);
|
|
const reporter = new ReporterImpl(terminal);
|
|
const middlewareManager = new MiddlewareManager(args);
|
|
|
|
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,
|
|
},
|
|
});
|
|
|
|
// 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
|
|
//
|
|
// $FlowFixMe
|
|
serverInstance.keepAliveTimeout = 30000;
|
|
}
|
|
|
|
function getReporterImpl(customLogReporterPath: ?string) {
|
|
if (customLogReporterPath == null) {
|
|
return require('metro/src/lib/TerminalReporter');
|
|
}
|
|
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));
|
|
}
|
|
}
|
|
|
|
module.exports = runServer;
|