UNPKG

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