Skip to content

Commit f62f20f

Browse files
feat: allow to disable web socket server
1 parent 4a48ae6 commit f62f20f

17 files changed

+153
-87
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Options:
159159
--watch-files-reset Clear all items provided in 'watchFiles' configuration. Allows to configure list of globs/directories/files
160160
to watch for file changes.
161161
--web-socket-server <value> Allows to set web socket server and options (by default 'ws').
162+
--no-web-socket-server Negative 'web-socket-server' option.
162163
163164
Global options:
164165
--color Enable colors on console.

bin/cli-flags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ module.exports = {
298298
configs: [
299299
{
300300
type: 'enum',
301-
values: ['sockjs', 'ws'],
301+
values: [false, 'sockjs', 'ws'],
302302
multiple: false,
303303
description:
304304
"Allows to set web socket server and options (by default 'ws').",

lib/Server.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,7 @@ class Server {
671671
}
672672

673673
createWebSocketServer() {
674-
this.webSocketServerImplementation =
675-
this.getWebSocketServerImplementation();
676-
// eslint-disable-next-line new-cap
677-
this.webSocketServer = new this.webSocketServerImplementation(this);
678-
674+
this.webSocketServer = new (this.getWebSocketServerImplementation())(this);
679675
this.webSocketServer.implementation.on('connection', (client, request) => {
680676
const headers =
681677
// eslint-disable-next-line no-nested-ternary
@@ -1037,8 +1033,14 @@ class Server {
10371033
fs.chmodSync(this.options.ipc, READ_WRITE);
10381034
}
10391035

1040-
if (Boolean(this.options.hot) || this.options.liveReload) {
1041-
this.createWebSocketServer();
1036+
if (this.options.webSocketServer) {
1037+
try {
1038+
this.createWebSocketServer();
1039+
} catch (webSocketServerError) {
1040+
fn.call(this.server, webSocketServerError);
1041+
1042+
return;
1043+
}
10421044
}
10431045

10441046
if (this.options.bonjour) {

lib/options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@
635635
"link": "https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver"
636636
},
637637
"WebSocketServerEnum": {
638-
"enum": ["sockjs", "ws"]
638+
"enum": [false, "sockjs", "ws"]
639639
},
640640
"WebSocketServerFunction": {
641641
"instanceof": "Function"

lib/utils/DevServerPlugin.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class DevServerPlugin {
1818
let clientImplementationFound = true;
1919

2020
const isKnownWebSocketServerImplementation =
21+
this.options.webSocketServer &&
2122
typeof this.options.webSocketServer.type === 'string' &&
2223
(this.options.webSocketServer.type === 'ws' ||
2324
this.options.webSocketServer.type === 'sockjs');
@@ -29,6 +30,8 @@ class DevServerPlugin {
2930
clientTransport = this.options.client.webSocketTransport;
3031
} else if (isKnownWebSocketServerImplementation) {
3132
clientTransport = this.options.webSocketServer.type;
33+
} else {
34+
clientTransport = 'ws';
3235
}
3336
} else {
3437
clientTransport = 'ws';
@@ -186,8 +189,10 @@ class DevServerPlugin {
186189
* @returns {string}
187190
*/
188191
getClientEntry() {
189-
const webSocketURL = this.getWebSocketURL();
190192
/** @type {string} */
193+
const webSocketURL = this.options.webSocketServer
194+
? this.getWebSocketURL()
195+
: '';
191196

192197
return `${require.resolve('../../client/index.js')}?${webSocketURL}`;
193198
}
@@ -243,11 +248,8 @@ class DevServerPlugin {
243248

244249
const additionalEntries = [];
245250

246-
if (
247-
this.options.client &&
248-
this.isWebTarget(compiler.options) &&
249-
(Boolean(this.options.hot) || this.options.liveReload)
250-
) {
251+
// TODO maybe empty empty client
252+
if (this.options.client && this.isWebTarget(compiler.options)) {
251253
const clientEntry = this.getClientEntry();
252254

253255
additionalEntries.push(clientEntry);

lib/utils/normalizeOptions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ function normalizeOptions(compiler, options, logger, cacheDir) {
409409
type: defaultWebSocketServerType,
410410
options: defaultWebSocketServerOptions,
411411
};
412+
} else if (
413+
typeof options.webSocketServer === 'boolean' &&
414+
!options.webSocketServer
415+
) {
416+
options.webSocketServer = false;
412417
} else if (
413418
typeof options.webSocketServer === 'string' ||
414419
typeof options.webSocketServer === 'function'

test/__snapshots__/validate-options.test.js.snap.webpack4

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,43 +666,43 @@ exports[`options validate should throw an error on the "webSocketServer" option
666666
exports[`options validate should throw an error on the "webSocketServer" option with '{"type":true}' value 1`] = `
667667
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
668668
- options.webSocketServer should be one of these:
669-
\\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
669+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
670670
-> Allows to set web socket server and options (by default 'ws').
671671
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver
672672
Details:
673673
* options.webSocketServer.type should be one of these:
674-
\\"sockjs\\" | \\"ws\\" | non-empty string | function
674+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function
675675
Details:
676676
* options.webSocketServer.type should be one of these:
677-
\\"sockjs\\" | \\"ws\\"
677+
false | \\"sockjs\\" | \\"ws\\"
678678
* options.webSocketServer.type should be a non-empty string.
679679
* options.webSocketServer.type should be an instance of function."
680680
`;
681681

682-
exports[`options validate should throw an error on the "webSocketServer" option with 'false' value 1`] = `
682+
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
683683
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
684684
- options.webSocketServer should be one of these:
685-
\\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
685+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
686686
-> Allows to set web socket server and options (by default 'ws').
687687
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver
688688
Details:
689689
* options.webSocketServer should be one of these:
690-
\\"sockjs\\" | \\"ws\\"
690+
false | \\"sockjs\\" | \\"ws\\"
691691
* options.webSocketServer should be a non-empty string.
692692
* options.webSocketServer should be an instance of function.
693693
* options.webSocketServer should be an object:
694694
object { type?, options? }"
695695
`;
696696

697-
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
697+
exports[`options validate should throw an error on the "webSocketServer" option with 'true' value 1`] = `
698698
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
699699
- options.webSocketServer should be one of these:
700-
\\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
700+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
701701
-> Allows to set web socket server and options (by default 'ws').
702702
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver
703703
Details:
704704
* options.webSocketServer should be one of these:
705-
\\"sockjs\\" | \\"ws\\"
705+
false | \\"sockjs\\" | \\"ws\\"
706706
* options.webSocketServer should be a non-empty string.
707707
* options.webSocketServer should be an instance of function.
708708
* options.webSocketServer should be an object:

test/__snapshots__/validate-options.test.js.snap.webpack5

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,43 +666,43 @@ exports[`options validate should throw an error on the "webSocketServer" option
666666
exports[`options validate should throw an error on the "webSocketServer" option with '{"type":true}' value 1`] = `
667667
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
668668
- options.webSocketServer should be one of these:
669-
\\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
669+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
670670
-> Allows to set web socket server and options (by default 'ws').
671671
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver
672672
Details:
673673
* options.webSocketServer.type should be one of these:
674-
\\"sockjs\\" | \\"ws\\" | non-empty string | function
674+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function
675675
Details:
676676
* options.webSocketServer.type should be one of these:
677-
\\"sockjs\\" | \\"ws\\"
677+
false | \\"sockjs\\" | \\"ws\\"
678678
* options.webSocketServer.type should be a non-empty string.
679679
* options.webSocketServer.type should be an instance of function."
680680
`;
681681

682-
exports[`options validate should throw an error on the "webSocketServer" option with 'false' value 1`] = `
682+
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
683683
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
684684
- options.webSocketServer should be one of these:
685-
\\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
685+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
686686
-> Allows to set web socket server and options (by default 'ws').
687687
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver
688688
Details:
689689
* options.webSocketServer should be one of these:
690-
\\"sockjs\\" | \\"ws\\"
690+
false | \\"sockjs\\" | \\"ws\\"
691691
* options.webSocketServer should be a non-empty string.
692692
* options.webSocketServer should be an instance of function.
693693
* options.webSocketServer should be an object:
694694
object { type?, options? }"
695695
`;
696696

697-
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
697+
exports[`options validate should throw an error on the "webSocketServer" option with 'true' value 1`] = `
698698
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
699699
- options.webSocketServer should be one of these:
700-
\\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
700+
false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? }
701701
-> Allows to set web socket server and options (by default 'ws').
702702
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver
703703
Details:
704704
* options.webSocketServer should be one of these:
705-
\\"sockjs\\" | \\"ws\\"
705+
false | \\"sockjs\\" | \\"ws\\"
706706
* options.webSocketServer should be a non-empty string.
707707
* options.webSocketServer should be an instance of function.
708708
* options.webSocketServer should be an object:

test/cli/__snapshots__/basic.test.js.snap.webpack4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Options:
6161
--client-web-socket-url-username <value> Tells clients connected to devServer to use the provided username to authenticate.
6262
--client-web-socket-url-password <value> Tells clients connected to devServer to use the provided password to authenticate.
6363
--web-socket-server <value> Allows to set web socket server and options (by default 'ws').
64+
--no-web-socket-server Negative 'web-socket-server' option.
6465
--compress Enables gzip compression for everything served.
6566
--no-compress Disables gzip compression for everything served.
6667
--history-api-fallback Allows to proxy requests through a specified index page (by default 'index.html'), useful for Single Page Applications that utilise the HTML5 History API.

test/cli/__snapshots__/basic.test.js.snap.webpack5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Options:
104104
--watch-files <value...> Allows to configure list of globs/directories/files to watch for file changes.
105105
--watch-files-reset Clear all items provided in 'watchFiles' configuration. Allows to configure list of globs/directories/files to watch for file changes.
106106
--web-socket-server <value> Allows to set web socket server and options (by default 'ws').
107+
--no-web-socket-server Negative 'web-socket-server' option.
107108

108109
Global options:
109110
--color Enable colors on console.

test/e2e/__snapshots__/hot-and-live-reload.test.js.snap.webpack4

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`hot and live reload should not refresh content when hot and no live reload disabled (default): console messages 1`] = `Array []`;
3+
exports[`hot and live reload should not refresh content when hot and no live reload disabled (default): console messages 1`] = `
4+
Array [
5+
"[webpack-dev-server] App updated. Recompiling...",
6+
]
7+
`;
48

59
exports[`hot and live reload should not refresh content when hot and no live reload disabled (default): page errors 1`] = `Array []`;
610

7-
exports[`hot and live reload should not refresh content when hot and no live reload disabled (sockjs): console messages 1`] = `Array []`;
11+
exports[`hot and live reload should not refresh content when hot and no live reload disabled (sockjs): console messages 1`] = `
12+
Array [
13+
"[webpack-dev-server] App updated. Recompiling...",
14+
]
15+
`;
816

917
exports[`hot and live reload should not refresh content when hot and no live reload disabled (sockjs): page errors 1`] = `Array []`;
1018

11-
exports[`hot and live reload should not refresh content when hot and no live reload disabled (ws): console messages 1`] = `Array []`;
19+
exports[`hot and live reload should not refresh content when hot and no live reload disabled (ws): console messages 1`] = `
20+
Array [
21+
"[webpack-dev-server] App updated. Recompiling...",
22+
]
23+
`;
1224

1325
exports[`hot and live reload should not refresh content when hot and no live reload disabled (ws): page errors 1`] = `Array []`;
1426

@@ -47,6 +59,17 @@ Array [
4759

4860
exports[`hot and live reload should work and allow to disable live reload using the "webpack-dev-server-live-reload=false" (default): page errors 1`] = `Array []`;
4961

62+
exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `
63+
Array [
64+
"[HMR] Waiting for update signal from WDS...",
65+
"WebSocket connection to 'ws://localhost:8095/ws' failed: Error during WebSocket handshake: Unexpected response code: 404",
66+
"[webpack-dev-server] JSHandle@object",
67+
"[webpack-dev-server] Disconnected!",
68+
]
69+
`;
70+
71+
exports[`hot and live reload should work and do nothing when web socket server disabled (default): page errors 1`] = `Array []`;
72+
5073
exports[`hot and live reload should work and refresh content using hot module replacement (default): console messages 1`] = `
5174
Array [
5275
"[HMR] Waiting for update signal from WDS...",

test/e2e/__snapshots__/hot-and-live-reload.test.js.snap.webpack5

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`hot and live reload should not refresh content when hot and no live reload disabled (default): console messages 1`] = `Array []`;
3+
exports[`hot and live reload should not refresh content when hot and no live reload disabled (default): console messages 1`] = `
4+
Array [
5+
"[webpack-dev-server] App updated. Recompiling...",
6+
]
7+
`;
48

59
exports[`hot and live reload should not refresh content when hot and no live reload disabled (default): page errors 1`] = `Array []`;
610

7-
exports[`hot and live reload should not refresh content when hot and no live reload disabled (sockjs): console messages 1`] = `Array []`;
11+
exports[`hot and live reload should not refresh content when hot and no live reload disabled (sockjs): console messages 1`] = `
12+
Array [
13+
"[webpack-dev-server] App updated. Recompiling...",
14+
]
15+
`;
816

917
exports[`hot and live reload should not refresh content when hot and no live reload disabled (sockjs): page errors 1`] = `Array []`;
1018

11-
exports[`hot and live reload should not refresh content when hot and no live reload disabled (ws): console messages 1`] = `Array []`;
19+
exports[`hot and live reload should not refresh content when hot and no live reload disabled (ws): console messages 1`] = `
20+
Array [
21+
"[webpack-dev-server] App updated. Recompiling...",
22+
]
23+
`;
1224

1325
exports[`hot and live reload should not refresh content when hot and no live reload disabled (ws): page errors 1`] = `Array []`;
1426

@@ -47,6 +59,17 @@ Array [
4759

4860
exports[`hot and live reload should work and allow to disable live reload using the "webpack-dev-server-live-reload=false" (default): page errors 1`] = `Array []`;
4961

62+
exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `
63+
Array [
64+
"[HMR] Waiting for update signal from WDS...",
65+
"WebSocket connection to 'ws://localhost:8095/ws' failed: Error during WebSocket handshake: Unexpected response code: 404",
66+
"[webpack-dev-server] JSHandle@object",
67+
"[webpack-dev-server] Disconnected!",
68+
]
69+
`;
70+
71+
exports[`hot and live reload should work and do nothing when web socket server disabled (default): page errors 1`] = `Array []`;
72+
5073
exports[`hot and live reload should work and refresh content using hot module replacement (default): console messages 1`] = `
5174
Array [
5275
"[HMR] Waiting for update signal from WDS...",

test/e2e/__snapshots__/web-socket-server-and-transport.test.js.snap.webpack4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`web socket server and transport should throw an error on wrong path 1`] = `"When you use custom web socket implementation you must explicitly specify client.webSocketTransport. client.webSocketTransport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class "`;
3+
exports[`web socket server and transport should throw an error on wrong path 1`] = `"webSocketServer (webSocketServer.type) must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer.js) via require.resolve(...), or the class itself which extends BaseServer"`;
44

55
exports[`web socket server and transport should use "sockjs" transport and "sockjs" web socket server 1`] = `
66
Array [

test/e2e/__snapshots__/web-socket-server-and-transport.test.js.snap.webpack5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`web socket server and transport should throw an error on wrong path 1`] = `"When you use custom web socket implementation you must explicitly specify client.webSocketTransport. client.webSocketTransport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class "`;
3+
exports[`web socket server and transport should throw an error on wrong path 1`] = `"webSocketServer (webSocketServer.type) must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer.js) via require.resolve(...), or the class itself which extends BaseServer"`;
44

55
exports[`web socket server and transport should use "sockjs" transport and "sockjs" web socket server 1`] = `
66
Array [

0 commit comments

Comments
 (0)