diff --git a/.changeset/warn-non-web-target.md b/.changeset/warn-non-web-target.md new file mode 100644 index 0000000000..3ab3817011 --- /dev/null +++ b/.changeset/warn-non-web-target.md @@ -0,0 +1,5 @@ +--- +"webpack-dev-server": patch +--- + +Warn when `hot` or `liveReload` is enabled but no compiler has a web target, since no client is injected and nothing reaches a page. diff --git a/lib/Server.js b/lib/Server.js index 3a8ab17bad..0ab2ba6bb9 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -1772,11 +1772,38 @@ class Server { /** @type {MultiCompiler} */ (this.compiler).compilers || [this.compiler]; + // No client goes into a compilation a browser will not run, so `hot` and + // `liveReload` have no page to reach there. Said once per compiler, and + // only when none of them is a web target: a `node` compiler next to a + // `web` one is a server-side rendering setup, where the page gets its + // client from the `web` compiler and the `node` one is expected to have + // none. + const reaching = [ + this.options.hot && "'hot'", + this.options.liveReload && "'liveReload'", + ].filter(Boolean); + const warnNonWebTarget = + this.options.client !== false && + reaching.length > 0 && + !compilers.some( + (compiler) => + compiler.options.devServer !== false && + Server.isWebTarget(compiler), + ); + for (const compiler of compilers) { if (compiler.options.devServer === false) { continue; } + if (warnNonWebTarget) { + const { name, target } = compiler.options; + + this.logger.warn( + `${name ? `Compiler "${name}"` : "The compiler"} has a non-web target (${JSON.stringify(target)}), so no client is injected into it and ${reaching.join(" and ")} will not reach a page built from it.`, + ); + } + const webpack = compiler.webpack || /** @type {NonNullable>>} */ ( diff --git a/test/e2e/target.test.js b/test/e2e/target.test.js index 1462ca5ac2..0e1b3bd8d4 100644 --- a/test/e2e/target.test.js +++ b/test/e2e/target.test.js @@ -1,6 +1,7 @@ import path from "node:path"; import { describe, it } from "node:test"; import { fileURLToPath } from "node:url"; +import { expect } from "expect"; import webpack from "webpack"; import Server from "../../lib/Server.js"; import config from "../fixtures/client-config/webpack.config.js"; @@ -212,3 +213,120 @@ describe("target", () => { } }); }); + +describe("non-web target warning", () => { + /** + * Build a compiler for `targets` whose infrastructure warnings land in + * `warnings`. + * @param {(string | false)[]} targets one target per compiler + * @param {string[]} warnings where the warnings are collected + * @returns {import("webpack").Compiler | import("webpack").MultiCompiler} compiler + */ + const compile = (targets, warnings) => { + const configs = targets.map((target, index) => ({ + ...config, + ...(targets.length > 1 ? { name: `${target}-${index}` } : {}), + target, + output: { + path: "/", + filename: `${index}-[name].js`, + }, + infrastructureLogging: { + level: "warn", + colors: false, + stream: { + write: (message) => { + warnings.push(message); + }, + }, + }, + })); + + return webpack(configs.length === 1 ? configs[0] : configs); + }; + + /** + * @param {string[]} warnings collected warnings + * @returns {string[]} the ones about a non-web target + */ + const nonWebTargetWarnings = (warnings) => + warnings.filter((message) => message.includes("non-web target")); + + /** + * @param {import("webpack").Compiler | import("webpack").MultiCompiler} compiler compiler + * @param {Record=} options dev server options + * @returns {Promise} resolved once the server started and stopped + */ + const startAndStop = async (compiler, options = {}) => { + const server = new Server({ port, ...options }, compiler); + + await server.start(); + await server.stop(); + }; + + it("should warn once for a `node` target", async () => { + const warnings = []; + + await startAndStop(compile(["node"], warnings)); + + expect(nonWebTargetWarnings(warnings)).toEqual([ + " [webpack-dev-server] The compiler has a non-web target (\"node\"), so no client is injected into it and 'hot' and 'liveReload' will not reach a page built from it.\n", + ]); + }); + + it("should warn about `liveReload` alone when `hot` is disabled", async () => { + const warnings = []; + + await startAndStop(compile(["node"], warnings), { hot: false }); + + expect(nonWebTargetWarnings(warnings)).toEqual([ + " [webpack-dev-server] The compiler has a non-web target (\"node\"), so no client is injected into it and 'liveReload' will not reach a page built from it.\n", + ]); + }); + + it("should not warn when `hot` and `liveReload` are disabled", async () => { + const warnings = []; + + await startAndStop(compile(["node"], warnings), { + hot: false, + liveReload: false, + }); + + expect(nonWebTargetWarnings(warnings)).toEqual([]); + }); + + it("should not warn when `client` is disabled", async () => { + const warnings = []; + + await startAndStop(compile(["node"], warnings), { client: false }); + + expect(nonWebTargetWarnings(warnings)).toEqual([]); + }); + + it("should not warn for a `web` target", async () => { + const warnings = []; + + await startAndStop(compile(["web"], warnings)); + + expect(nonWebTargetWarnings(warnings)).toEqual([]); + }); + + it("should not warn for the `node` compiler of a `web` and `node` multi compiler", async () => { + const warnings = []; + + await startAndStop(compile(["web", "node"], warnings)); + + expect(nonWebTargetWarnings(warnings)).toEqual([]); + }); + + it("should warn once per compiler when no compiler has a web target", async () => { + const warnings = []; + + await startAndStop(compile(["node", "async-node"], warnings)); + + expect(nonWebTargetWarnings(warnings)).toEqual([ + " [webpack-dev-server] Compiler \"node-0\" has a non-web target (\"node\"), so no client is injected into it and 'hot' and 'liveReload' will not reach a page built from it.\n", + " [webpack-dev-server] Compiler \"async-node-1\" has a non-web target (\"async-node\"), so no client is injected into it and 'hot' and 'liveReload' will not reach a page built from it.\n", + ]); + }); +});