Skip to content

Commit 8b341cb

Browse files
authoredAug 20, 2024··
fix: support devServer: false (#5272)
1 parent 06005e7 commit 8b341cb

File tree

10 files changed

+151
-17
lines changed

10 files changed

+151
-17
lines changed
 

‎.cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"hoge",
6767
"subsubcomain",
6868
"noselect",
69-
"commitlint"
69+
"commitlint",
70+
"eslintcache"
7071
],
7172
"ignorePaths": [
7273
"CHANGELOG.md",

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ test/fixtures/static-config/public/assets/non-exist.txt
2020
test/fixtures/watch-files-config/public/assets/non-exist.txt
2121
test/fixtures/reload-config/main.css
2222
test/fixtures/reload-config-2/main.css
23+
test/fixtures/worker-config-dev-server-false/public
2324
!/test/fixtures/static-config/public/node_modules

‎lib/Server.js

+5
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,11 @@ class Server {
17201720
(this.compiler).compilers || [this.compiler];
17211721

17221722
for (const compiler of compilers) {
1723+
if (compiler.options.devServer === false) {
1724+
// eslint-disable-next-line no-continue
1725+
continue;
1726+
}
1727+
17231728
this.addAdditionalEntries(compiler);
17241729

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

‎package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"serve-index": "^1.9.1",
7373
"sockjs": "^0.3.24",
7474
"spdy": "^4.0.2",
75-
"webpack-dev-middleware": "^7.1.0",
75+
"webpack-dev-middleware": "^7.4.0",
7676
"ws": "^8.18.0"
7777
},
7878
"devDependencies": {

‎test/e2e/__snapshots__/target.test.js.snap.webpack5

+11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ exports[`target should work using "webworker" target: console messages 1`] = `
7272

7373
exports[`target should work using "webworker" target: page errors 1`] = `[]`;
7474

75+
exports[`target should work using multi compiler mode with \`web\` and \`webworker\` targets with \`devServer: false\`: console messages 1`] = `
76+
[
77+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
78+
"[HMR] Waiting for update signal from WDS...",
79+
"Worker said: I'm working before postMessage",
80+
"Worker said: Message sent: message",
81+
]
82+
`;
83+
84+
exports[`target should work using multi compiler mode with \`web\` and \`webworker\` targets with \`devServer: false\`: page errors 1`] = `[]`;
85+
7586
exports[`target should work using multi compiler mode with \`web\` and \`webworker\` targets: console messages 1`] = `
7687
[
7788
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",

‎test/e2e/target.test.js

+64-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
"use strict";
22

3+
const path = require("path");
34
const webpack = require("webpack");
45
const Server = require("../../lib/Server");
56
const config = require("../fixtures/client-config/webpack.config");
67
const workerConfig = require("../fixtures/worker-config/webpack.config");
8+
const workerConfigDevServerFalse = require("../fixtures/worker-config-dev-server-false/webpack.config");
79
const runBrowser = require("../helpers/run-browser");
810
const port = require("../ports-map").target;
911

12+
const sortByTerm = (data, term) =>
13+
data.sort((a, b) => (a.indexOf(term) < b.indexOf(term) ? -1 : 1));
14+
1015
describe("target", () => {
1116
const targets = [
1217
false,
@@ -35,10 +40,7 @@ describe("target", () => {
3540
}
3641
: {}),
3742
});
38-
const devServerOptions = {
39-
port,
40-
};
41-
const server = new Server(devServerOptions, compiler);
43+
const server = new Server({ port }, compiler);
4244

4345
await server.start();
4446

@@ -93,10 +95,58 @@ describe("target", () => {
9395

9496
it("should work using multi compiler mode with `web` and `webworker` targets", async () => {
9597
const compiler = webpack(workerConfig);
96-
const devServerOptions = {
97-
port,
98-
};
99-
const server = new Server(devServerOptions, compiler);
98+
const server = new Server({ port }, compiler);
99+
100+
await server.start();
101+
102+
const { page, browser } = await runBrowser();
103+
104+
try {
105+
const pageErrors = [];
106+
const consoleMessages = [];
107+
108+
page
109+
.on("console", (message) => {
110+
consoleMessages.push(message);
111+
})
112+
.on("pageerror", (error) => {
113+
pageErrors.push(error);
114+
});
115+
116+
await page.goto(`http://127.0.0.1:${port}/`, {
117+
waitUntil: "networkidle0",
118+
});
119+
120+
expect(
121+
sortByTerm(
122+
consoleMessages.map((message) => message.text()),
123+
"Worker said:",
124+
),
125+
).toMatchSnapshot("console messages");
126+
127+
expect(pageErrors).toMatchSnapshot("page errors");
128+
} catch (error) {
129+
throw error;
130+
} finally {
131+
await browser.close();
132+
await server.stop();
133+
}
134+
});
135+
136+
it("should work using multi compiler mode with `web` and `webworker` targets with `devServer: false`", async () => {
137+
const compiler = webpack(workerConfigDevServerFalse);
138+
const server = new Server(
139+
{
140+
port,
141+
static: {
142+
directory: path.resolve(
143+
__dirname,
144+
"../fixtures/worker-config-dev-server-false/public/",
145+
),
146+
},
147+
},
148+
compiler,
149+
);
100150

101151
await server.start();
102152

@@ -118,9 +168,12 @@ describe("target", () => {
118168
waitUntil: "networkidle0",
119169
});
120170

121-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
122-
"console messages",
123-
);
171+
expect(
172+
sortByTerm(
173+
consoleMessages.map((message) => message.text()),
174+
"Worker said:",
175+
),
176+
).toMatchSnapshot("console messages");
124177

125178
expect(pageErrors).toMatchSnapshot("page errors");
126179
} catch (error) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
const myWorker = new Worker("./worker-bundle.js");
4+
5+
myWorker.onmessage = (event) => {
6+
console.log(`Worker said: ${event.data}`);
7+
};
8+
9+
myWorker.postMessage("message");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const HTMLGeneratorPlugin = require("../../helpers/html-generator-plugin");
5+
6+
module.exports = [
7+
{
8+
name: "app",
9+
// dependencies: ["worker"],
10+
devtool: false,
11+
target: "web",
12+
entry: "./index.js",
13+
mode: "development",
14+
context: __dirname,
15+
stats: "none",
16+
output: {
17+
path: path.resolve(__dirname, "./dist/"),
18+
},
19+
infrastructureLogging: {
20+
level: "info",
21+
stream: {
22+
write: () => {},
23+
},
24+
},
25+
plugins: [new HTMLGeneratorPlugin()],
26+
},
27+
{
28+
name: "worker",
29+
devtool: false,
30+
target: "webworker",
31+
entry: "./worker.js",
32+
mode: "development",
33+
context: __dirname,
34+
stats: "none",
35+
output: {
36+
path: path.resolve(__dirname, "public"),
37+
filename: "worker-bundle.js",
38+
},
39+
infrastructureLogging: {
40+
level: "info",
41+
stream: {
42+
write: () => {},
43+
},
44+
},
45+
devServer: false,
46+
},
47+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
postMessage("I'm working before postMessage");
4+
5+
onmessage = (event) => {
6+
postMessage(`Message sent: ${event.data}`);
7+
};

0 commit comments

Comments
 (0)
Please sign in to comment.