UNPKG

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