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');
|
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 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-02-26 02:14:55 +00:00
|
|
|
var ms = 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()))
|
2016-02-26 02:14:55 +00:00
|
|
|
.use(getDevToolsMiddleware(args, () => ms && ms.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');
|
2016-02-26 02:14:55 +00:00
|
|
|
ms = messageSocket.attachToServer(serverInstance, '/message');
|
2015-10-26 19:59:54 +00:00
|
|
|
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,
|
Support non-image assets in packager
Summary:
public
The packager currently assumes that all assets that are not JSON or JS files must be images. Although it is possible to add other extension types, they crash the packager if you try to require them, because it attempts to get their dimensions, assuming that they are an image.
This is a crude workaround for that problem, which skips the image-specific processing for non-image assets, but really it would be better if the packager was properly aware of different asset types and treated them differently (e.g. for sounds it could include the duration, for HTML pages it could parse and include linked CSS files, etc).
I've also added an example of using `require('...')` to load a packager-managed HTML page in the UIExplorer WebView example. In future I anticipate that all static asset types (sounds, fonts, etc.) could be handled in this way, which allows them to be edited or added/removed on the fly instead of needing to restart the app.
Reviewed By: martinbigio
Differential Revision: D2895619
fb-gh-sync-id: cd93794ca66bad838621cd7df3ff3c62b5645e85
2016-02-04 01:30:01 +00:00
|
|
|
assetExts: [
|
|
|
|
'bmp', 'gif', 'jpg', 'jpeg', 'png', 'psd', 'svg', 'webp', // Image formats
|
|
|
|
'm4v', 'mov', 'mp4', 'mpeg', 'mpg', 'webm', // Video formats
|
|
|
|
'aac', 'aiff', 'caf', 'm4a', 'mp3', 'wav', // Audio formats
|
|
|
|
'html', // Document formats
|
|
|
|
],
|
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;
|