-
-
Notifications
You must be signed in to change notification settings - Fork 678
Expand file tree
/
Copy pathcli-plugin.ts
More file actions
158 lines (120 loc) · 4.2 KB
/
Copy pathcli-plugin.ts
File metadata and controls
158 lines (120 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { type Compiler } from "webpack";
interface CLIPluginOptions {
isMultiCompiler?: boolean;
configPath?: string[];
helpfulOutput: boolean;
hot?: boolean | "only";
progress?: boolean | "profile";
prefetch?: string;
analyze?: boolean;
}
export default class CLIPlugin {
logger!: ReturnType<Compiler["getInfrastructureLogger"]>;
options: CLIPluginOptions;
constructor(options: CLIPluginOptions) {
this.options = options;
}
async setupBundleAnalyzerPlugin(compiler: Compiler) {
// @ts-expect-error No types right now
const { BundleAnalyzerPlugin } = (await import("webpack-bundle-analyzer")).default;
const bundleAnalyzerPlugin = compiler.options.plugins.some(
(plugin) => plugin instanceof BundleAnalyzerPlugin,
);
if (!bundleAnalyzerPlugin) {
new BundleAnalyzerPlugin().apply(compiler);
}
}
static #progressStates: [number, ...string[]][] = [];
setupProgressPlugin(compiler: Compiler) {
const { ProgressPlugin } = compiler.webpack;
const progressPlugin = compiler.options.plugins.some(
(plugin) => plugin instanceof ProgressPlugin,
);
if (progressPlugin) {
return;
}
const isProfile = this.options.progress === "profile";
const options: ConstructorParameters<typeof ProgressPlugin>[0] = {
profile: isProfile,
};
if (this.options.isMultiCompiler && ProgressPlugin.createDefaultHandler) {
const handler = ProgressPlugin.createDefaultHandler(
isProfile,
compiler.getInfrastructureLogger("webpack.Progress"),
);
const idx = CLIPlugin.#progressStates.length;
CLIPlugin.#progressStates[idx] = [0];
options.handler = (progress: number, msg: string, ...args: string[]) => {
CLIPlugin.#progressStates[idx] = [progress, msg, ...args];
let sum = 0;
for (const [progress] of CLIPlugin.#progressStates) {
sum += progress;
}
handler(
sum / CLIPlugin.#progressStates.length,
`[${compiler.name || idx}] ${msg}`,
...args,
);
};
}
new ProgressPlugin(options).apply(compiler);
}
setupHelpfulOutput(compiler: Compiler) {
const pluginName = "webpack-cli";
const getCompilationName = () => (compiler.name ? ` '${compiler.name}'` : "");
const logCompilation = (message: string) => {
if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG) {
process.stderr.write(message);
} else {
this.logger.log(message);
}
};
const { configPath } = this.options;
compiler.hooks.run.tap(pluginName, () => {
const name = getCompilationName();
logCompilation(`Compiler${name} starting... `);
if (configPath) {
this.logger.log(
`Compiler${name} is using config: ${configPath.map((path) => `'${path}'`).join(", ")}`,
);
}
});
compiler.hooks.watchRun.tap(pluginName, (compiler) => {
const { bail, watch } = compiler.options;
if (bail && watch) {
this.logger.warn(
'You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.',
);
}
const name = getCompilationName();
logCompilation(`Compiler${name} starting... `);
if (configPath) {
this.logger.log(`Compiler${name} is using config: '${configPath}'`);
}
});
compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
const date = new Date(changeTime);
this.logger.log(`File '${filename}' was modified`);
this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
});
compiler.hooks.afterDone.tap(pluginName, () => {
const name = getCompilationName();
logCompilation(`Compiler${name} finished`);
process.nextTick(() => {
if (compiler.watchMode) {
this.logger.log(`Compiler${name} is watching files for updates...`);
}
});
});
}
apply(compiler: Compiler) {
this.logger = compiler.getInfrastructureLogger("webpack-cli");
if (this.options.progress) {
this.setupProgressPlugin(compiler);
}
if (this.options.analyze) {
this.setupBundleAnalyzerPlugin(compiler);
}
this.setupHelpfulOutput(compiler);
}
}