From 8b341cb8c1dc01cef62c70959620cd0cbd87fee7 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:08:26 +0300 Subject: [PATCH] fix: support `devServer: false` (#5272) --- .cspell.json | 3 +- .gitignore | 1 + lib/Server.js | 5 ++ package-lock.json | 8 +- package.json | 2 +- .../target.test.js.snap.webpack5 | 11 +++ test/e2e/target.test.js | 75 ++++++++++++++++--- .../worker-config-dev-server-false/index.js | 9 +++ .../webpack.config.js | 47 ++++++++++++ .../worker-config-dev-server-false/worker.js | 7 ++ 10 files changed, 151 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/worker-config-dev-server-false/index.js create mode 100644 test/fixtures/worker-config-dev-server-false/webpack.config.js create mode 100644 test/fixtures/worker-config-dev-server-false/worker.js diff --git a/.cspell.json b/.cspell.json index b022c7824e..21a4aa2ed5 100644 --- a/.cspell.json +++ b/.cspell.json @@ -66,7 +66,8 @@ "hoge", "subsubcomain", "noselect", - "commitlint" + "commitlint", + "eslintcache" ], "ignorePaths": [ "CHANGELOG.md", diff --git a/.gitignore b/.gitignore index 389040450b..de3b694aec 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ test/fixtures/static-config/public/assets/non-exist.txt test/fixtures/watch-files-config/public/assets/non-exist.txt test/fixtures/reload-config/main.css test/fixtures/reload-config-2/main.css +test/fixtures/worker-config-dev-server-false/public !/test/fixtures/static-config/public/node_modules diff --git a/lib/Server.js b/lib/Server.js index 82640fb8f0..ca16ec04d7 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -1720,6 +1720,11 @@ class Server { (this.compiler).compilers || [this.compiler]; for (const compiler of compilers) { + if (compiler.options.devServer === false) { + // eslint-disable-next-line no-continue + continue; + } + this.addAdditionalEntries(compiler); const webpack = compiler.webpack || require("webpack"); diff --git a/package-lock.json b/package-lock.json index aacb24c00f..4526c95237 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,7 +35,7 @@ "serve-index": "^1.9.1", "sockjs": "^0.3.24", "spdy": "^4.0.2", - "webpack-dev-middleware": "^7.1.0", + "webpack-dev-middleware": "^7.4.0", "ws": "^8.18.0" }, "bin": { @@ -19325,9 +19325,9 @@ } }, "node_modules/webpack-dev-middleware": { - "version": "7.3.0", - "resolved": "https://registry.npmjs.org/webpack-dev-middleware/-/webpack-dev-middleware-7.3.0.tgz", - "integrity": "sha512-xD2qnNew+F6KwOGZR7kWdbIou/ud7cVqLEXeK1q0nHcNsX/u7ul/fSdlOTX4ntSL5FNFy7ZJJXbf0piF591JYw==", + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/webpack-dev-middleware/-/webpack-dev-middleware-7.4.0.tgz", + "integrity": "sha512-o2Pe3n+vhx+OTwKhdV0EAA84XhcGYC5Jo2OSNRzU6V1chl/GOUz91xBHfMutsBp0GULP5zuQ30Hb9mv5xoxf7Q==", "dependencies": { "colorette": "^2.0.10", "memfs": "^4.6.0", diff --git a/package.json b/package.json index ca7723d03d..e30ec812cb 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "serve-index": "^1.9.1", "sockjs": "^0.3.24", "spdy": "^4.0.2", - "webpack-dev-middleware": "^7.1.0", + "webpack-dev-middleware": "^7.4.0", "ws": "^8.18.0" }, "devDependencies": { diff --git a/test/e2e/__snapshots__/target.test.js.snap.webpack5 b/test/e2e/__snapshots__/target.test.js.snap.webpack5 index 4040d1fcfb..f8a53f5891 100644 --- a/test/e2e/__snapshots__/target.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/target.test.js.snap.webpack5 @@ -72,6 +72,17 @@ exports[`target should work using "webworker" target: console messages 1`] = ` exports[`target should work using "webworker" target: page errors 1`] = `[]`; +exports[`target should work using multi compiler mode with \`web\` and \`webworker\` targets with \`devServer: false\`: console messages 1`] = ` +[ + "[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.", + "[HMR] Waiting for update signal from WDS...", + "Worker said: I'm working before postMessage", + "Worker said: Message sent: message", +] +`; + +exports[`target should work using multi compiler mode with \`web\` and \`webworker\` targets with \`devServer: false\`: page errors 1`] = `[]`; + exports[`target should work using multi compiler mode with \`web\` and \`webworker\` targets: console messages 1`] = ` [ "[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.", diff --git a/test/e2e/target.test.js b/test/e2e/target.test.js index 7a779c4aee..5e3d29c8c4 100644 --- a/test/e2e/target.test.js +++ b/test/e2e/target.test.js @@ -1,12 +1,17 @@ "use strict"; +const path = require("path"); const webpack = require("webpack"); const Server = require("../../lib/Server"); const config = require("../fixtures/client-config/webpack.config"); const workerConfig = require("../fixtures/worker-config/webpack.config"); +const workerConfigDevServerFalse = require("../fixtures/worker-config-dev-server-false/webpack.config"); const runBrowser = require("../helpers/run-browser"); const port = require("../ports-map").target; +const sortByTerm = (data, term) => + data.sort((a, b) => (a.indexOf(term) < b.indexOf(term) ? -1 : 1)); + describe("target", () => { const targets = [ false, @@ -35,10 +40,7 @@ describe("target", () => { } : {}), }); - const devServerOptions = { - port, - }; - const server = new Server(devServerOptions, compiler); + const server = new Server({ port }, compiler); await server.start(); @@ -93,10 +95,58 @@ describe("target", () => { it("should work using multi compiler mode with `web` and `webworker` targets", async () => { const compiler = webpack(workerConfig); - const devServerOptions = { - port, - }; - const server = new Server(devServerOptions, compiler); + const server = new Server({ port }, compiler); + + await server.start(); + + const { page, browser } = await runBrowser(); + + try { + const pageErrors = []; + const consoleMessages = []; + + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + await page.goto(`http://127.0.0.1:${port}/`, { + waitUntil: "networkidle0", + }); + + expect( + sortByTerm( + consoleMessages.map((message) => message.text()), + "Worker said:", + ), + ).toMatchSnapshot("console messages"); + + expect(pageErrors).toMatchSnapshot("page errors"); + } catch (error) { + throw error; + } finally { + await browser.close(); + await server.stop(); + } + }); + + it("should work using multi compiler mode with `web` and `webworker` targets with `devServer: false`", async () => { + const compiler = webpack(workerConfigDevServerFalse); + const server = new Server( + { + port, + static: { + directory: path.resolve( + __dirname, + "../fixtures/worker-config-dev-server-false/public/", + ), + }, + }, + compiler, + ); await server.start(); @@ -118,9 +168,12 @@ describe("target", () => { waitUntil: "networkidle0", }); - expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( - "console messages", - ); + expect( + sortByTerm( + consoleMessages.map((message) => message.text()), + "Worker said:", + ), + ).toMatchSnapshot("console messages"); expect(pageErrors).toMatchSnapshot("page errors"); } catch (error) { diff --git a/test/fixtures/worker-config-dev-server-false/index.js b/test/fixtures/worker-config-dev-server-false/index.js new file mode 100644 index 0000000000..a7cbdfdd80 --- /dev/null +++ b/test/fixtures/worker-config-dev-server-false/index.js @@ -0,0 +1,9 @@ +"use strict"; + +const myWorker = new Worker("./worker-bundle.js"); + +myWorker.onmessage = (event) => { + console.log(`Worker said: ${event.data}`); +}; + +myWorker.postMessage("message"); diff --git a/test/fixtures/worker-config-dev-server-false/webpack.config.js b/test/fixtures/worker-config-dev-server-false/webpack.config.js new file mode 100644 index 0000000000..a53fe51d31 --- /dev/null +++ b/test/fixtures/worker-config-dev-server-false/webpack.config.js @@ -0,0 +1,47 @@ +"use strict"; + +const path = require("path"); +const HTMLGeneratorPlugin = require("../../helpers/html-generator-plugin"); + +module.exports = [ + { + name: "app", + // dependencies: ["worker"], + devtool: false, + target: "web", + entry: "./index.js", + mode: "development", + context: __dirname, + stats: "none", + output: { + path: path.resolve(__dirname, "./dist/"), + }, + infrastructureLogging: { + level: "info", + stream: { + write: () => {}, + }, + }, + plugins: [new HTMLGeneratorPlugin()], + }, + { + name: "worker", + devtool: false, + target: "webworker", + entry: "./worker.js", + mode: "development", + context: __dirname, + stats: "none", + output: { + path: path.resolve(__dirname, "public"), + filename: "worker-bundle.js", + }, + infrastructureLogging: { + level: "info", + stream: { + write: () => {}, + }, + }, + devServer: false, + }, +]; diff --git a/test/fixtures/worker-config-dev-server-false/worker.js b/test/fixtures/worker-config-dev-server-false/worker.js new file mode 100644 index 0000000000..37bab96133 --- /dev/null +++ b/test/fixtures/worker-config-dev-server-false/worker.js @@ -0,0 +1,7 @@ +"use strict"; + +postMessage("I'm working before postMessage"); + +onmessage = (event) => { + postMessage(`Message sent: ${event.data}`); +};