diff --git a/packages/metro/src/HmrServer/__tests__/HmrServer-test.js b/packages/metro/src/HmrServer/__tests__/HmrServer-test.js index ac926db4..ec03ed39 100644 --- a/packages/metro/src/HmrServer/__tests__/HmrServer-test.js +++ b/packages/metro/src/HmrServer/__tests__/HmrServer-test.js @@ -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 () => { diff --git a/packages/metro/src/HmrServer/index.js b/packages/metro/src/HmrServer/index.js index 0791bafa..03ae0fd9 100644 --- a/packages/metro/src/HmrServer/index.js +++ b/packages/metro/src/HmrServer/index.js @@ -44,9 +44,9 @@ class HmrServer { _packagerServer: PackagerServer; _reporter: Reporter; - constructor(packagerServer: PackagerServer, reporter: Reporter) { + constructor(packagerServer: PackagerServer) { this._packagerServer = packagerServer; - this._reporter = reporter; + this._reporter = packagerServer.getReporter(); } async onClientConnect( diff --git a/packages/metro/src/Server/index.js b/packages/metro/src/Server/index.js index bade0194..92b004b6 100644 --- a/packages/metro/src/Server/index.js +++ b/packages/metro/src/Server/index.js @@ -865,6 +865,10 @@ class Server { return (this._nextBundleBuildID++).toString(36); } + getReporter(): Reporter { + return this._reporter; + } + static DEFAULT_BUNDLE_OPTIONS = { assetPlugins: [], dev: true, diff --git a/packages/metro/src/index.js b/packages/metro/src/index.js index 943bd1d6..b6ea638b 100644 --- a/packages/metro/src/index.js +++ b/packages/metro/src/index.js @@ -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, () => {