packager: runServer: @flow

Summary: This makes it easier to verify correctness when adding new config/args.

Reviewed By: davidaurelio

Differential Revision: D5069537

fbshipit-source-id: 4d8058851900b23163d0f2744e91dd14dfcdd461
This commit is contained in:
Jean Lauliac 2017-05-17 04:55:59 -07:00 committed by Facebook Github Bot
parent ca2d57c744
commit b98c33f1b4
8 changed files with 60 additions and 29 deletions

View File

@ -5,7 +5,11 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/
'use strict';
const InspectorProxy = require('./util/inspectorProxy.js');
@ -18,7 +22,8 @@ const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
const defaultAssetExts = require('../../packager/defaults').assetExts;
const defaultSourceExts = require('../../packager/defaults').sourceExts;
const defaultPlatforms = require('../../packager/defaults').platforms;
const defaultProvidesModuleNodeModules = require('../../packager/defaults').providesModuleNodeModules;
const defaultProvidesModuleNodeModules = require('../../packager/defaults')
.providesModuleNodeModules;
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const http = require('http');
const indexPageMiddleware = require('./middleware/indexPage');
@ -31,7 +36,26 @@ const systraceProfileMiddleware = require('./middleware/systraceProfileMiddlewar
const unless = require('./middleware/unless');
const webSocketProxy = require('./util/webSocketProxy.js');
function runServer(args, config, startedCallback, readyCallback) {
import type {ConfigT} from '../util/Config';
type Args = {|
+assetExts: $ReadOnlyArray<string>,
+host: string,
+nonPersistent: boolean,
+platforms: $ReadOnlyArray<string>,
+port: number,
+projectRoots: $ReadOnlyArray<string>,
+resetCache: boolean,
+sourceExts: $ReadOnlyArray<string>,
+verbose: boolean,
|};
function runServer(
args: Args,
config: ConfigT,
startedCallback: () => mixed,
readyCallback: () => mixed,
) {
var wsProxy = null;
var ms = null;
const packagerServer = getPackagerServer(args, config);
@ -41,7 +65,9 @@ function runServer(args, config, startedCallback, readyCallback) {
const app = connect()
.use(loadRawBodyMiddleware)
.use(connect.compress())
.use(getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()))
.use(
getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()),
)
.use(getDevToolsMiddleware(args, () => ms && ms.isChromeConnected()))
.use(openStackFrameInEditorMiddleware(args))
.use(copyToClipBoardMiddleware)
@ -49,30 +75,32 @@ function runServer(args, config, startedCallback, readyCallback) {
.use(systraceProfileMiddleware)
.use(cpuProfilerMiddleware)
.use(indexPageMiddleware)
.use(unless('/inspector', inspectorProxy.processRequest.bind(inspectorProxy)))
.use(
unless('/inspector', inspectorProxy.processRequest.bind(inspectorProxy)),
)
.use(packagerServer.processRequest.bind(packagerServer));
args.projectRoots.forEach(root => app.use(connect.static(root)));
app.use(connect.logger())
.use(connect.errorHandler());
app.use(connect.logger()).use(connect.errorHandler());
const serverInstance = http.createServer(app).listen(
args.port,
args.host,
function() {
const serverInstance = http
.createServer(app)
.listen(args.port, args.host, 511, function() {
attachHMRServer({
httpServer: serverInstance,
path: '/hot',
packagerServer,
});
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
wsProxy = webSocketProxy.attachToServer(
serverInstance,
'/debugger-proxy',
);
ms = messageSocket.attachToServer(serverInstance, '/message');
inspectorProxy.attachToServer(serverInstance, '/inspector');
readyCallback(packagerServer._reporter);
}
);
});
// Disable any kind of automatic timeout behavior for incoming
// requests in case it takes the packager more than the default
// timeout of 120 seconds to respond to a request.
@ -80,10 +108,11 @@ function runServer(args, config, startedCallback, readyCallback) {
}
function getPackagerServer(args, config) {
const transformModulePath =
args.transformer ? path.resolve(args.transformer) :
typeof config.getTransformModulePath === 'function' ? config.getTransformModulePath() :
undefined;
const transformModulePath = args.transformer
? path.resolve(args.transformer)
: typeof config.getTransformModulePath === 'function'
? config.getTransformModulePath()
: undefined;
const providesModuleNodeModules =
args.providesModuleNodeModules || defaultProvidesModuleNodeModules;
@ -93,10 +122,12 @@ function getPackagerServer(args, config) {
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 */
LogReporter = require(args.customLogReporterPath);
} catch (e) {
// If that doesn't work, then we next try relative to the cwd, eg:
// require('./reporter');
/* $FlowFixMe: can't type dynamic require */
LogReporter = require(path.resolve(args.customLogReporterPath));
}
} else {

View File

@ -31,7 +31,7 @@ type Options = {
nonPersistent?: boolean,
postProcessModules?: PostProcessModules,
postMinifyProcess?: PostMinifyProcess,
projectRoots: Array<string>,
projectRoots: $ReadOnlyArray<string>,
reporter?: Reporter,
+sourceExts: ?Array<string>,
+transformModulePath: string,

View File

@ -128,7 +128,7 @@ type Options = {|
+polyfillModuleNames: Array<string>,
+postProcessModules?: PostProcessModules,
+postMinifyProcess: PostMinifyProcess,
+projectRoots: Array<string>,
+projectRoots: $ReadOnlyArray<string>,
+providesModuleNodeModules?: Array<string>,
+reporter: Reporter,
+resetCache: boolean,
@ -146,7 +146,7 @@ class Bundler {
_getModuleId: (opts: Module) => number;
_transformer: Transformer;
_resolverPromise: Promise<Resolver>;
_projectRoots: Array<string>;
_projectRoots: $ReadOnlyArray<string>;
_assetServer: AssetServer;
_getTransformOptions: void | GetTransformOptions;
@ -785,7 +785,7 @@ class Bundler {
hot: boolean,
minify: boolean,
platform: ?string,
projectRoots: Array<string>,
projectRoots: $ReadOnlyArray<string>,
|},
): Promise<BundlingOptions> {
const getDependencies = (entryFile: string) =>

View File

@ -42,7 +42,7 @@ type Options = {|
+postMinifyProcess: PostMinifyProcess,
+platforms: Set<string>,
+polyfillModuleNames?: Array<string>,
+projectRoots: Array<string>,
+projectRoots: $ReadOnlyArray<string>,
+providesModuleNodeModules: Array<string>,
+reporter: Reporter,
+resetCache: boolean,

View File

@ -69,7 +69,7 @@ type Options = {
polyfillModuleNames?: Array<string>,
postProcessModules?: PostProcessModules,
postMinifyProcess: PostMinifyProcess,
projectRoots: Array<string>,
projectRoots: $ReadOnlyArray<string>,
providesModuleNodeModules?: Array<string>,
reporter: Reporter,
resetCache?: boolean,
@ -125,7 +125,7 @@ class Server {
polyfillModuleNames: Array<string>,
postProcessModules?: PostProcessModules,
postMinifyProcess: PostMinifyProcess,
projectRoots: Array<string>,
projectRoots: $ReadOnlyArray<string>,
providesModuleNodeModules?: Array<string>,
reporter: Reporter,
resetCache: boolean,
@ -135,7 +135,7 @@ class Server {
transformTimeoutInterval: ?number,
watch: boolean,
};
_projectRoots: Array<string>;
_projectRoots: $ReadOnlyArray<string>;
_bundles: {};
_changeWatchers: Array<{
req: IncomingMessage,

View File

@ -60,7 +60,7 @@ type Options = {|
+providesModuleNodeModules: Array<string>,
+reporter: Reporter,
+resetCache: boolean,
+roots: Array<string>,
+roots: $ReadOnlyArray<string>,
+sourceExts: Array<string>,
+transformCode: TransformCode,
+useWatchman: boolean,

View File

@ -41,7 +41,7 @@ class ModuleCache {
_platforms: Set<string>;
_transformCode: TransformCode;
_reporter: Reporter;
_roots: Array<string>;
_roots: $ReadOnlyArray<string>;
constructor(
{
@ -62,7 +62,7 @@ class ModuleCache {
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
moduleOptions: ModuleOptions,
roots: Array<string>,
roots: $ReadOnlyArray<string>,
reporter: Reporter,
transformCode: TransformCode,
|},

View File

@ -18,7 +18,7 @@ export type LocalPath = OpaqueLocalPath & string;
// FIXME: This function has the shortcoming of potentially returning identical
// paths for two files in different roots.
function toLocalPath(roots: Array<string>, absolutePath: string): LocalPath {
function toLocalPath(roots: $ReadOnlyArray<string>, absolutePath: string): LocalPath {
for (let i = 0; i < roots.length; i++) {
const localPath = relative(roots[i], absolutePath);
if (localPath[0] !== '.') {