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-11-29 11:00:49 +00:00
|
|
|
const HmrServer = require('metro/src/HmrServer');
|
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-06-04 09:22:44 +00:00
|
|
|
const attachWebsocketServer = require('metro/src/lib/attachWebsocketServer');
|
2018-01-17 11:30:04 +00:00
|
|
|
const compression = require('compression');
|
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');
|
2017-11-29 12:37:22 +00:00
|
|
|
const defaultAssetExts = Metro.defaults.assetExts;
|
|
|
|
const defaultSourceExts = Metro.defaults.sourceExts;
|
|
|
|
const defaultPlatforms = Metro.defaults.platforms;
|
|
|
|
const defaultProvidesModuleNodeModules =
|
|
|
|
Metro.defaults.providesModuleNodeModules;
|
2018-01-17 11:30:04 +00:00
|
|
|
const errorhandler = require('errorhandler');
|
2017-06-12 23:06:36 +00:00
|
|
|
const fs = require('fs');
|
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');
|
2017-06-12 23:06:36 +00:00
|
|
|
const https = require('https');
|
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');
|
2018-01-17 11:30:04 +00:00
|
|
|
const morgan = require('morgan');
|
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');
|
2018-01-17 11:30:04 +00:00
|
|
|
const serveStatic = require('serve-static');
|
2015-10-26 19:59:54 +00:00
|
|
|
const statusPageMiddleware = require('./middleware/statusPageMiddleware.js');
|
|
|
|
const systraceProfileMiddleware = require('./middleware/systraceProfileMiddleware.js');
|
2016-11-17 04:11:48 +00:00
|
|
|
const webSocketProxy = require('./util/webSocketProxy.js');
|
2015-10-12 23:46:52 +00:00
|
|
|
|
2017-07-25 00:04:38 +00:00
|
|
|
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
|
|
|
|
2017-11-29 11:00:49 +00:00
|
|
|
import type {ConfigT} from 'metro';
|
2018-02-19 18:29:01 +00:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 11:00:49 +00:00
|
|
|
import type {Reporter} from 'metro/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,
|
2017-06-13 16:11:57 +00:00
|
|
|
+maxWorkers: number,
|
2017-05-17 11:55:59 +00:00
|
|
|
+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;
|
2017-09-08 14:52:24 +00:00
|
|
|
|
|
|
|
const terminal = new Terminal(process.stdout);
|
2018-03-20 01:24:11 +00:00
|
|
|
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an error
|
|
|
|
* found when Flow v0.68 was deployed. To see the error delete this comment
|
|
|
|
* and run Flow. */
|
2017-09-08 14:52:24 +00:00
|
|
|
const ReporterImpl = getReporterImpl(args.customLogReporterPath || null);
|
|
|
|
const reporter = new ReporterImpl(terminal);
|
|
|
|
const packagerServer = getPackagerServer(args, config, reporter);
|
|
|
|
startedCallback(reporter);
|
2017-03-31 09:10:00 +00:00
|
|
|
|
2015-10-12 23:46:52 +00:00
|
|
|
const app = connect()
|
|
|
|
.use(loadRawBodyMiddleware)
|
2018-01-17 11:30:04 +00:00
|
|
|
.use(compression())
|
2017-09-26 12:07:57 +00:00
|
|
|
.use(
|
|
|
|
'/debugger-ui',
|
2018-01-17 11:30:04 +00:00
|
|
|
serveStatic(path.join(__dirname, 'util', 'debugger-ui')),
|
2017-09-26 12:07:57 +00:00
|
|
|
)
|
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)
|
2016-05-24 18:54:28 +00:00
|
|
|
.use(indexPageMiddleware)
|
2016-01-15 03:32:17 +00:00
|
|
|
.use(packagerServer.processRequest.bind(packagerServer));
|
2015-10-12 23:46:52 +00:00
|
|
|
|
2018-01-17 11:30:04 +00:00
|
|
|
args.projectRoots.forEach(root => app.use(serveStatic(root)));
|
2015-10-12 23:46:52 +00:00
|
|
|
|
2018-01-17 11:30:04 +00:00
|
|
|
app.use(morgan('combined')).use(errorhandler());
|
2015-10-12 23:46:52 +00:00
|
|
|
|
2018-03-20 01:24:11 +00:00
|
|
|
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an error
|
|
|
|
* found when Flow v0.68 was deployed. To see the error delete this comment
|
|
|
|
* and run Flow. */
|
2017-06-12 23:06:36 +00:00
|
|
|
if (args.https && (!args.key || !args.cert)) {
|
|
|
|
throw new Error('Cannot use https without specifying key and cert options');
|
|
|
|
}
|
2016-01-15 03:32:17 +00:00
|
|
|
|
2018-03-20 01:24:11 +00:00
|
|
|
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an error
|
|
|
|
* found when Flow v0.68 was deployed. To see the error delete this comment
|
|
|
|
* and run Flow. */
|
2017-06-12 23:06:36 +00:00
|
|
|
const serverInstance = args.https
|
|
|
|
? https.createServer(
|
|
|
|
{
|
|
|
|
key: fs.readFileSync(args.key),
|
|
|
|
cert: fs.readFileSync(args.cert),
|
|
|
|
},
|
|
|
|
app,
|
|
|
|
)
|
|
|
|
: http.createServer(app);
|
|
|
|
|
|
|
|
serverInstance.listen(args.port, args.host, 511, function() {
|
2017-11-08 17:07:41 +00:00
|
|
|
attachWebsocketServer({
|
2017-06-12 23:06:36 +00:00
|
|
|
httpServer: serverInstance,
|
|
|
|
path: '/hot',
|
2018-01-19 15:59:19 +00:00
|
|
|
websocketServer: new HmrServer(packagerServer),
|
2017-05-17 11:55:59 +00:00
|
|
|
});
|
2017-06-12 23:06:36 +00:00
|
|
|
|
|
|
|
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
|
|
|
|
ms = messageSocket.attachToServer(serverInstance, '/message');
|
2017-09-08 14:52:24 +00:00
|
|
|
readyCallback(reporter);
|
2017-06-12 23:06:36 +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
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPackagerServer(args, config, reporter) {
|
2018-03-20 01:24:11 +00:00
|
|
|
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an error
|
|
|
|
* found when Flow v0.68 was deployed. To see the error delete this comment
|
|
|
|
* and run Flow. */
|
2017-05-17 11:55:59 +00:00
|
|
|
const transformModulePath = args.transformer
|
|
|
|
? path.resolve(args.transformer)
|
2017-07-21 16:24:39 +00:00
|
|
|
: config.getTransformModulePath();
|
2016-06-22 15:08:28 +00:00
|
|
|
|
2017-01-13 05:03:57 +00:00
|
|
|
const providesModuleNodeModules =
|
2018-03-20 01:24:11 +00:00
|
|
|
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an
|
|
|
|
* error found when Flow v0.68 was deployed. To see the error delete this
|
|
|
|
* comment and run Flow. */
|
2017-01-13 05:03:57 +00:00
|
|
|
args.providesModuleNodeModules || defaultProvidesModuleNodeModules;
|
|
|
|
|
2017-11-29 12:37:22 +00:00
|
|
|
return Metro.createServer({
|
2018-04-20 21:03:33 +00:00
|
|
|
asyncRequireModulePath: config.getAsyncRequireModulePath(),
|
2016-11-17 04:11:48 +00:00
|
|
|
assetExts: defaultAssetExts.concat(args.assetExts),
|
2017-07-25 00:04:38 +00:00
|
|
|
assetRegistryPath: ASSET_REGISTRY_PATH,
|
2015-10-12 23:46:52 +00:00
|
|
|
blacklistRE: config.getBlacklistRE(),
|
2018-02-26 11:21:11 +00:00
|
|
|
cacheStores: config.cacheStores,
|
2015-10-12 23:46:52 +00:00
|
|
|
cacheVersion: '3',
|
2017-07-26 22:28:20 +00:00
|
|
|
enableBabelRCLookup: config.getEnableBabelRCLookup(),
|
2016-11-16 18:22:23 +00:00
|
|
|
extraNodeModules: config.extraNodeModules,
|
2018-01-18 15:50:53 +00:00
|
|
|
dynamicDepsInPackages: config.dynamicDepsInPackages,
|
2017-11-07 15:31:39 +00:00
|
|
|
getModulesRunBeforeMainModule: config.getModulesRunBeforeMainModule,
|
2017-07-13 10:30:06 +00:00
|
|
|
getPolyfills: config.getPolyfills,
|
2018-03-20 13:53:49 +00:00
|
|
|
getRunModuleStatement: config.getRunModuleStatement,
|
2016-11-28 15:27:09 +00:00
|
|
|
getTransformOptions: config.getTransformOptions,
|
2018-01-30 19:20:20 +00:00
|
|
|
hasteImplModulePath: config.hasteImplModulePath,
|
2017-06-13 16:11:57 +00:00
|
|
|
maxWorkers: args.maxWorkers,
|
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-05-03 10:45:04 +00:00
|
|
|
postMinifyProcess: config.postMinifyProcess,
|
2017-07-28 00:58:55 +00:00
|
|
|
postProcessBundleSourcemap: config.postProcessBundleSourcemap,
|
2016-11-17 04:11:48 +00:00
|
|
|
projectRoots: args.projectRoots,
|
2017-01-13 05:03:57 +00:00
|
|
|
providesModuleNodeModules: providesModuleNodeModules,
|
2017-09-08 14:52:24 +00:00
|
|
|
reporter,
|
2016-11-16 18:22:23 +00:00
|
|
|
resetCache: args.resetCache,
|
2018-04-05 15:54:52 +00:00
|
|
|
resolveRequest: config.resolveRequest,
|
2018-02-23 16:46:48 +00:00
|
|
|
sourceExts: args.sourceExts.concat(defaultSourceExts),
|
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;
|