Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warn-non-web-target.md
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Awaited<ReturnType<typeof loadWebpackPeer>>>} */ (
Expand Down
118 changes: 118 additions & 0 deletions test/e2e/target.test.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<string, unknown>=} options dev server options
* @returns {Promise<void>} 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([
"<w> [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([
"<w> [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([
"<w> [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",
"<w> [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",
]);
});
});
Loading