Skip to content

Commit 84c639e

Browse files
authored
Merge pull request webpack#4669 from timse/IgnorePluginHotfix
Ignore plugin hotfix - continuation of 4418
2 parents 39ed23f + e41c0f0 commit 84c639e

25 files changed

Lines changed: 193 additions & 17 deletions

lib/IgnorePlugin.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,60 @@ class IgnorePlugin {
88
constructor(resourceRegExp, contextRegExp) {
99
this.resourceRegExp = resourceRegExp;
1010
this.contextRegExp = contextRegExp;
11+
12+
this.checkIgnore = this.checkIgnore.bind(this);
13+
}
14+
15+
/*
16+
* Only returns true if a "resourceRegExp" exists
17+
* and the resource given matches the regexp.
18+
*/
19+
checkResouce(resource) {
20+
if(!this.resourceRegExp) {
21+
return false;
22+
}
23+
return this.resourceRegExp.test(resource);
24+
}
25+
26+
/*
27+
* Returns true if contextRegExp does not exist
28+
* or if context matches the given regexp.
29+
*/
30+
checkContext(context) {
31+
if(!this.contextRegExp) {
32+
return true;
33+
}
34+
return this.contextRegExp.test(context);
35+
}
36+
37+
/*
38+
* Returns true if result should be ignored.
39+
* false if it shouldn't.
40+
*
41+
* Not that if "contextRegExp" is given, both the "resourceRegExp"
42+
* and "contextRegExp" have to match.
43+
*/
44+
checkResult(result) {
45+
if(!result) {
46+
return true;
47+
}
48+
return this.checkResouce(result.request) && this.checkContext(result.context);
49+
}
50+
51+
checkIgnore(result, callback) {
52+
// check if result is ignored
53+
if(this.checkResult(result)) {
54+
return callback();
55+
}
56+
return callback(null, result);
1157
}
1258

1359
apply(compiler) {
14-
const resourceRegExp = this.resourceRegExp;
15-
const contextRegExp = this.contextRegExp;
1660
compiler.plugin("normal-module-factory", (nmf) => {
17-
nmf.plugin("before-resolve", (result, callback) => {
18-
if(!result) return callback();
19-
if(resourceRegExp.test(result.request) &&
20-
(!contextRegExp || contextRegExp.test(result.context))) {
21-
return callback();
22-
}
23-
return callback(null, result);
24-
});
61+
nmf.plugin("before-resolve", this.checkIgnore);
2562
});
2663
compiler.plugin("context-module-factory", (cmf) => {
27-
cmf.plugin("before-resolve", (result, callback) => {
28-
if(!result) return callback();
29-
if(resourceRegExp.test(result.request)) {
30-
return callback();
31-
}
32-
return callback(null, result);
33-
});
64+
cmf.plugin("before-resolve", this.checkIgnore);
3465
});
3566
}
3667
}
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 = "normal";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* globals it */
2+
"use strict";
3+
4+
it("should ignore ignored resources", function() {
5+
const folderBContext = function(mod) {
6+
require("./src/" + mod);
7+
};
8+
9+
(function() {
10+
folderBContext("ignored-module");
11+
}).should.throw();
12+
});
13+
it("should not ignore resources that do not match", function() {
14+
const folderBContext = function(mod) {
15+
require("./src/" + mod);
16+
};
17+
18+
(function() {
19+
folderBContext("normal-module");
20+
}).should.not.throw();
21+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
const IgnorePlugin = require("../../../../lib/IgnorePlugin");
4+
5+
module.exports = {
6+
entry: "./test.js",
7+
plugins: [
8+
new IgnorePlugin(/ignored-module/)
9+
],
10+
};
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 = "normal";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* globals it */
2+
"use strict";
3+
4+
it("should ignore ignored resources", function() {
5+
(function() {
6+
require("./ignored-module");
7+
}).should.throw();
8+
});
9+
it("should not ignore resources that do not match", function() {
10+
(function() {
11+
require("./normal-module");
12+
}).should.not.throw();
13+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
const IgnorePlugin = require("../../../../lib/IgnorePlugin");
4+
5+
module.exports = {
6+
entry: "./test.js",
7+
plugins: [
8+
new IgnorePlugin(/ignored-module/)
9+
],
10+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "ignored";

0 commit comments

Comments
 (0)