Skip to content

Commit

Permalink
launch https websocket server is --https (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
niicojs authored and devongovett committed Feb 1, 2018
1 parent 7518805 commit 391e17f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Bundler extends EventEmitter {
minify:
typeof options.minify === 'boolean' ? options.minify : isProduction,
hmr: typeof options.hmr === 'boolean' ? options.hmr : watch,
https: options.https || false,
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3,
mainFile: this.mainFile,
hmrPort: options.hmrPort || 0,
Expand Down Expand Up @@ -248,7 +249,7 @@ class Bundler extends EventEmitter {

if (this.options.hmr) {
this.hmr = new HMRServer();
this.options.hmrPort = await this.hmr.start(this.options.hmrPort);
this.options.hmrPort = await this.hmr.start(this.options);
}

this.farm = WorkerFarm.getShared(this.options);
Expand Down
12 changes: 10 additions & 2 deletions src/HMRServer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const http = require('http');
const https = require('https');
const WebSocket = require('ws');
const prettyError = require('./utils/prettyError');
const generateCertificate = require('./utils/generateCertificate');
const logger = require('./Logger');

class HMRServer {
async start(port) {
async start(options = {}) {
await new Promise(resolve => {
this.wss = new WebSocket.Server({port}, resolve);
let server = options.https
? https.createServer(generateCertificate(options))
: http.createServer();

this.wss = new WebSocket.Server({server});
server.listen(options.hmrPort, resolve);
});

this.wss.on('connection', ws => {
Expand Down
3 changes: 2 additions & 1 deletion src/builtins/hmr-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.bundle.Module = Module;

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

Expand Down

0 comments on commit 391e17f

Please sign in to comment.