-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (75 loc) · 1.96 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
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
const del = require('del');
const ALIAS__BEFORE_COMPILATION = 'before-compilation';
const ALIAS__AFTER_COMPILATION = 'after-compilation';
function WebpackDelPlugin(options) {
this.options = options;
};
WebpackDelPlugin.prototype.apply = function (compiler) {
if (!isValidOptions(this.options)) {
return;
}
if (Array.isArray(this.options)) {
for (var i = 0; i < this.options.length; i++) {
handleHook.call(this, compiler, this.options[i]);
}
} else if (typeof this.options === 'object') {
handleHook.call(this, compiler, this.options);
}
};
function handleHook(compiler, option) {
const hook = resolveWhen(option.when);
compiler.plugin(hook, function(params) {
del.sync(option.what);
log(option.when, 'Successfully deleted ' + option.what + ' assets.\n');
});
}
function log(type, msg) {
let collor = null;
switch (type) {
case 'warning':
collor = '\x1b[33m%s\x1b[0m'
break;
case 'success':
collor = '\x1b[32m%s\x1b[0m';
break;
case 'error':
collor = '\x1b[31m%s\x1b[0m';
break;
default:
collor = '\x1b[34m%s\x1b[0m';
break;
}
console.error(collor, 'WebpackDelPlugin::'+ type + ': ' + msg);
}
function isValidOption(option) {
if (typeof option !== 'object') {
log('error', 'Specified option' + option + ' must be object type.');
return false;
}
if (!option.what || !option.when) {
log('error', 'Specified option\n' + JSON.stringify(option, null, 2) + '\nmust have fields "what" and "when".')
return false;
}
return true;
}
function isValidOptions(options) {
if (Array.isArray(options)) {
return options.reduce(function(isValid, option) {
return isValid && isValidOption(option);
}, true);
} else if (typeof options === 'object') {
return isValidOption(options);
}
return false
}
function resolveWhen(alias) {
switch (alias) {
case ALIAS__BEFORE_COMPILATION:
return 'compile';
case ALIAS__AFTER_COMPILATION:
return 'done';
default:
return 'compile';
}
}
module.exports = WebpackDelPlugin;