diff --git a/local-cli/server/util/attachHMRServer.js b/local-cli/server/util/attachHMRServer.js index f46114142..e0495e3d7 100644 --- a/local-cli/server/util/attachHMRServer.js +++ b/local-cli/server/util/attachHMRServer.js @@ -185,11 +185,23 @@ function attachHMRServer({httpServer, path, packagerServer}) { return; } + const httpServerAddress = httpServer.address(); + + // Sanitize the value from the HTTP server + let packagerHost = 'localhost'; + if (httpServer.address().address && + httpServer.address().address !== '::' && + httpServer.address().address !== '') { + packagerHost = httpServerAddress.address; + } + + let packagerPort = httpServerAddress.port; + return packagerServer.buildBundleForHMR({ entryFile: client.bundleEntry, platform: client.platform, resolutionResponse, - }); + }, packagerHost, packagerPort); }) .then(bundle => { if (!client || !bundle || bundle.isEmpty()) { diff --git a/packager/react-packager/src/Bundler/index.js b/packager/react-packager/src/Bundler/index.js index 22696cf59..9da681d65 100644 --- a/packager/react-packager/src/Bundler/index.js +++ b/packager/react-packager/src/Bundler/index.js @@ -168,9 +168,9 @@ class Bundler { }); } - _sourceHMRURL(platform, path) { + _sourceHMRURL(platform, host, port, path) { return this._hmrURL( - 'http://localhost:8081', // TODO: (martinb) avoid hardcoding + `http://${host}:${port}`, platform, 'bundle', path, @@ -211,10 +211,10 @@ class Bundler { ); } - hmrBundle(options) { + hmrBundle(options, host, port) { return this._bundle({ bundle: new HMRBundle({ - sourceURLFn: this._sourceHMRURL.bind(this, options.platform), + sourceURLFn: this._sourceHMRURL.bind(this, options.platform, host, port), sourceMappingURLFn: this._sourceMappingHMRURL.bind( this, options.platform, diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index 7db64bf74..5949bd0c7 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -240,8 +240,8 @@ class Server { return this.buildBundle(options); } - buildBundleForHMR(modules) { - return this._bundler.hmrBundle(modules); + buildBundleForHMR(modules, host, port) { + return this._bundler.hmrBundle(modules, host, port); } getShallowDependencies(entryFile) {