Skip to content

Commit 006b502

Browse files
committed
fix: respect dev middleware stats in plugin mode
1 parent cc78c94 commit 006b502

3 files changed

Lines changed: 108 additions & 8 deletions

File tree

‎packages/webpack-cli/src/webpack-cli.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,17 +2398,35 @@ class WebpackCLI {
23982398
process.exitCode = 1;
23992399
}
24002400

2401-
// Plugin mode leaves stats output to the CLI.
2402-
const statsOptions = this.isMultipleCompiler(compiler)
2403-
? {
2404-
children: compiler.compilers.map((compiler) => compiler.options.stats),
2401+
// Each middleware can override stats for the whole compiler.
2402+
for (const devServerConfiguration of devServerConfigurations) {
2403+
const middlewareStats = devServerConfiguration.devMiddleware?.stats;
2404+
const getStatsOptions = (compiler: Compiler): StatsOptions => {
2405+
if (typeof middlewareStats === "undefined") {
2406+
return compiler.options.stats as StatsOptions;
24052407
}
2406-
: compiler.options.stats;
24072408

2408-
const printedStats = stats.toString(statsOptions as StatsOptions);
2409+
const statsOptions: StatsOptions =
2410+
typeof middlewareStats === "boolean"
2411+
? { preset: middlewareStats ? "normal" : "none" }
2412+
: typeof middlewareStats === "string"
2413+
? { preset: middlewareStats }
2414+
: { ...middlewareStats };
24092415

2410-
if (printedStats) {
2411-
this.logger.raw(printedStats);
2416+
if (typeof statsOptions.colors === "undefined") {
2417+
statsOptions.colors = (compiler.options.stats as StatsOptions).colors;
2418+
}
2419+
2420+
return statsOptions;
2421+
};
2422+
const statsOptions = this.isMultipleCompiler(compiler)
2423+
? { children: compiler.compilers.map(getStatsOptions) }
2424+
: getStatsOptions(compiler);
2425+
const printedStats = stats.toString(statsOptions);
2426+
2427+
if (printedStats) {
2428+
this.logger.raw(printedStats);
2429+
}
24122430
}
24132431
};
24142432

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { runWatch } = require("../../utils/test-utils");
2+
3+
describe.each([false, true])("middleware stats (multi: %s)", (multi) => {
4+
it.each(["none", "false", "true", "assets", "fallback"])(
5+
"should respect %s stats",
6+
async (kind) => {
7+
const args = ["serve", "--watch-options-stdin", "--env", `kind=${kind}`];
8+
if (multi) {
9+
args.push("--env", "multi=true");
10+
}
11+
const { exitCode, stdout } = await runWatch(__dirname, args, {
12+
handler: (proc) => {
13+
let output = "";
14+
let stopping = false;
15+
proc.stderr.on("data", (chunk) => {
16+
output += chunk.toString();
17+
if (
18+
!stopping &&
19+
output.includes("Server ready") &&
20+
output.includes("Built first") &&
21+
(!multi || output.includes("Built second"))
22+
) {
23+
stopping = true;
24+
proc.stdin.end();
25+
}
26+
});
27+
},
28+
});
29+
expect(exitCode).toBe(0);
30+
if (kind === "none" || kind === "false" || kind === "fallback") {
31+
expect(stdout).toBe("");
32+
} else {
33+
expect(stdout).toContain("first.js");
34+
if (multi) {
35+
expect(stdout).toContain("second.js");
36+
}
37+
if (kind === "assets") {
38+
expect(stdout).not.toContain("compiled successfully");
39+
} else {
40+
expect(stdout).toContain("compiled successfully");
41+
}
42+
}
43+
},
44+
);
45+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = (env) => {
2+
const overrides = {
3+
none: "none",
4+
false: false,
5+
true: true,
6+
assets: { all: false, assets: true },
7+
};
8+
const makeConfig = (name) => ({
9+
name,
10+
mode: "development",
11+
entry: "../rebuild/src/index.js",
12+
output: { filename: `${name}.js` },
13+
stats: env.kind === "none" || env.kind === "false" ? "normal" : "none",
14+
plugins: [
15+
{
16+
apply(compiler) {
17+
compiler.hooks.afterDone.tap("StatsTest", () => {
18+
console.error(`Built ${name}`);
19+
});
20+
},
21+
},
22+
],
23+
});
24+
const first = makeConfig("first");
25+
first.devServer = {
26+
host: "127.0.0.1",
27+
port: 0,
28+
static: false,
29+
client: false,
30+
hot: false,
31+
devMiddleware: { stats: overrides[env.kind] },
32+
onListening() {
33+
console.error("Server ready");
34+
},
35+
};
36+
return env.multi ? [first, makeConfig("second")] : first;
37+
};

0 commit comments

Comments
 (0)