diff --git a/src/Bundler.js b/src/Bundler.js index de60f3d1f97..08ee747ee88 100644 --- a/src/Bundler.js +++ b/src/Bundler.js @@ -64,7 +64,8 @@ class Bundler extends EventEmitter { typeof options.minify === 'boolean' ? options.minify : isProduction, hmr: typeof options.hmr === 'boolean' ? options.hmr : watch, logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3, - mainFile: this.mainFile + mainFile: this.mainFile, + hmrPort: options.hmrPort || 0 }; } @@ -191,7 +192,7 @@ class Bundler extends EventEmitter { if (this.options.hmr) { this.hmr = new HMRServer(); - this.options.hmrPort = await this.hmr.start(); + this.options.hmrPort = await this.hmr.start(this.options.hmrPort); } } diff --git a/src/HMRServer.js b/src/HMRServer.js index 8ce9b79832d..620d34eabd5 100644 --- a/src/HMRServer.js +++ b/src/HMRServer.js @@ -2,9 +2,9 @@ const WebSocket = require('ws'); const prettyError = require('./utils/prettyError'); class HMRServer { - async start() { + async start(port) { await new Promise(resolve => { - this.wss = new WebSocket.Server({port: 0}, resolve); + this.wss = new WebSocket.Server({port}, resolve); }); this.wss.on('connection', ws => { diff --git a/src/cli.js b/src/cli.js index add30aa5ea3..9dc5f71198f 100755 --- a/src/cli.js +++ b/src/cli.js @@ -13,6 +13,11 @@ program 'set the port to serve on. defaults to 1234', parseInt ) + .option( + '-h, --hmr-port ', + 'set the port to serve HMR websockets, defaults to random', + parseInt + ) .option('--https', 'serves files over HTTPS') .option('-o, --open', 'automatically open in default browser') .option(