Skip to content

Commit f659624

Browse files
authoredJun 7, 2023
fix: multi compiler progress output
1 parent 0d1ff01 commit f659624

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"ts-loader": "^9.3.1",
9494
"ts-node": "^10.9.1",
9595
"typescript": "^5.0.4",
96-
"webpack": "^5.72.0",
96+
"webpack": "^5.86.0",
9797
"webpack-bundle-analyzer": "^4.5.0",
9898
"webpack-dev-server": "^4.8.1"
9999
}

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

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Compiler } from "webpack";
1+
import { type Compiler, ProgressPlugin } from "webpack";
22
import { type CLIPluginOptions } from "../types";
33

44
export class CLIPlugin {
@@ -21,17 +21,51 @@ export class CLIPlugin {
2121
}
2222
}
2323

24+
static #progressStates: [number, ...string[]][] = [];
25+
2426
setupProgressPlugin(compiler: Compiler) {
2527
const { ProgressPlugin } = compiler.webpack || require("webpack");
2628
const progressPlugin = Boolean(
2729
compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin),
2830
);
2931

30-
if (!progressPlugin) {
31-
new ProgressPlugin({
32-
profile: this.options.progress === "profile",
33-
}).apply(compiler);
32+
if (progressPlugin) {
33+
return;
3434
}
35+
36+
const isProfile = this.options.progress === "profile";
37+
38+
const options: ConstructorParameters<typeof ProgressPlugin>[0] = {
39+
profile: isProfile,
40+
};
41+
42+
if (this.options.isMultiCompiler && ProgressPlugin.createDefaultHandler) {
43+
const handler = ProgressPlugin.createDefaultHandler(
44+
isProfile,
45+
compiler.getInfrastructureLogger("webpack.Progress"),
46+
);
47+
const idx = CLIPlugin.#progressStates.length;
48+
49+
CLIPlugin.#progressStates[idx] = [0];
50+
51+
options.handler = (p: number, msg: string, ...args: string[]) => {
52+
CLIPlugin.#progressStates[idx] = [p, msg, ...args];
53+
54+
let sum = 0;
55+
56+
for (const [p] of CLIPlugin.#progressStates) {
57+
sum += p;
58+
}
59+
60+
handler(
61+
sum / CLIPlugin.#progressStates.length,
62+
`[${compiler.name ? compiler.name : idx}] ${msg}`,
63+
...args,
64+
);
65+
};
66+
}
67+
68+
new ProgressPlugin(options).apply(compiler);
3569
}
3670

3771
setupHelpfulOutput(compiler: Compiler) {

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

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
Argument,
1212
AssetEmittedInfo,
1313
FileCacheOptions,
14+
WebpackPluginInstance,
1415
} from "webpack";
1516
import type webpack from "webpack";
1617

@@ -210,6 +211,7 @@ type ProcessedArguments = Record<string, BasicPrimitive | RegExp | (BasicPrimiti
210211
type CommandAction = Parameters<WebpackCLICommand["action"]>[0];
211212

212213
interface WebpackRunOptions extends WebpackOptionsNormalized {
214+
progress?: boolean | "profile";
213215
json?: boolean;
214216
argv?: Argv;
215217
env: Env;
@@ -237,6 +239,7 @@ interface BasicPackageJsonContent {
237239
*/
238240

239241
interface CLIPluginOptions {
242+
isMultiCompiler?: boolean;
240243
configPath?: string[];
241244
helpfulOutput: boolean;
242245
hot?: boolean | "only";

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

+1
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,7 @@ class WebpackCLI implements IWebpackCLI {
23482348
helpfulOutput: !options.json,
23492349
progress: options.progress,
23502350
analyze: options.analyze,
2351+
isMultiCompiler: Array.isArray(config.options),
23512352
}),
23522353
);
23532354
};

‎yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -11138,10 +11138,10 @@ webpack-sources@^3.2.3:
1113811138
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde"
1113911139
integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==
1114011140

11141-
webpack@^5.72.0:
11142-
version "5.85.1"
11143-
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.85.1.tgz#d77406352f8f14ec847c54e4dcfb80b28c776b3f"
11144-
integrity sha512-xTb7MRf4LY8Z5rzn7aIx4TDrwYJrjcHnIfU1TqtyZOoObyuGSpAUwIvVuqq5wPnv7WEgQr8UvO1q/dgoGG4HjA==
11141+
webpack@^5.86.0:
11142+
version "5.86.0"
11143+
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.86.0.tgz#b0eb81794b62aee0b7e7eb8c5073495217d9fc6d"
11144+
integrity sha512-3BOvworZ8SO/D4GVP+GoRC3fVeg5MO4vzmq8TJJEkdmopxyazGDxN8ClqN12uzrZW9Tv8EED8v5VSb6Sqyi0pg==
1114511145
dependencies:
1114611146
"@types/eslint-scope" "^3.7.3"
1114711147
"@types/estree" "^1.0.0"

0 commit comments

Comments
 (0)
Please sign in to comment.