Skip to content

Commit

Permalink
Add ability to specify HMR hostname (#492)
Browse files Browse the repository at this point in the history
* Add ability to specify HMR hostname

* Add --hmr-hostname option for cli.js
  • Loading branch information
suevalov authored and devongovett committed Jan 15, 2018
1 parent ee654c0 commit af9102f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/core/parcel-bundler/src/Bundler.js
Expand Up @@ -67,7 +67,8 @@ class Bundler extends EventEmitter {
hmr: typeof options.hmr === 'boolean' ? options.hmr : watch,
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3,
mainFile: this.mainFile,
hmrPort: options.hmrPort || 0
hmrPort: options.hmrPort || 0,
hmrHostname: options.hmrHostname || ''
};
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/parcel-bundler/src/builtins/hmr-runtime.js
Expand Up @@ -15,7 +15,8 @@ function Module() {
module.bundle.Module = Module;

if (!module.bundle.parent && typeof WebSocket !== 'undefined') {
var ws = new WebSocket('ws://' + location.hostname + ':{{HMR_PORT}}/');
var hostname = '{{HMR_HOSTNAME}}' || location.hostname;
var ws = new WebSocket('ws://' + hostname + ':{{HMR_PORT}}/');
ws.onmessage = function(event) {
var data = JSON.parse(event.data);

Expand Down
4 changes: 4 additions & 0 deletions packages/core/parcel-bundler/src/cli.js
Expand Up @@ -18,6 +18,10 @@ program
'set the port to serve HMR websockets, defaults to random',
parseInt
)
.option(
'--hmr-hostname <hostname>',
'set the hostname of HMR websockets, defaults to location.hostname of current window'
)
.option('--https', 'serves files over HTTPS')
.option('-o, --open', 'automatically open in default browser')
.option(
Expand Down
4 changes: 3 additions & 1 deletion packages/core/parcel-bundler/src/packagers/JSPackager.js
Expand Up @@ -75,7 +75,9 @@ class JSPackager extends Packager {
// Asset ids normally start at 1, so this should be safe.
await this.writeModule(
0,
hmr.replace('{{HMR_PORT}}', this.options.hmrPort)
hmr
.replace('{{HMR_PORT}}', this.options.hmrPort)
.replace('{{HMR_HOSTNAME}}', this.options.hmrHostname)
);
entry.push(0);
}
Expand Down

0 comments on commit af9102f

Please sign in to comment.