/
CLIPlugin.js
104 lines (79 loc) · 3.61 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
const packageExists = require('../utils/package-exists');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
class CLIPlugin {
constructor(options) {
this.options = options;
}
setupHotPlugin(compiler) {
const { HotModuleReplacementPlugin } = compiler.webpack || webpack;
const hotModuleReplacementPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof HotModuleReplacementPlugin));
if (!hotModuleReplacementPlugin) {
new HotModuleReplacementPlugin().apply(compiler);
}
}
setupPrefetchPlugin(compiler) {
const { PrefetchPlugin } = compiler.webpack || 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 || 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 = (compilation) => (compilation.name ? ` '${compilation.name}'` : '');
compiler.hooks.run.tap(pluginName, (compiler) => {
this.logger.info(`Compilation${getCompilationName(compiler)} starting...`);
});
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.');
}
this.logger.info(`Compilation${getCompilationName(compiler)} starting...`);
});
compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
const date = new Date(changeTime * 1000);
this.logger.info(`File '${filename}' was modified`);
this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
});
(compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, (stats) => {
this.logger.info(`Compilation${getCompilationName(stats.compilation)} finished`);
process.nextTick(() => {
if (compiler.watchMode) {
this.logger.info(`Compiler${getCompilationName(stats.compilation)} is watching files for updates...`);
}
});
});
}
apply(compiler) {
this.logger = compiler.getInfrastructureLogger('webpack-cli');
if (this.options.progress && this.options.helpfulOutput) {
this.setupProgressPlugin(compiler);
}
if (this.options.hot) {
this.setupHotPlugin(compiler);
}
if (this.options.prefetch) {
this.setupPrefetchPlugin(compiler);
}
if (this.options.analyze) {
this.setupBundleAnalyzerPlugin(compiler);
}
if (this.options.helpfulOutput) {
this.setupHelpfulOutput(compiler);
}
}
}
module.exports = CLIPlugin;