Skip to content

Commit 45e798a

Browse files
authored
Merge pull request webpack#7827 from AoDev/feat/ignore-plugin-check-fn
feat(IgnorePlugin): allow user to provide his own check functions
2 parents ed96915 + 6235e99 commit 45e798a

File tree

27 files changed

+210
-16
lines changed

27 files changed

+210
-16
lines changed

lib/IgnorePlugin.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@
44
*/
55
"use strict";
66

7+
const validateOptions = require("schema-utils");
8+
const schema = require("../schemas/plugins/IgnorePlugin.json");
9+
710
/** @typedef {import("./Compiler")} Compiler */
811

912
class IgnorePlugin {
1013
/**
11-
* @param {RegExp} resourceRegExp A RegExp to test the request against
12-
* @param {RegExp=} contextRegExp A RegExp to test the context (directory) against
14+
* @param {object} options IgnorePlugin options
15+
* @param {RegExp} options.resourceRegExp - A RegExp to test the request against
16+
* @param {RegExp} options.contextRegExp - A RegExp to test the context (directory) against
17+
* @param {function(string): boolean=} options.checkResource - A filter function for resource
18+
* @param {function(string): boolean=} options.checkContext - A filter function for context
1319
*/
14-
constructor(resourceRegExp, contextRegExp) {
15-
/** @private @type {RegExp} */
16-
this.resourceRegExp = resourceRegExp;
17-
/** @private @type {RegExp} */
18-
this.contextRegExp = contextRegExp;
20+
constructor(options) {
21+
// TODO webpack 5 remove this compat-layer
22+
if (arguments.length > 1 || options instanceof RegExp) {
23+
options = {
24+
resourceRegExp: arguments[0],
25+
contextRegExp: arguments[1]
26+
};
27+
}
28+
29+
validateOptions(schema, options, "IgnorePlugin");
30+
this.options = options;
1931

2032
/** @private @type {Function} */
2133
this.checkIgnore = this.checkIgnore.bind(this);
@@ -27,10 +39,13 @@ class IgnorePlugin {
2739
* and the resource given matches the regexp.
2840
*/
2941
checkResource(resource) {
30-
if (!this.resourceRegExp) {
42+
if (this.options.checkResource) {
43+
return this.options.checkResource(resource);
44+
}
45+
if (!this.options.resourceRegExp) {
3146
return false;
3247
}
33-
return this.resourceRegExp.test(resource);
48+
return this.options.resourceRegExp.test(resource);
3449
}
3550

3651
/**
@@ -39,10 +54,14 @@ class IgnorePlugin {
3954
* or if context matches the given regexp.
4055
*/
4156
checkContext(context) {
42-
if (!this.contextRegExp) {
57+
if (this.options.checkContext) {
58+
return this.options.checkContext(context);
59+
}
60+
61+
if (!this.options.contextRegExp) {
4362
return true;
4463
}
45-
return this.contextRegExp.test(context);
64+
return this.options.contextRegExp.test(context);
4665
}
4766

4867
/**

schemas/plugins/IgnorePlugin.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"type": "object",
3+
"oneOf": [
4+
{
5+
"additionalProperties": false,
6+
"properties": {
7+
"resourceRegExp": {
8+
"description": "A RegExp to test the request against",
9+
"instanceof": "RegExp"
10+
},
11+
"contextRegExp": {
12+
"description": "A RegExp to test the context (directory) against",
13+
"instanceof": "RegExp"
14+
}
15+
}
16+
},
17+
{
18+
"additionalProperties": false,
19+
"properties": {
20+
"checkResource": {
21+
"description": "A filter function for resource",
22+
"instanceof": "Function"
23+
},
24+
"checkContext": {
25+
"description": "A filter function for context",
26+
"instanceof": "Function"
27+
}
28+
}
29+
}
30+
]
31+
}

test/configCases/errors/multi-entry-missing-module/webpack.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ module.exports = {
77
output: {
88
filename: "[name].js"
99
},
10-
plugins: [new IgnorePlugin(new RegExp(/intentionally-missing-module/))],
10+
plugins: [
11+
new IgnorePlugin({
12+
resourceRegExp: new RegExp(/intentionally-missing-module/)
13+
})
14+
],
1115
node: {
1216
__dirname: false
1317
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "ignored";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./normal-module");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "ignored";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./ignored-module");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "should be fine";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./only-context-match-require");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* globals it */
2+
"use strict";
3+
4+
it("should ignore resources that match resource regex and context", function() {
5+
expect(function() {
6+
require("./folder-b/normal-module");
7+
}).toThrowError();
8+
});
9+
10+
it("should not ignore resources that match resource but not context", function() {
11+
expect(function() {
12+
require("./folder-a/normal-module");
13+
}).not.toThrowError();
14+
});
15+
16+
it("should not ignore resources that do not match resource but do match context", function() {
17+
expect(function() {
18+
require("./folder-b/only-context-match");
19+
}).not.toThrowError();
20+
});

0 commit comments

Comments
 (0)