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

View File

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

View File

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

View File

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