/
CLIPlugin.js
115 lines (85 loc) · 3.81 KB
/
CLIPlugin.js
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
class CLIPlugin {
constructor(options) {
this.options = options;
}
setupHotPlugin(compiler) {
const { HotModuleReplacementPlugin } = compiler.webpack || require('webpack');
const hotModuleReplacementPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof HotModuleReplacementPlugin));
if (!hotModuleReplacementPlugin) {
new HotModuleReplacementPlugin().apply(compiler);
}
}
setupPrefetchPlugin(compiler) {
const { PrefetchPlugin } = compiler.webpack || require('webpack');
new PrefetchPlugin(null, this.options.prefetch).apply(compiler);
}
async setupBundleAnalyzerPlugin(compiler) {
// eslint-disable-next-line node/no-extraneous-require
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const bundleAnalyzerPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof BundleAnalyzerPlugin));
if (!bundleAnalyzerPlugin) {
new BundleAnalyzerPlugin().apply(compiler);
}
}
setupProgressPlugin(compiler) {
const { ProgressPlugin } = compiler.webpack || require('webpack');
const progressPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));
if (!progressPlugin) {
new ProgressPlugin({ profile: this.options.progress === 'profile' }).apply(compiler);
}
}
setupHelpfulOutput(compiler) {
const pluginName = 'webpack-cli';
const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : '');
const { configPath } = this.options;
compiler.hooks.run.tap(pluginName, () => {
const name = getCompilationName();
this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`);
if (configPath) {
this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`);
}
});
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();
this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`);
if (configPath) {
this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`);
}
});
compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
const date = new Date(changeTime * 1000);
this.logger.log(`File '${filename}' was modified`);
this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
});
(compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => {
const name = getCompilationName();
this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`);
process.nextTick(() => {
if (compiler.watchMode) {
this.logger.log(`Compiler${name ? `${name}` : ''} is watching files for updates...`);
}
});
});
}
apply(compiler) {
this.logger = compiler.getInfrastructureLogger('webpack-cli');
if (this.options.progress) {
this.setupProgressPlugin(compiler);
}
if (this.options.hot) {
this.setupHotPlugin(compiler);
}
if (this.options.prefetch) {
this.setupPrefetchPlugin(compiler);
}
if (this.options.analyze) {
this.setupBundleAnalyzerPlugin(compiler);
}
this.setupHelpfulOutput(compiler);
}
}
module.exports = CLIPlugin;