Skip to content

Commit 8874d72

Browse files
fix: avoid web socket connection when web socket server is not running (#3879)
1 parent 094c932 commit 8874d72

8 files changed

+105
-46
lines changed

lib/Server.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -1063,42 +1063,43 @@ class Server {
10631063
}
10641064

10651065
async initialize() {
1066-
const compilers = this.compiler.compilers || [this.compiler];
1066+
if (this.options.webSocketServer) {
1067+
const compilers = this.compiler.compilers || [this.compiler];
10671068

1068-
// eslint-disable-next-line no-shadow
1069-
compilers.forEach((compiler) => {
1070-
this.addAdditionalEntries(compiler);
1069+
// eslint-disable-next-line no-shadow
1070+
compilers.forEach((compiler) => {
1071+
this.addAdditionalEntries(compiler);
10711072

1072-
const webpack = compiler.webpack || require("webpack");
1073+
const webpack = compiler.webpack || require("webpack");
10731074

1074-
const providePlugin = new webpack.ProvidePlugin({
1075-
__webpack_dev_server_client__: this.getClientTransport(),
1076-
});
1075+
new webpack.ProvidePlugin({
1076+
__webpack_dev_server_client__: this.getClientTransport(),
1077+
}).apply(compiler);
10771078

1078-
providePlugin.apply(compiler);
1079+
// TODO remove after drop webpack v4 support
1080+
compiler.options.plugins = compiler.options.plugins || [];
10791081

1080-
// TODO remove after drop webpack v4 support
1081-
compiler.options.plugins = compiler.options.plugins || [];
1082+
if (this.options.hot) {
1083+
const HMRPluginExists = compiler.options.plugins.find(
1084+
(p) => p.constructor === webpack.HotModuleReplacementPlugin
1085+
);
10821086

1083-
if (this.options.hot) {
1084-
const HMRPluginExists = compiler.options.plugins.find(
1085-
(p) => p.constructor === webpack.HotModuleReplacementPlugin
1086-
);
1087+
if (HMRPluginExists) {
1088+
this.logger.warn(
1089+
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
1090+
);
1091+
} else {
1092+
// Apply the HMR plugin
1093+
const plugin = new webpack.HotModuleReplacementPlugin();
10871094

1088-
if (HMRPluginExists) {
1089-
this.logger.warn(
1090-
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
1091-
);
1092-
} else {
1093-
// apply the HMR plugin
1094-
const plugin = new webpack.HotModuleReplacementPlugin();
1095-
plugin.apply(compiler);
1095+
plugin.apply(compiler);
1096+
}
10961097
}
1097-
}
1098-
});
1098+
});
10991099

1100-
if (this.options.client && this.options.client.progress) {
1101-
this.setupProgressPlugin();
1100+
if (this.options.client && this.options.client.progress) {
1101+
this.setupProgressPlugin();
1102+
}
11021103
}
11031104

11041105
this.setupHooks();
@@ -2065,7 +2066,6 @@ class Server {
20652066
};
20662067

20672068
const chokidar = require("chokidar");
2068-
20692069
const watcher = chokidar.watch(watchPath, finalWatchOptions);
20702070

20712071
// disabling refreshing on changing the content

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ Array [
5959

6060
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 []`;
6161

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-
`;
62+
exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `Array []`;
7063

7164
exports[`hot and live reload should work and do nothing when web socket server disabled (default): page errors 1`] = `Array []`;
7265

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ Array [
5959

6060
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 []`;
6161

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-
`;
62+
exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `Array []`;
7063

7164
exports[`hot and live reload should work and do nothing when web socket server disabled (default): page errors 1`] = `Array []`;
7265

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`web socket server should work allow to disable: console messages 1`] = `
4+
Array [
5+
"Hey.",
6+
]
7+
`;
8+
9+
exports[`web socket server should work allow to disable: page errors 1`] = `Array []`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`web socket server should work allow to disable: console messages 1`] = `
4+
Array [
5+
"Hey.",
6+
]
7+
`;
8+
9+
exports[`web socket server should work allow to disable: page errors 1`] = `Array []`;

test/e2e/progress.test.js

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ describe("progress", () => {
2020
const compiler = webpack(reloadConfig);
2121
const devServerOptions = {
2222
port,
23-
static: false,
24-
hot: true,
2523
client: {
2624
progress: true,
2725
},

test/e2e/web-socket-server.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const Server = require("../../lib/Server");
5+
const config = require("../fixtures/client-config/webpack.config");
6+
const runBrowser = require("../helpers/run-browser");
7+
const port = require("../ports-map")["web-socket-server-test"];
8+
9+
describe("web socket server", () => {
10+
it("should work allow to disable", async () => {
11+
const devServerPort = port;
12+
13+
const compiler = webpack(config);
14+
const devServerOptions = {
15+
webSocketServer: false,
16+
port: devServerPort,
17+
};
18+
const server = new Server(devServerOptions, compiler);
19+
20+
await server.start();
21+
22+
const { page, browser } = await runBrowser();
23+
24+
const pageErrors = [];
25+
const consoleMessages = [];
26+
27+
page
28+
.on("console", (message) => {
29+
consoleMessages.push(message);
30+
})
31+
.on("pageerror", (error) => {
32+
pageErrors.push(error);
33+
});
34+
35+
const webSocketRequests = [];
36+
37+
const client = page._client;
38+
39+
client.on("Network.webSocketCreated", (test) => {
40+
webSocketRequests.push(test);
41+
});
42+
43+
await page.goto(`http://127.0.0.1:${port}/main`, {
44+
waitUntil: "networkidle0",
45+
});
46+
47+
expect(webSocketRequests).toHaveLength(0);
48+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
49+
"console messages"
50+
);
51+
expect(pageErrors).toMatchSnapshot("page errors");
52+
53+
await browser.close();
54+
await server.stop();
55+
});
56+
});

test/ports-map.js

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const listOfTests = {
7373
"lazy-compilation": 1,
7474
"range-header": 1,
7575
port: 1,
76+
"web-socket-server-test": 1,
7677
};
7778

7879
let startPort = 8089;

0 commit comments

Comments
 (0)