-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (43 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
'use strict';
const ModuleDependencyWarning = require('webpack/lib/ModuleDependencyWarning');
const ANY_PATH = /./;
const EXPORT_NOT_FOUND_REG_EXP = /export.*was not found in/;
module.exports = class IgnoreNotFoundExportPlugin {
/**
*
* @param {Object} options
* @param {RegExp | RegExp[]} [options.include=/./] A list of regular expressions
*/
constructor({ include = ANY_PATH } = {}) {
this.include = Array.isArray(include) ? include : [include];
this.include.forEach((matcher, i) => {
if (!(matcher instanceof RegExp)) {
throw new TypeError(
`IgnoreNotFoundExportPlugin: argument[${i}] must be an instance of RegExp.`,
);
}
});
}
isModuleDependencyWarning(warning) {
return (
warning instanceof ModuleDependencyWarning ||
warning.constructor.name === 'ModuleDependencyWarning'
);
}
isResourcePathAllowed(resourcePath) {
return this.include.some((regExp) => regExp.test(resourcePath));
}
apply(compiler) {
compiler.hooks.done.tap('IgnoreNotFoundExportPlugin', (stats) => {
// mutates `compilation.warnings` to remove ignored warnings
stats.compilation.warnings = stats.compilation.warnings.filter(
(warning) =>
!(
this.isModuleDependencyWarning(warning) &&
EXPORT_NOT_FOUND_REG_EXP.test(warning.message) &&
this.isResourcePathAllowed(warning.module.resource)
),
);
});
}
};