UNPKG

3.66 kBPlain TextView Raw
1'use strict';
2
3import {version, Compilation, Compiler} from 'webpack';
4
5class IgnoreEmitPlugin {
6 private readonly options: { debug?: boolean };
7 private readonly DEBUG: boolean;
8 private readonly ignorePatterns: RegExp[];
9
10 constructor(ignoreRegex: RegExp | string | Array<RegExp | string> = [], options: { debug?: boolean; } = {}) {
11 if (!ignoreRegex || Array.isArray(ignoreRegex) && !ignoreRegex.length) {
12 throw new Error(`IgnoreEmitPlugin: Must include patterns to ignore`);
13 }
14
15 this.options = options;
16 this.DEBUG = !!this.options.debug;
17 this.ignorePatterns = this.normalizeRegex(ignoreRegex);
18 }
19
20 private normalizeRegex(regex: RegExp | string | Array<RegExp | string>): RegExp[] {
21 if (regex instanceof RegExp) {
22 return [regex];
23 } else if (Array.isArray(regex)) {
24 const normalizedList = [];
25 for (const input of regex) {
26 normalizedList.push(...this.normalizeRegex(input));
27 }
28 return normalizedList;
29 } else if (typeof regex === 'string') {
30 // escape special chars and create a regex instance
31 return [new RegExp(regex.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'))];
32 }
33
34 throw new Error('IgnoreEmitPlugin: invalid ignore pattern - must be {RegExp|string|Array<RegExp|string>');
35 }
36
37 private checkIgnore(assetName: string, ignorePatterns: RegExp[]): boolean {
38 return ignorePatterns.some(pattern => {
39 if (Array.isArray(pattern)) {
40 return this.checkIgnore(assetName, pattern);
41 }
42
43 return pattern.test(assetName);
44 });
45 }
46
47 public apply(compiler: Compiler) {
48 const ignoreAssets = (compilation: Compilation) => {
49 Object.keys(compilation.assets).forEach(assetName => {
50 if (this.checkIgnore(assetName, this.ignorePatterns)) {
51 this.DEBUG && console.log(`IgnoreEmitPlugin: Ignoring asset ${assetName}`);
52 if (typeof compilation.deleteAsset === 'function') {
53 // Webpack 5
54 compilation.deleteAsset(assetName);
55 } else {
56 // older versions
57 delete compilation.assets[assetName];
58 }
59 }
60 });
61 };
62
63 // webpack 5
64 if (compiler.hooks && compiler.hooks.compilation && version && Number(version[0]) > 4) {
65 this.DEBUG && console.log('Using Webpack5 format');
66 compiler.hooks.compilation.tap(
67 'IgnoreEmitPlugin',
68 (compilation: Compilation) => {
69 if (compilation.hooks.processAssets) {
70 compilation.hooks.processAssets.tap(
71 {
72 name: 'IgnoreEmitPlugin',
73 stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
74 },
75 () => {
76 ignoreAssets(compilation);
77 }
78 );
79 } else {
80 compilation.hooks.additionalAssets.tap('IgnoreEmitPlugin',
81 () => ignoreAssets(compilation)
82 );
83 }
84 }
85 );
86 }
87 // webpack 4
88 else if (compiler.hooks && compiler.hooks.emit) {
89 this.DEBUG && console.log('Using Webpack4 format');
90 compiler.hooks.emit.tap('IgnoreEmitPlugin', ignoreAssets);
91 }
92 // webpack 3
93 else {
94 this.DEBUG && console.log('Using Webpack3 or older format');
95 // @ts-ignore - this signature does not exist on the latest webpack typing
96 compiler.plugin('emit', (compilation, callback) => {
97 ignoreAssets(compilation);
98 callback();
99 });
100 }
101 }
102}
103
104export {IgnoreEmitPlugin};
105export default IgnoreEmitPlugin;
106
107// support plain node require
108module.exports = IgnoreEmitPlugin;
109module.exports.default = IgnoreEmitPlugin;
110module.exports.IgnoreEmitPlugin = IgnoreEmitPlugin;