-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
110 lines (98 loc) · 3.58 KB
/
index.ts
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
105
106
107
108
109
110
'use strict';
import {version, Compilation, Compiler} from 'webpack';
class IgnoreEmitPlugin {
private readonly options: { debug?: boolean };
private readonly DEBUG: boolean;
private readonly ignorePatterns: RegExp[];
constructor(ignoreRegex: RegExp | string | Array<RegExp | string> = [], options: { debug?: boolean; } = {}) {
if (!ignoreRegex || Array.isArray(ignoreRegex) && !ignoreRegex.length) {
throw new Error(`IgnoreEmitPlugin: Must include patterns to ignore`);
}
this.options = options;
this.DEBUG = !!this.options.debug;
this.ignorePatterns = this.normalizeRegex(ignoreRegex);
}
private normalizeRegex(regex: RegExp | string | Array<RegExp | string>): RegExp[] {
if (regex instanceof RegExp) {
return [regex];
} else if (Array.isArray(regex)) {
const normalizedList = [];
for (const input of regex) {
normalizedList.push(...this.normalizeRegex(input));
}
return normalizedList;
} else if (typeof regex === 'string') {
// escape special chars and create a regex instance
return [new RegExp(regex.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'))];
}
throw new Error('IgnoreEmitPlugin: invalid ignore pattern - must be {RegExp|string|Array<RegExp|string>');
}
private checkIgnore(assetName: string, ignorePatterns: RegExp[]): boolean {
return ignorePatterns.some(pattern => {
if (Array.isArray(pattern)) {
return this.checkIgnore(assetName, pattern);
}
return pattern.test(assetName);
});
}
public apply(compiler: Compiler) {
const ignoreAssets = (compilation: Compilation) => {
Object.keys(compilation.assets).forEach(assetName => {
if (this.checkIgnore(assetName, this.ignorePatterns)) {
this.DEBUG && console.log(`IgnoreEmitPlugin: Ignoring asset ${assetName}`);
if (typeof compilation.deleteAsset === 'function') {
// Webpack 5
compilation.deleteAsset(assetName);
} else {
// older versions
delete compilation.assets[assetName];
}
}
});
};
// webpack 5
if (compiler.hooks && compiler.hooks.compilation && version && Number(version[0]) > 4) {
this.DEBUG && console.log('Using Webpack5 format');
compiler.hooks.compilation.tap(
'IgnoreEmitPlugin',
(compilation: Compilation) => {
if (compilation.hooks.processAssets) {
compilation.hooks.processAssets.tap(
{
name: 'IgnoreEmitPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
},
() => {
ignoreAssets(compilation);
}
);
} else {
compilation.hooks.additionalAssets.tap('IgnoreEmitPlugin',
() => ignoreAssets(compilation)
);
}
}
);
}
// webpack 4
else if (compiler.hooks && compiler.hooks.emit) {
this.DEBUG && console.log('Using Webpack4 format');
compiler.hooks.emit.tap('IgnoreEmitPlugin', ignoreAssets);
}
// webpack 3
else {
this.DEBUG && console.log('Using Webpack3 or older format');
// @ts-ignore - this signature does not exist on the latest webpack typing
compiler.plugin('emit', (compilation, callback) => {
ignoreAssets(compilation);
callback();
});
}
}
}
export {IgnoreEmitPlugin};
export default IgnoreEmitPlugin;
// support plain node require
module.exports = IgnoreEmitPlugin;
module.exports.default = IgnoreEmitPlugin;
module.exports.IgnoreEmitPlugin = IgnoreEmitPlugin;