Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1719 #2017

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
188 changes: 110 additions & 78 deletions lib/Server.js
Expand Up @@ -85,7 +85,10 @@ class Server {
? this.options.stats
: {};

this.sockets = [];
// Socket map
// key: SocketServer
// value: Socket
this.socketsMap = new Map();
this.contentBaseWatchers = [];

// TODO this.<property> is deprecated (remove them in next major release.) in favor this.options.<property>
Expand Down Expand Up @@ -119,10 +122,12 @@ class Server {
this.setupProgressPlugin();
}

this.initialized = false;
this.setupHooks();
this.setupApp();
this.setupCheckHostRoute();
this.setupDevMiddleware();
this.initialized = true;

// set express routes
routes(this.app, this.middleware, this.options);
Expand All @@ -143,24 +148,31 @@ class Server {
}, this);
}

getCompilers() {
return this.compiler.compilers || [this.compiler];
}

setupProgressPlugin() {
const progressPlugin = new webpack.ProgressPlugin(
(percent, msg, addInfo) => {
percent = Math.floor(percent * 100);
this.getCompilers().forEach(compiler => {
const progressPlugin = new webpack.ProgressPlugin(
(percent, msg, addInfo) => {
percent = Math.floor(percent * 100);

if (percent === 100) {
msg = 'Compilation completed';
}
if (percent === 100) {
msg = 'Compilation completed';
}

if (addInfo) {
msg = `${msg} (${addInfo})`;
if (addInfo) {
msg = `${msg} (${addInfo})`;
}

this.sockWrite(this.getSocketServer(compiler), this.getSockets(compiler), 'progress-update', { percent, msg });
}
);

this.sockWrite(this.sockets, 'progress-update', { percent, msg });
}
);
progressPlugin.apply(compiler);
})

progressPlugin.apply(this.compiler);
}

setupApp() {
Expand All @@ -171,26 +183,25 @@ class Server {

setupHooks() {
// Listening for events
const invalidPlugin = () => {
this.sockWrite(this.sockets, 'invalid');
const invalidPlugin = (compiler) => {
if (!this.initialized) return;
this.sockWrite(this.getSocketServer(compiler), this.getSockets(compiler), 'invalid');
};

const addHooks = (compiler) => {
const { compile, invalid, done } = compiler.hooks;

compile.tap('webpack-dev-server', invalidPlugin);
invalid.tap('webpack-dev-server', invalidPlugin);
compile.tap('webpack-dev-server', invalidPlugin.bind(this, compiler));
invalid.tap('webpack-dev-server', invalidPlugin.bind(this, compiler));
done.tap('webpack-dev-server', (stats) => {
this._sendStats(this.sockets, this.getStats(stats));
this._sendStats(this.getSocketServer(compiler), this.getSockets(compiler), this.getStats(stats));
this._stats = stats;
});
};

if (this.compiler.compilers) {
this.compiler.compilers.forEach(addHooks);
} else {
addHooks(this.compiler);
}
this.getCompilers().forEach(compiler => {
addHooks(compiler);
})
}

setupCheckHostRoute() {
Expand Down Expand Up @@ -664,63 +675,82 @@ class Server {
}
}

createSocketServer() {
createSocketServers() {
const compilers = this.compiler.compilers || [this.compiler];
const SocketServerImplementation = this.socketServerImplementation;
this.socketServer = new SocketServerImplementation(this);
// SocketServer map
// key: Compiler
// value: SocketServer
this.socketServers = new Map();
compilers.forEach(compiler => {
const socketServer = new SocketServerImplementation(this, compiler);
const sockets = [];
this.socketsMap.set(socketServer, sockets);

socketServer.onConnection((connection) => {
if (!connection) {
return;
}

this.socketServer.onConnection((connection) => {
if (!connection) {
return;
}
if (
!this.checkHost(connection.headers) ||
!this.checkOrigin(connection.headers)
) {
this.sockWrite(socketServer, [connection], 'error', 'Invalid Host/Origin header');

if (
!this.checkHost(connection.headers) ||
!this.checkOrigin(connection.headers)
) {
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');
connection.close();

connection.close();
return;
}

return;
}
sockets.push(connection);

this.sockets.push(connection);
connection.on('close', () => {
const idx = sockets.indexOf(connection);

connection.on('close', () => {
const idx = this.sockets.indexOf(connection);
if (idx >= 0) {
sockets.splice(idx, 1);
}
});

if (idx >= 0) {
this.sockets.splice(idx, 1);
if (this.clientLogLevel) {
this.sockWrite(socketServer, [connection], 'log-level', this.clientLogLevel);
}
});

if (this.clientLogLevel) {
this.sockWrite([connection], 'log-level', this.clientLogLevel);
}
if (this.hot) {
this.sockWrite(socketServer, [connection], 'hot');
}

if (this.hot) {
this.sockWrite([connection], 'hot');
}
// TODO: change condition at major version
if (this.options.liveReload !== false) {
this.sockWrite(socketServer, [connection], 'liveReload', this.options.liveReload);
}

// TODO: change condition at major version
if (this.options.liveReload !== false) {
this.sockWrite([connection], 'liveReload', this.options.liveReload);
}
if (this.progress) {
this.sockWrite(socketServer, [connection], 'progress', this.progress);
}

if (this.progress) {
this.sockWrite([connection], 'progress', this.progress);
}
if (this.clientOverlay) {
this.sockWrite(socketServer, [connection], 'overlay', this.clientOverlay);
}

if (this.clientOverlay) {
this.sockWrite([connection], 'overlay', this.clientOverlay);
}
if (!this._stats) {
return;
}

if (!this._stats) {
return;
}
this._sendStats(socketServer, [connection], this.getStats(this._stats), true);
});

this._sendStats([connection], this.getStats(this._stats), true);
});
this.socketServers.set(compiler, socketServer);
})
}

getSocketServer(compiler) {
return this.socketServers.get(compiler);
}

getSockets(compiler) {
return this.socketsMap.get(this.getSocketServer(compiler));
}

showStatus() {
Expand All @@ -742,7 +772,7 @@ class Server {
this.hostname = hostname;

return this.listeningApp.listen(port, hostname, (err) => {
this.createSocketServer();
this.createSocketServers();

if (this.options.bonjour) {
runBonjour(this.options);
Expand All @@ -761,11 +791,11 @@ class Server {
}

close(cb) {
this.sockets.forEach((socket) => {
this.socketServer.close(socket);
this.socketsMap.forEach((sockets, server) => {
sockets.forEach(socket => server.close(socket));
});

this.sockets = [];
this.socketsMap = new Map();

this.contentBaseWatchers.forEach((watcher) => {
watcher.close();
Expand Down Expand Up @@ -906,9 +936,9 @@ class Server {
}

// eslint-disable-next-line
sockWrite(sockets, type, data) {
sockWrite(socketServer, sockets, type, data) {
sockets.forEach((socket) => {
this.socketServer.send(socket, JSON.stringify({ type, data }));
socketServer.send(socket, JSON.stringify({ type, data }));
});
}

Expand Down Expand Up @@ -938,25 +968,25 @@ class Server {
}

// send stats to a socket or multiple sockets
_sendStats(sockets, stats, force) {
_sendStats(socketServer, sockets, stats, force) {
if (
!force &&
stats &&
(!stats.errors || stats.errors.length === 0) &&
stats.assets &&
stats.assets.every((asset) => !asset.emitted)
) {
return this.sockWrite(sockets, 'still-ok');
return this.sockWrite(socketServer, sockets, 'still-ok');
}

this.sockWrite(sockets, 'hash', stats.hash);
this.sockWrite(socketServer, sockets, 'hash', stats.hash);

if (stats.errors.length > 0) {
this.sockWrite(sockets, 'errors', stats.errors);
this.sockWrite(socketServer, sockets, 'errors', stats.errors);
} else if (stats.warnings.length > 0) {
this.sockWrite(sockets, 'warnings', stats.warnings);
this.sockWrite(socketServer, sockets, 'warnings', stats.warnings);
} else {
this.sockWrite(sockets, 'ok');
this.sockWrite(socketServer, sockets, 'ok');
}
}

Expand Down Expand Up @@ -986,7 +1016,9 @@ class Server {
// disabling refreshing on changing the content
if (this.options.liveReload !== false) {
watcher.on('change', () => {
this.sockWrite(this.sockets, 'content-changed');
this.getCompilers().forEach(compiler => {
this.sockWrite(this.getSocketServer(compiler), this.getSockets(compiler), 'content-changed');
})
});
}
this.contentBaseWatchers.push(watcher);
Expand All @@ -1004,4 +1036,4 @@ class Server {
// like task-runners can use it
Server.addDevServerEntrypoints = require('./utils/addEntries');

module.exports = Server;
module.exports = Server;
3 changes: 2 additions & 1 deletion lib/servers/BaseServer.js
Expand Up @@ -3,7 +3,8 @@
// base class that users should extend if they are making their own
// server implementation
module.exports = class BaseServer {
constructor(server) {
constructor(server, compiler) {
this.server = server;
this.compiler = compiler;
}
};
7 changes: 3 additions & 4 deletions lib/servers/SockJSServer.js
Expand Up @@ -29,9 +29,10 @@ const BaseServer = require('./BaseServer');

module.exports = class SockJSServer extends BaseServer {
// options has: error (function), debug (function), server (http/s server), path (string)
constructor(server) {
constructor(server, compiler) {
super(server);
this.socket = sockjs.createServer({
prefix: `/${compiler.options.name}`,
// Use provided up-to-date sockjs-client
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
// Limit useless logs
Expand All @@ -44,9 +45,7 @@ module.exports = class SockJSServer extends BaseServer {
},
});

this.socket.installHandlers(this.server.listeningApp, {
prefix: this.server.sockPath,
});
this.socket.installHandlers(this.server.listeningApp);
}

send(connection, message) {
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/guid.js
@@ -0,0 +1,10 @@
'use strict';

function guid() {
function s4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
}

module.exports = guid;