Skip to content

Commit 391e17f

Browse files
niicojsdevongovett
authored andcommitted
launch https websocket server is --https (#708)
1 parent 7518805 commit 391e17f

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/Bundler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Bundler extends EventEmitter {
7878
minify:
7979
typeof options.minify === 'boolean' ? options.minify : isProduction,
8080
hmr: typeof options.hmr === 'boolean' ? options.hmr : watch,
81+
https: options.https || false,
8182
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3,
8283
mainFile: this.mainFile,
8384
hmrPort: options.hmrPort || 0,
@@ -248,7 +249,7 @@ class Bundler extends EventEmitter {
248249

249250
if (this.options.hmr) {
250251
this.hmr = new HMRServer();
251-
this.options.hmrPort = await this.hmr.start(this.options.hmrPort);
252+
this.options.hmrPort = await this.hmr.start(this.options);
252253
}
253254

254255
this.farm = WorkerFarm.getShared(this.options);

src/HMRServer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
const http = require('http');
2+
const https = require('https');
13
const WebSocket = require('ws');
24
const prettyError = require('./utils/prettyError');
5+
const generateCertificate = require('./utils/generateCertificate');
36
const logger = require('./Logger');
47

58
class HMRServer {
6-
async start(port) {
9+
async start(options = {}) {
710
await new Promise(resolve => {
8-
this.wss = new WebSocket.Server({port}, resolve);
11+
let server = options.https
12+
? https.createServer(generateCertificate(options))
13+
: http.createServer();
14+
15+
this.wss = new WebSocket.Server({server});
16+
server.listen(options.hmrPort, resolve);
917
});
1018

1119
this.wss.on('connection', ws => {

src/builtins/hmr-runtime.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module.bundle.Module = Module;
1616

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

0 commit comments

Comments
 (0)