-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
59 lines (49 loc) · 1.44 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
const { validate } = require('schema-utils');
const schema = {
type: 'object',
properties: {
options: {
type: 'array',
},
},
additionalProperties: false
};
class NoEmitPlugin {
constructor(options = []) {
if (this.isString(options)) {
options = [options];
}
validate(schema, { options }, { name: 'No Emit Plugin' });
this.options = options;
}
isString(target) {
return typeof target === 'string';
}
apply(compiler) {
compiler.hooks.compilation.tap('NoEmitPlugin', (compilation) => {
compilation.hooks.processAssets.tap({
name: 'NoEmitPlugin',
stage: compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
additionalAssets: true,
}, (assets) => {
// Put all assets in removal list.
if (this.options.length === 0) {
this.options = Object.keys(assets).filter((key) => key !== '*');
}
// Remove selected assets.
this.options.forEach((file) => {
if (!this.isString(file)) {
compilation.errors.push(Error(`All bundle names in the options must be strings. ${JSON.stringify(file)} is not a string.`));
return;
}
if (compilation.getAsset(file) === undefined) {
compilation.warnings.push(`Output asset does not exist: ${file}`);
return;
}
compilation.deleteAsset(file);
});
});
});
}
}
module.exports = NoEmitPlugin;