mirror of https://github.com/webpack/webpack.git
unite logic for "normal-module-factory" and "context-module-factory"
This commit is contained in:
parent
c4835a48dd
commit
8ad4a2004e
|
@ -8,30 +8,60 @@ class IgnorePlugin {
|
||||||
constructor(resourceRegExp, contextRegExp) {
|
constructor(resourceRegExp, contextRegExp) {
|
||||||
this.resourceRegExp = resourceRegExp;
|
this.resourceRegExp = resourceRegExp;
|
||||||
this.contextRegExp = contextRegExp;
|
this.contextRegExp = contextRegExp;
|
||||||
|
|
||||||
|
this.checkIgnore = this.checkIgnore.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Only returns true if a "resourceRegExp" exists
|
||||||
|
* and the resource given matches the regexp.
|
||||||
|
*/
|
||||||
|
checkResouce(resource) {
|
||||||
|
if(!this.resourceRegExp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.resourceRegExp.test(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns true if contextRegExp does not exist
|
||||||
|
* or if context matches the given regexp.
|
||||||
|
*/
|
||||||
|
checkContext(context) {
|
||||||
|
if(!this.contextRegExp) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return this.contextRegExp.test(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns true if result should be ignored.
|
||||||
|
* false if it shouldn't.
|
||||||
|
*
|
||||||
|
* Not that if "contextRegExp" is given, both the "resourceRegExp"
|
||||||
|
* and "contextRegExp" have to match.
|
||||||
|
*/
|
||||||
|
checkResult(result, callback) {
|
||||||
|
if(!result) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return this.checkResouce(result.request) && this.checkContext(result.context);
|
||||||
|
}
|
||||||
|
|
||||||
|
checkIgnore(result, callback) {
|
||||||
|
// check if result is ignored
|
||||||
|
if(this.checkResult(result)) {
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
return callback(null, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(compiler) {
|
apply(compiler) {
|
||||||
const resourceRegExp = this.resourceRegExp;
|
|
||||||
const contextRegExp = this.contextRegExp;
|
|
||||||
compiler.plugin("normal-module-factory", (nmf) => {
|
compiler.plugin("normal-module-factory", (nmf) => {
|
||||||
nmf.plugin("before-resolve", (result, callback) => {
|
nmf.plugin("before-resolve", this.checkIgnore);
|
||||||
if(!result) return callback();
|
|
||||||
if(resourceRegExp.test(result.request) &&
|
|
||||||
(!contextRegExp || contextRegExp.test(result.context))) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
return callback(null, result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
compiler.plugin("context-module-factory", (cmf) => {
|
compiler.plugin("context-module-factory", (cmf) => {
|
||||||
cmf.plugin("before-resolve", (result, callback) => {
|
cmf.plugin("before-resolve", this.checkIgnore);
|
||||||
if(!result) return callback();
|
|
||||||
if(resourceRegExp.test(result.request) &&
|
|
||||||
(!contextRegExp || contextRegExp.test(result.context))) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
return callback(null, result);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue