forked from webpack/webpack-cli
/
WebpackCLIPlugin.js
51 lines (40 loc) · 1.73 KB
/
WebpackCLIPlugin.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
const { packageExists } = require('../utils/package-exists');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
const logger = require('../utils/logger');
const PluginName = 'webpack-cli';
class WebpackCLIPlugin {
constructor(options) {
this.options = options;
}
async apply(compiler) {
const compilers = compiler.compilers ? compiler.compilers : [compiler];
for (const compiler of compilers) {
if (this.options.progress) {
const { ProgressPlugin } = compiler.webpack || webpack;
let progressPluginExists;
if (compiler.options.plugins) {
progressPluginExists = Boolean(compiler.options.plugins.find((e) => e instanceof ProgressPlugin));
}
if (!progressPluginExists) {
new ProgressPlugin().apply(compiler);
}
}
}
compiler.hooks.watchRun.tap(PluginName, (compilation) => {
const { bail, watch } = compilation.options;
if (bail && watch) {
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
}
logger.success(`Compilation${compilation.name ? `${compilation.name}` : ''} starting...`);
});
compiler.hooks.done.tap(PluginName, (compilation) => {
logger.success(`Compilation${compilation.name ? `${compilation.name}` : ''} finished`);
process.nextTick(() => {
if (compiler.watchMode) {
logger.success('watching files for updates...');
}
});
});
}
}
module.exports = WebpackCLIPlugin;