-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
55 lines (44 loc) · 1.32 KB
/
index.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
'use strict';
class CleanTerminalPlugin {
constructor(options = {}) {
const { message, onlyInWatchMode = true, skipFirstRun = false, beforeCompile = false } = options;
this.message = message;
this.onlyInWatchMode = onlyInWatchMode;
this.skipFirstRun = skipFirstRun;
this.beforeCompile = beforeCompile;
this.firstRun = true;
}
apply(compiler) {
let hook = compiler.hooks.afterCompile;
if (this.beforeCompile) {
hook = compiler.hooks.beforeCompile;
}
hook.tap('CleanTerminalPlugin', () => {
if (this.shouldClearConsole(compiler)) {
this.clearConsole();
}
});
}
shouldClearConsole(compiler) {
if (this.firstRun) {
this.firstRun = false;
if (this.skipFirstRun) {
return false;
}
}
if (this.onlyInWatchMode) {
return Boolean(compiler.watchMode);
}
const isNodeEnvProduction = process.env.NODE_ENV === 'production';
const isOptionsModeProduction = Boolean(
compiler.options && compiler.options.mode === 'production'
);
return !isNodeEnvProduction && !isOptionsModeProduction;
}
clearConsole() {
const clear = '\x1B[2J\x1B[3J\x1B[H';
const output = this.message ? `${clear + this.message}\n\n` : clear;
process.stdout.write(output);
}
}
module.exports = CleanTerminalPlugin;