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.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
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');
|
2015-10-26 19:59:54 +00:00
|
|
|
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
|
|
|
|
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
|
2015-10-12 23:46:52 +00:00
|
|
|
const http = require('http');
|
|
|
|
const isAbsolutePath = require('absolute-path');
|
2015-10-26 19:59:54 +00:00
|
|
|
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
|
|
|
|
const openStackFrameInEditorMiddleware = require('./middleware/openStackFrameInEditorMiddleware');
|
2015-10-12 23:46:52 +00:00
|
|
|
const path = require('path');
|
2015-10-26 14:55:29 +00:00
|
|
|
const ReactPackager = require('../../packager/react-packager');
|
2015-10-26 19:59:54 +00:00
|
|
|
const statusPageMiddleware = require('./middleware/statusPageMiddleware.js');
|
|
|
|
const systraceProfileMiddleware = require('./middleware/systraceProfileMiddleware.js');
|
|
|
|
const webSocketProxy = require('./util/webSocketProxy.js');
|
2015-10-12 23:46:52 +00:00
|
|
|
|
|
|
|
function runServer(args, config, readyCallback) {
|
2015-10-26 19:59:54 +00:00
|
|
|
var wsProxy = null;
|
2016-01-15 03:32:17 +00:00
|
|
|
const packagerServer = getPackagerServer(args, config);
|
2015-10-12 23:46:52 +00:00
|
|
|
const app = connect()
|
|
|
|
.use(loadRawBodyMiddleware)
|
2015-11-17 20:20:31 +00:00
|
|
|
.use(connect.compress())
|
2015-10-26 19:59:54 +00:00
|
|
|
.use(getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()))
|
2015-10-12 23:46:52 +00:00
|
|
|
.use(openStackFrameInEditorMiddleware)
|
|
|
|
.use(statusPageMiddleware)
|
|
|
|
.use(systraceProfileMiddleware)
|
|
|
|
.use(cpuProfilerMiddleware)
|
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)));
|
|
|
|
|
|
|
|
app.use(connect.logger())
|
|
|
|
.use(connect.errorHandler());
|
|
|
|
|
2015-10-26 19:59:54 +00:00
|
|
|
const serverInstance = http.createServer(app).listen(
|
|
|
|
args.port,
|
2016-01-20 00:47:01 +00:00
|
|
|
args.host,
|
2015-10-26 19:59:54 +00:00
|
|
|
function() {
|
2016-01-15 03:32:17 +00:00
|
|
|
attachHMRServer({
|
|
|
|
httpServer: serverInstance,
|
|
|
|
path: '/hot',
|
|
|
|
packagerServer,
|
|
|
|
});
|
|
|
|
|
2015-10-26 19:59:54 +00:00
|
|
|
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
|
|
|
|
webSocketProxy.attachToServer(serverInstance, '/devtools');
|
|
|
|
readyCallback();
|
|
|
|
}
|
|
|
|
);
|
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) {
|
2015-10-12 23:46:52 +00:00
|
|
|
let transformerPath = args.transformer;
|
|
|
|
if (!isAbsolutePath(transformerPath)) {
|
|
|
|
transformerPath = path.resolve(process.cwd(), transformerPath);
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:32:17 +00:00
|
|
|
return ReactPackager.createServer({
|
2015-10-12 23:46:52 +00:00
|
|
|
nonPersistent: args.nonPersistent,
|
|
|
|
projectRoots: args.projectRoots,
|
|
|
|
blacklistRE: config.getBlacklistRE(),
|
|
|
|
cacheVersion: '3',
|
2015-12-24 09:01:18 +00:00
|
|
|
getTransformOptionsModulePath: config.getTransformOptionsModulePath,
|
2015-10-12 23:46:52 +00:00
|
|
|
transformModulePath: transformerPath,
|
|
|
|
assetRoots: args.assetRoots,
|
2015-11-04 01:42:15 +00:00
|
|
|
assetExts: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp'],
|
2015-10-12 23:46:52 +00:00
|
|
|
resetCache: args.resetCache || args['reset-cache'],
|
2015-10-20 18:51:15 +00:00
|
|
|
verbose: args.verbose,
|
2015-10-12 23:46:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = runServer;
|