IgnorePlugin: add context argument to checkResource, deprecate checkContext

This commit is contained in:
Ilya Kantor 2018-12-17 20:40:51 +03:00
parent d5e26f728a
commit a9d465f183
23 changed files with 75 additions and 53 deletions

View File

@ -21,7 +21,7 @@ export type IgnorePluginOptions =
*/
checkContext?: ((context: string) => boolean);
/**
* A filter function for resource
* A filter function for resource and context
*/
checkResource?: ((resource: string) => boolean);
checkResource?: ((resource: string, context: string) => boolean);
};

View File

@ -30,61 +30,43 @@ class IgnorePlugin {
this.checkIgnore = this.checkIgnore.bind(this);
}
/**
* @param {string} resource resource
* @returns {boolean} returns true if a "resourceRegExp" exists
* and the resource given matches the regexp.
*/
checkResource(resource) {
if ("checkResource" in this.options && this.options.checkResource) {
return this.options.checkResource(resource);
}
if ("resourceRegExp" in this.options && this.options.resourceRegExp) {
return this.options.resourceRegExp.test(resource);
}
return false;
}
/**
* @param {string} context context
* @returns {boolean} returns true if "contextRegExp" does not exist
* or if context matches the given regexp.
*/
checkContext(context) {
if ("checkContext" in this.options && this.options.checkContext) {
return this.options.checkContext(context);
}
if ("contextRegExp" in this.options && this.options.contextRegExp) {
return this.options.contextRegExp.test(context);
}
return true;
}
/**
* Note that if "contextRegExp" is given, both the "resourceRegExp"
* and "contextRegExp" have to match.
*
* @param {TODO} result result
* @returns {boolean} returns true if result should be ignored
*/
checkResult(result) {
if (!result) {
return true;
}
return (
this.checkResource(result.request) && this.checkContext(result.context)
);
}
/**
* @param {TODO} result result
* @returns {TODO|null} returns result or null if result should be ignored
*/
checkIgnore(result) {
// check if result is ignored
if (this.checkResult(result)) {
return null;
if (
"checkResource" in this.options &&
this.options.checkResource(result.request, result.context)
) {
// TODO webpack 5 remove checkContext, as checkResource already gets context
if ("checkContext" in this.options) {
if (this.options.checkContext(result.context)) {
return null;
}
} else {
return null;
}
}
if (
"resourceRegExp" in this.options &&
this.options.resourceRegExp.test(result.request)
) {
if ("contextRegExp" in this.options) {
// if "contextRegExp" is given,
// both the "resourceRegExp" and "contextRegExp" have to match.
if (this.options.contextRegExp.test(result.context)) {
return null;
}
} else {
return null;
}
}
return result;
}

View File

@ -27,9 +27,9 @@
"tsType": "((context: string) => boolean)"
},
"checkResource": {
"description": "A filter function for resource",
"description": "A filter function for resource and context",
"instanceof": "Function",
"tsType": "((resource: string) => boolean)"
"tsType": "((resource: string, context: string) => boolean)"
}
}
}

View File

@ -6,10 +6,10 @@ module.exports = {
entry: "./test.js",
plugins: [
new IgnorePlugin({
checkResource: function(resource) {
checkResource(resource) {
return /ignored-module/.test(resource);
},
checkContext: function(context) {
checkContext(context) {
return /folder-b/.test(context);
}
})

View File

@ -6,7 +6,7 @@ module.exports = {
entry: "./test.js",
plugins: [
new IgnorePlugin({
checkResource: function(resource) {
checkResource(resource) {
return /ignored-module/.test(resource);
}
})

View File

@ -0,0 +1 @@
module.exports = "ignored";

View File

@ -0,0 +1 @@
module.exports = require("./normal-module");

View File

@ -0,0 +1 @@
module.exports = "ignored";

View File

@ -0,0 +1 @@
module.exports = require("./ignored-module");

View File

@ -0,0 +1 @@
module.exports = "should be fine";

View File

@ -0,0 +1 @@
module.exports = require("./only-context-match-require");

View File

@ -0,0 +1,20 @@
/* globals it */
"use strict";
it("should ignore resources that match resource regex and context", function() {
expect(function() {
require("./folder-b/normal-module");
}).toThrowError();
});
it("should not ignore resources that match resource but not context", function() {
expect(function() {
require("./folder-a/normal-module");
}).not.toThrowError();
});
it("should not ignore resources that do not match resource but do match context", function() {
expect(function() {
require("./folder-b/only-context-match");
}).not.toThrowError();
});

View File

@ -0,0 +1,14 @@
"use strict";
const IgnorePlugin = require("../../../../lib/IgnorePlugin");
module.exports = {
entry: "./test.js",
plugins: [
new IgnorePlugin({
checkResource(resource, context) {
return /ignored-module/.test(resource) && /folder-b/.test(context);
}
})
]
};