Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IgnorePlugin): allow user to provide his own check functions
The user can provide checkResource and checkContext functions that will be called with current resource or context respectively. These functions should return a boolean to decide whether the module should be ignored or not. Now the constructor can accept a single option object. ``` const resourceRegExp = /a_regex/ const contextRegExp = /another_regex/ // before: new webpack.IgnorePlugin(resourceRegExp, contextRegExp) // alternative: new webpack.ignorePlugin({resourceRegExp, contextRegExp}) ``` Note that from Webpack 5, only passing an object will be allowed, so we could encourage people to migrate already.
- Loading branch information
Showing
27 changed files
with
210 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"type": "object", | ||
"oneOf": [ | ||
{ | ||
"additionalProperties": false, | ||
"properties": { | ||
"resourceRegExp": { | ||
"description": "A RegExp to test the request against", | ||
"instanceof": "RegExp" | ||
}, | ||
"contextRegExp": { | ||
"description": "A RegExp to test the context (directory) against", | ||
"instanceof": "RegExp" | ||
} | ||
} | ||
}, | ||
{ | ||
"additionalProperties": false, | ||
"properties": { | ||
"checkResource": { | ||
"description": "A filter function for resource", | ||
"instanceof": "Function" | ||
}, | ||
"checkContext": { | ||
"description": "A filter function for context", | ||
"instanceof": "Function" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/checkContextFn/folder-a/ignored-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ignored"; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/checkContextFn/folder-a/normal-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./normal-module"); |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/checkContextFn/folder-b/ignored-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ignored"; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/checkContextFn/folder-b/normal-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./ignored-module"); |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/checkContextFn/folder-b/only-context-match-require.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "should be fine"; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/checkContextFn/folder-b/only-context-match.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./only-context-match-require"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const IgnorePlugin = require("../../../../lib/IgnorePlugin"); | ||
|
||
module.exports = { | ||
entry: "./test.js", | ||
plugins: [ | ||
new IgnorePlugin({ | ||
checkResource: function(resource) { | ||
return /ignored-module/.test(resource); | ||
}, | ||
checkContext: function(context) { | ||
return /folder-b/.test(context); | ||
} | ||
}) | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ignored"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "normal"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* globals it */ | ||
"use strict"; | ||
|
||
it("should ignore ignored resources", function() { | ||
expect(function() { | ||
require("./ignored-module"); | ||
}).toThrowError(); | ||
}); | ||
it("should not ignore resources that do not match", function() { | ||
expect(function() { | ||
require("./normal-module"); | ||
}).not.toThrowError(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
|
||
const IgnorePlugin = require("../../../../lib/IgnorePlugin"); | ||
|
||
module.exports = { | ||
entry: "./test.js", | ||
plugins: [ | ||
new IgnorePlugin({ | ||
checkResource: function(resource) { | ||
return /ignored-module/.test(resource); | ||
} | ||
}) | ||
] | ||
}; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/compatibilityLayer/folder-a/ignored-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ignored"; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/compatibilityLayer/folder-a/normal-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./normal-module"); |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/compatibilityLayer/folder-b/ignored-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "ignored"; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/compatibilityLayer/folder-b/normal-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./ignored-module"); |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/compatibilityLayer/folder-b/only-context-match-require.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "should be fine"; |
1 change: 1 addition & 0 deletions
1
test/configCases/ignore/compatibilityLayer/folder-b/only-context-match.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./only-context-match-require"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* globals it */ | ||
"use strict"; | ||
|
||
// TODO: remove in webpack 5 | ||
it("should ignore context modules that match resource regex and context (compat-layer)", function() { | ||
const folderBContext = function(mod) { | ||
require("./folder-b/" + mod); | ||
}; | ||
|
||
expect(function() { | ||
folderBContext("normal-module"); | ||
}).toThrowError(); | ||
}); | ||
|
||
it("should not ignore context modules that dont match the resource (compat-layer)", function() { | ||
const folderBContext = function(mod) { | ||
require("./folder-b/" + mod); | ||
}; | ||
|
||
expect(function() { | ||
folderBContext("only-context-match"); | ||
}).not.toThrowError(); | ||
}); | ||
|
||
it("should not ignore context modules that dont match the context (compat-layer)", function() { | ||
const folderBContext = function(mod) { | ||
require("./folder-a/" + mod); | ||
}; | ||
|
||
expect(function() { | ||
folderBContext("normal-module"); | ||
}).not.toThrowError(); | ||
expect(function() { | ||
folderBContext("ignored-module"); | ||
}).not.toThrowError(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
const IgnorePlugin = require("../../../../lib/IgnorePlugin"); | ||
|
||
module.exports = { | ||
entry: "./test.js", | ||
plugins: [new IgnorePlugin(/ignored-module/, /folder-b/)] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters