Allow to attach the HMR server to an external http server

Reviewed By: BYK

Differential Revision: D6752279

fbshipit-source-id: 250255a3b1226f8aab6054e507cb3910cdcd6a85
This commit is contained in:
Rafael Oleza 2018-01-19 07:59:19 -08:00 committed by Facebook Github Bot
parent 46545d4f5c
commit 429b91c382
4 changed files with 25 additions and 13 deletions

View File

@ -17,7 +17,6 @@ const {EventEmitter} = require('events');
describe('HmrServer', () => { describe('HmrServer', () => {
let hmrServer; let hmrServer;
let reporterMock;
let serverMock; let serverMock;
let deltaBundlerMock; let deltaBundlerMock;
let deltaTransformerMock; let deltaTransformerMock;
@ -41,12 +40,14 @@ describe('HmrServer', () => {
getDeltaBundler() { getDeltaBundler() {
return deltaBundlerMock; return deltaBundlerMock;
}, },
}; getReporter() {
reporterMock = { return {
update: jest.fn(), update: jest.fn(),
};
},
}; };
hmrServer = new HmrServer(serverMock, reporterMock); hmrServer = new HmrServer(serverMock);
}); });
it('should pass the correct options to the delta bundler', async () => { it('should pass the correct options to the delta bundler', async () => {

View File

@ -44,9 +44,9 @@ class HmrServer<TClient: Client> {
_packagerServer: PackagerServer; _packagerServer: PackagerServer;
_reporter: Reporter; _reporter: Reporter;
constructor(packagerServer: PackagerServer, reporter: Reporter) { constructor(packagerServer: PackagerServer) {
this._packagerServer = packagerServer; this._packagerServer = packagerServer;
this._reporter = reporter; this._reporter = packagerServer.getReporter();
} }
async onClientConnect( async onClientConnect(

View File

@ -865,6 +865,10 @@ class Server {
return (this._nextBundleBuildID++).toString(36); return (this._nextBundleBuildID++).toString(36);
} }
getReporter(): Reporter {
return this._reporter;
}
static DEFAULT_BUNDLE_OPTIONS = { static DEFAULT_BUNDLE_OPTIONS = {
assetPlugins: [], assetPlugins: [],
dev: true, dev: true,

View File

@ -166,6 +166,13 @@ exports.createConnectMiddleware = async function(
: Config.DEFAULT; : Config.DEFAULT;
return { return {
attachHmrServer(httpServer: HttpServer | HttpsServer) {
attachWebsocketServer({
httpServer,
path: '/hot',
websocketServer: new MetroHmrServer(metroServer),
});
},
metroServer, metroServer,
middleware: normalizedConfig.enhanceMiddleware(metroServer.processRequest), middleware: normalizedConfig.enhanceMiddleware(metroServer.processRequest),
end() { end() {
@ -195,7 +202,11 @@ exports.runServer = async (options: RunServerOptions) => {
const serverApp = connect(); const serverApp = connect();
const {metroServer, middleware, end} = await exports.createConnectMiddleware({ const {
attachHmrServer,
middleware,
end,
} = await exports.createConnectMiddleware({
config: options.config, config: options.config,
maxWorkers: options.maxWorkers, maxWorkers: options.maxWorkers,
port, port,
@ -221,11 +232,7 @@ exports.runServer = async (options: RunServerOptions) => {
} }
if (options.hmrEnabled) { if (options.hmrEnabled) {
attachWebsocketServer({ attachHmrServer(httpServer);
httpServer,
path: '/hot',
websocketServer: new MetroHmrServer(metroServer, reporter),
});
} }
httpServer.listen(port, options.host, () => { httpServer.listen(port, options.host, () => {