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: avoid connection when web socket server is not running #3879

Merged
merged 4 commits into from Sep 25, 2021
Merged
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
56 changes: 28 additions & 28 deletions lib/Server.js
Expand Up @@ -1063,42 +1063,43 @@ class Server {
}

async initialize() {
const compilers = this.compiler.compilers || [this.compiler];
if (this.options.webSocketServer) {
const compilers = this.compiler.compilers || [this.compiler];

// eslint-disable-next-line no-shadow
compilers.forEach((compiler) => {
this.addAdditionalEntries(compiler);
// eslint-disable-next-line no-shadow
compilers.forEach((compiler) => {
this.addAdditionalEntries(compiler);

const webpack = compiler.webpack || require("webpack");
const webpack = compiler.webpack || require("webpack");

const providePlugin = new webpack.ProvidePlugin({
__webpack_dev_server_client__: this.getClientTransport(),
});
new webpack.ProvidePlugin({
__webpack_dev_server_client__: this.getClientTransport(),
}).apply(compiler);

providePlugin.apply(compiler);
// TODO remove after drop webpack v4 support
compiler.options.plugins = compiler.options.plugins || [];

// TODO remove after drop webpack v4 support
compiler.options.plugins = compiler.options.plugins || [];
if (this.options.hot) {
const HMRPluginExists = compiler.options.plugins.find(
(p) => p.constructor === webpack.HotModuleReplacementPlugin
);

if (this.options.hot) {
const HMRPluginExists = compiler.options.plugins.find(
(p) => p.constructor === webpack.HotModuleReplacementPlugin
);
if (HMRPluginExists) {
this.logger.warn(
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
);
} else {
// Apply the HMR plugin
const plugin = new webpack.HotModuleReplacementPlugin();

if (HMRPluginExists) {
this.logger.warn(
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
);
} else {
// apply the HMR plugin
const plugin = new webpack.HotModuleReplacementPlugin();
plugin.apply(compiler);
plugin.apply(compiler);
}
}
}
});
});

if (this.options.client && this.options.client.progress) {
this.setupProgressPlugin();
if (this.options.client && this.options.client.progress) {
this.setupProgressPlugin();
}
}

this.setupHooks();
Expand Down Expand Up @@ -2045,7 +2046,6 @@ class Server {
};

const chokidar = require("chokidar");

const watcher = chokidar.watch(watchPath, finalWatchOptions);

// disabling refreshing on changing the content
Expand Down
Expand Up @@ -59,14 +59,7 @@ Array [

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

exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"WebSocket connection to 'ws://localhost:8095/ws' failed: Error during WebSocket handshake: Unexpected response code: 404",
"[webpack-dev-server] JSHandle@object",
"[webpack-dev-server] Disconnected!",
]
`;
exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `Array []`;

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

Expand Down
Expand Up @@ -59,14 +59,7 @@ Array [

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

exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"WebSocket connection to 'ws://localhost:8095/ws' failed: Error during WebSocket handshake: Unexpected response code: 404",
"[webpack-dev-server] JSHandle@object",
"[webpack-dev-server] Disconnected!",
]
`;
exports[`hot and live reload should work and do nothing when web socket server disabled (default): console messages 1`] = `Array []`;

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

Expand Down
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`web socket server should work allow to disable: console messages 1`] = `
Array [
"Hey.",
]
`;

exports[`web socket server should work allow to disable: page errors 1`] = `Array []`;
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`web socket server should work allow to disable: console messages 1`] = `
Array [
"Hey.",
]
`;

exports[`web socket server should work allow to disable: page errors 1`] = `Array []`;
2 changes: 0 additions & 2 deletions test/e2e/progress.test.js
Expand Up @@ -20,8 +20,6 @@ describe("progress", () => {
const compiler = webpack(reloadConfig);
const devServerOptions = {
port,
static: false,
hot: true,
client: {
progress: true,
},
Expand Down
56 changes: 56 additions & 0 deletions test/e2e/web-socket-server.test.js
@@ -0,0 +1,56 @@
"use strict";

const webpack = require("webpack");
const Server = require("../../lib/Server");
const config = require("../fixtures/client-config/webpack.config");
const runBrowser = require("../helpers/run-browser");
const port = require("../ports-map")["web-socket-server-test"];

describe("web socket server", () => {
it("should work allow to disable", async () => {
const devServerPort = port;

const compiler = webpack(config);
const devServerOptions = {
webSocketServer: false,
port: devServerPort,
};
const server = new Server(devServerOptions, compiler);

await server.start();

const { page, browser } = await runBrowser();

const pageErrors = [];
const consoleMessages = [];

page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const webSocketRequests = [];

const client = page._client;

client.on("Network.webSocketCreated", (test) => {
webSocketRequests.push(test);
});

await page.goto(`http://127.0.0.1:${port}/main`, {
waitUntil: "networkidle0",
});

expect(webSocketRequests).toHaveLength(0);
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);
expect(pageErrors).toMatchSnapshot("page errors");

await browser.close();
await server.stop();
});
});
1 change: 1 addition & 0 deletions test/ports-map.js
Expand Up @@ -73,6 +73,7 @@ const listOfTests = {
"lazy-compilation": 1,
"range-header": 1,
port: 1,
"web-socket-server-test": 1,
};

let startPort = 8089;
Expand Down