2015-10-12 23:46:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
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')();
|
2016-11-17 04:11:48 +00:00
|
|
|
const InspectorProxy = require('./util/inspectorProxy.js');
|
2017-05-24 18:38:10 +00:00
|
|
|
const ReactPackager = require('../../packager');
|
2017-06-01 09:57:56 +00:00
|
|
|
const Terminal = require('../../packager/src/lib/TerminalClass');
|
2016-10-22 13:07:01 +00:00
|
|
|
|
2016-01-15 03:32:17 +00:00
|
|
|
const attachHMRServer = require('./util/attachHMRServer');
|
2015-10-12 23:46:52 +00:00
|
|
|
const connect = require('connect');
|
2016-10-22 13:07:01 +00:00
|
|
|
const copyToClipBoardMiddleware = require('./middleware/copyToClipBoardMiddleware');
|
2015-10-26 19:59:54 +00:00
|
|
|
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
|
2017-05-31 18:00:41 +00:00
|
|
|
const defaultAssetExts = require('../../packager/src/defaults').assetExts;
|
|
|
|
const defaultSourceExts = require('../../packager/src/defaults').sourceExts;
|
|
|
|
const defaultPlatforms = require('../../packager/src/defaults').platforms;
|
|
|
|
const defaultProvidesModuleNodeModules = require('../../packager/src/defaults')
|
2017-05-17 11:55:59 +00:00
|
|
|
.providesModuleNodeModules;
|
2015-10-26 19:59:54 +00:00
|
|
|
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
|
2015-10-12 23:46:52 +00:00
|
|
|
const http = require('http');
|
2016-10-22 13:07:01 +00:00
|
|
|
const indexPageMiddleware = require('./middleware/indexPage');
|
2015-10-26 19:59:54 +00:00
|
|
|
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
|
2016-02-26 02:14:55 +00:00
|
|
|
const messageSocket = require('./util/messageSocket.js');
|
2015-10-26 19:59:54 +00:00
|
|
|
const openStackFrameInEditorMiddleware = require('./middleware/openStackFrameInEditorMiddleware');
|
2015-10-12 23:46:52 +00:00
|
|
|
const path = require('path');
|
2015-10-26 19:59:54 +00:00
|
|
|
const statusPageMiddleware = require('./middleware/statusPageMiddleware.js');
|
|
|
|
const systraceProfileMiddleware = require('./middleware/systraceProfileMiddleware.js');
|
2016-11-16 18:22:23 +00:00
|
|
|
const unless = require('./middleware/unless');
|
2016-11-17 04:11:48 +00:00
|
|
|
const webSocketProxy = require('./util/webSocketProxy.js');
|
2015-10-12 23:46:52 +00:00
|
|
|
|
2017-05-17 11:55:59 +00:00
|
|
|
import type {ConfigT} from '../util/Config';
|
2017-05-17 11:56:02 +00:00
|
|
|
import type {Reporter} from '../../packager/src/lib/reporting';
|
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>,
|
|
|
|
+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,
|
2017-05-17 11:56:02 +00:00
|
|
|
// FIXME: this is weird design. The top-level should pass down a custom
|
|
|
|
// reporter rather than passing it up as argument to an event.
|
|
|
|
startedCallback: (reporter: Reporter) => mixed,
|
|
|
|
readyCallback: (reporter: Reporter) => mixed,
|
2017-05-17 11:55:59 +00:00
|
|
|
) {
|
2015-10-26 19:59:54 +00:00
|
|
|
var wsProxy = null;
|
2016-02-26 02:14:55 +00:00
|
|
|
var ms = null;
|
2016-01-15 03:32:17 +00:00
|
|
|
const packagerServer = getPackagerServer(args, config);
|
2017-03-31 09:10:00 +00:00
|
|
|
startedCallback(packagerServer._reporter);
|
|
|
|
|
2016-11-15 16:55:37 +00:00
|
|
|
const inspectorProxy = new InspectorProxy();
|
2015-10-12 23:46:52 +00:00
|
|
|
const app = connect()
|
|
|
|
.use(loadRawBodyMiddleware)
|
2015-11-17 20:20:31 +00:00
|
|
|
.use(connect.compress())
|
2017-05-17 11:55:59 +00:00
|
|
|
.use(
|
|
|
|
getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()),
|
|
|
|
)
|
2016-02-26 02:14:55 +00:00
|
|
|
.use(getDevToolsMiddleware(args, () => ms && ms.isChromeConnected()))
|
2016-06-21 17:45:44 +00:00
|
|
|
.use(openStackFrameInEditorMiddleware(args))
|
2016-06-30 15:09:55 +00:00
|
|
|
.use(copyToClipBoardMiddleware)
|
2015-10-12 23:46:52 +00:00
|
|
|
.use(statusPageMiddleware)
|
|
|
|
.use(systraceProfileMiddleware)
|
|
|
|
.use(cpuProfilerMiddleware)
|
2016-05-24 18:54:28 +00:00
|
|
|
.use(indexPageMiddleware)
|
2017-05-17 11:55:59 +00:00
|
|
|
.use(
|
|
|
|
unless('/inspector', inspectorProxy.processRequest.bind(inspectorProxy)),
|
|
|
|
)
|
2016-01-15 03:32:17 +00:00
|
|
|
.use(packagerServer.processRequest.bind(packagerServer));
|
2015-10-12 23:46:52 +00:00
|
|
|
|
|
|
|
args.projectRoots.forEach(root => app.use(connect.static(root)));
|
|
|
|
|
2017-05-17 11:55:59 +00:00
|
|
|
app.use(connect.logger()).use(connect.errorHandler());
|
2015-10-12 23:46:52 +00:00
|
|
|
|
2017-05-17 11:55:59 +00:00
|
|
|
const serverInstance = http
|
|
|
|
.createServer(app)
|
|
|
|
.listen(args.port, args.host, 511, function() {
|
2016-01-15 03:32:17 +00:00
|
|
|
attachHMRServer({
|
|
|
|
httpServer: serverInstance,
|
|
|
|
path: '/hot',
|
|
|
|
packagerServer,
|
|
|
|
});
|
|
|
|
|
2017-05-17 11:55:59 +00:00
|
|
|
wsProxy = webSocketProxy.attachToServer(
|
|
|
|
serverInstance,
|
|
|
|
'/debugger-proxy',
|
|
|
|
);
|
2016-02-26 02:14:55 +00:00
|
|
|
ms = messageSocket.attachToServer(serverInstance, '/message');
|
2016-11-15 16:55:37 +00:00
|
|
|
inspectorProxy.attachToServer(serverInstance, '/inspector');
|
2017-03-31 09:10:00 +00:00
|
|
|
readyCallback(packagerServer._reporter);
|
2017-05-17 11:55:59 +00:00
|
|
|
});
|
2015-12-22 23:16:46 +00:00
|
|
|
// 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.
|
|
|
|
serverInstance.timeout = 0;
|
2015-10-12 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:32:17 +00:00
|
|
|
function getPackagerServer(args, config) {
|
2017-05-17 11:55:59 +00:00
|
|
|
const transformModulePath = args.transformer
|
|
|
|
? path.resolve(args.transformer)
|
|
|
|
: typeof config.getTransformModulePath === 'function'
|
|
|
|
? config.getTransformModulePath()
|
|
|
|
: undefined;
|
2016-06-22 15:08:28 +00:00
|
|
|
|
2017-01-13 05:03:57 +00:00
|
|
|
const providesModuleNodeModules =
|
|
|
|
args.providesModuleNodeModules || defaultProvidesModuleNodeModules;
|
|
|
|
|
2017-03-31 01:14:28 +00:00
|
|
|
let LogReporter;
|
|
|
|
if (args.customLogReporterPath) {
|
|
|
|
try {
|
|
|
|
// First we let require resolve it, so we can require packages in node_modules
|
|
|
|
// as expected. eg: require('my-package/reporter');
|
2017-05-17 11:55:59 +00:00
|
|
|
/* $FlowFixMe: can't type dynamic require */
|
2017-03-31 01:14:28 +00:00
|
|
|
LogReporter = require(args.customLogReporterPath);
|
2017-05-04 12:06:27 +00:00
|
|
|
} catch (e) {
|
2017-03-31 01:14:28 +00:00
|
|
|
// If that doesn't work, then we next try relative to the cwd, eg:
|
|
|
|
// require('./reporter');
|
2017-05-17 11:55:59 +00:00
|
|
|
/* $FlowFixMe: can't type dynamic require */
|
2017-03-31 01:14:28 +00:00
|
|
|
LogReporter = require(path.resolve(args.customLogReporterPath));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogReporter = require('../../packager/src/lib/TerminalReporter');
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:57:56 +00:00
|
|
|
/* $FlowFixMe: Flow is wrong, Node.js docs specify that process.stdout is an
|
|
|
|
* instance of a net.Socket (a local socket, not network). */
|
|
|
|
const terminal = new Terminal(process.stdout);
|
2016-01-15 03:32:17 +00:00
|
|
|
return ReactPackager.createServer({
|
2016-11-17 04:11:48 +00:00
|
|
|
assetExts: defaultAssetExts.concat(args.assetExts),
|
2015-10-12 23:46:52 +00:00
|
|
|
blacklistRE: config.getBlacklistRE(),
|
|
|
|
cacheVersion: '3',
|
2016-11-16 18:22:23 +00:00
|
|
|
extraNodeModules: config.extraNodeModules,
|
2016-11-28 15:27:09 +00:00
|
|
|
getTransformOptions: config.getTransformOptions,
|
2017-02-27 18:48:17 +00:00
|
|
|
hasteImpl: config.hasteImpl,
|
2016-12-02 17:41:28 +00:00
|
|
|
platforms: defaultPlatforms.concat(args.platforms),
|
2017-05-17 11:56:03 +00:00
|
|
|
polyfillModuleNames: config.getPolyfillModuleNames(),
|
2017-04-28 16:27:19 +00:00
|
|
|
postProcessModules: config.postProcessModules,
|
2017-05-03 10:45:04 +00:00
|
|
|
postMinifyProcess: config.postMinifyProcess,
|
2016-11-17 04:11:48 +00:00
|
|
|
projectRoots: args.projectRoots,
|
2017-01-13 05:03:57 +00:00
|
|
|
providesModuleNodeModules: providesModuleNodeModules,
|
2017-06-01 09:57:56 +00:00
|
|
|
reporter: new LogReporter(terminal),
|
2016-11-16 18:22:23 +00:00
|
|
|
resetCache: args.resetCache,
|
2017-05-04 12:06:27 +00:00
|
|
|
sourceExts: defaultSourceExts.concat(args.sourceExts),
|
2016-11-17 04:11:48 +00:00
|
|
|
transformModulePath: transformModulePath,
|
2015-10-20 18:51:15 +00:00
|
|
|
verbose: args.verbose,
|
2016-11-17 04:11:48 +00:00
|
|
|
watch: !args.nonPersistent,
|
2017-05-30 11:39:43 +00:00
|
|
|
workerPath: config.getWorkerPath(),
|
2015-10-12 23:46:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = runServer;
|