2013-01-31 09:33:11 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-03-11 01:27:53 +08:00
|
|
|
"use strict";
|
2014-06-18 04:46:24 +08:00
|
|
|
|
2017-03-11 01:27:53 +08:00
|
|
|
const path = require("path");
|
|
|
|
const ContextElementDependency = require("./dependencies/ContextElementDependency");
|
|
|
|
|
|
|
|
class ContextReplacementPlugin {
|
|
|
|
constructor(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) {
|
|
|
|
this.resourceRegExp = resourceRegExp;
|
|
|
|
|
|
|
|
if(typeof newContentResource === "function") {
|
|
|
|
this.newContentCallback = newContentResource;
|
|
|
|
} else if(typeof newContentResource === "string" && typeof newContentRecursive === "object") {
|
|
|
|
this.newContentResource = newContentResource;
|
|
|
|
this.newContentCreateContextMap = (fs, callback) => {
|
|
|
|
callback(null, newContentRecursive);
|
|
|
|
};
|
|
|
|
} else if(typeof newContentResource === "string" && typeof newContentRecursive === "function") {
|
|
|
|
this.newContentResource = newContentResource;
|
|
|
|
this.newContentCreateContextMap = newContentRecursive;
|
|
|
|
} else {
|
|
|
|
if(typeof newContentResource !== "string") {
|
|
|
|
newContentRegExp = newContentRecursive;
|
|
|
|
newContentRecursive = newContentResource;
|
|
|
|
newContentResource = undefined;
|
|
|
|
}
|
|
|
|
if(typeof newContentRecursive !== "boolean") {
|
|
|
|
newContentRegExp = newContentRecursive;
|
|
|
|
newContentRecursive = undefined;
|
|
|
|
}
|
|
|
|
this.newContentResource = newContentResource;
|
|
|
|
this.newContentRecursive = newContentRecursive;
|
|
|
|
this.newContentRegExp = newContentRegExp;
|
2014-10-23 16:58:26 +08:00
|
|
|
}
|
2014-06-18 04:38:17 +08:00
|
|
|
}
|
2017-03-11 01:27:53 +08:00
|
|
|
|
|
|
|
apply(compiler) {
|
|
|
|
const resourceRegExp = this.resourceRegExp;
|
|
|
|
const newContentCallback = this.newContentCallback;
|
|
|
|
const newContentResource = this.newContentResource;
|
|
|
|
const newContentRecursive = this.newContentRecursive;
|
|
|
|
const newContentRegExp = this.newContentRegExp;
|
|
|
|
const newContentCreateContextMap = this.newContentCreateContextMap;
|
|
|
|
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.contextModuleFactory.tap("ContextReplacementPlugin", (cmf) => {
|
2017-12-14 21:38:53 +08:00
|
|
|
cmf.hooks.beforeResolve.tap("ContextReplacementPlugin", (result) => {
|
|
|
|
if(!result) return;
|
2017-03-11 01:27:53 +08:00
|
|
|
if(resourceRegExp.test(result.request)) {
|
|
|
|
if(typeof newContentResource !== "undefined")
|
|
|
|
result.request = newContentResource;
|
|
|
|
if(typeof newContentRecursive !== "undefined")
|
|
|
|
result.recursive = newContentRecursive;
|
|
|
|
if(typeof newContentRegExp !== "undefined")
|
|
|
|
result.regExp = newContentRegExp;
|
|
|
|
if(typeof newContentCallback === "function") {
|
|
|
|
newContentCallback(result);
|
|
|
|
} else {
|
2018-01-22 20:52:43 +08:00
|
|
|
for(const d of result.dependencies) {
|
2017-03-11 01:27:53 +08:00
|
|
|
if(d.critical)
|
|
|
|
d.critical = false;
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-03-11 01:27:53 +08:00
|
|
|
}
|
2014-10-23 16:58:26 +08:00
|
|
|
}
|
2017-12-14 21:38:53 +08:00
|
|
|
return result;
|
2017-03-11 01:27:53 +08:00
|
|
|
});
|
2017-12-14 21:38:53 +08:00
|
|
|
cmf.hooks.afterResolve.tap("ContextReplacementPlugin", (result) => {
|
|
|
|
if(!result) return;
|
2017-03-11 01:27:53 +08:00
|
|
|
if(resourceRegExp.test(result.resource)) {
|
|
|
|
if(typeof newContentResource !== "undefined")
|
|
|
|
result.resource = path.resolve(result.resource, newContentResource);
|
|
|
|
if(typeof newContentRecursive !== "undefined")
|
|
|
|
result.recursive = newContentRecursive;
|
|
|
|
if(typeof newContentRegExp !== "undefined")
|
|
|
|
result.regExp = newContentRegExp;
|
|
|
|
if(typeof newContentCreateContextMap === "function")
|
|
|
|
result.resolveDependencies = createResolveDependenciesFromContextMap(newContentCreateContextMap);
|
|
|
|
if(typeof newContentCallback === "function") {
|
|
|
|
const origResource = result.resource;
|
|
|
|
newContentCallback(result);
|
|
|
|
if(result.resource !== origResource) {
|
|
|
|
result.resource = path.resolve(origResource, result.resource);
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-22 20:52:43 +08:00
|
|
|
for(const d of result.dependencies) {
|
2017-03-11 01:27:53 +08:00
|
|
|
if(d.critical)
|
|
|
|
d.critical = false;
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2014-10-23 16:58:26 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 21:38:53 +08:00
|
|
|
return result;
|
2017-03-11 01:27:53 +08:00
|
|
|
});
|
2013-01-31 09:33:11 +08:00
|
|
|
});
|
2017-03-11 01:27:53 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 02:56:17 +08:00
|
|
|
|
2017-03-11 01:27:53 +08:00
|
|
|
const createResolveDependenciesFromContextMap = (createContextMap) => {
|
2017-11-09 03:49:41 +08:00
|
|
|
const resolveDependenciesFromContextMap = (fs, options, callback) => {
|
2017-03-11 01:27:53 +08:00
|
|
|
createContextMap(fs, (err, map) => {
|
2016-01-07 02:56:17 +08:00
|
|
|
if(err) return callback(err);
|
2017-03-11 01:27:53 +08:00
|
|
|
const dependencies = Object.keys(map).map((key) => {
|
2017-11-16 14:20:50 +08:00
|
|
|
return new ContextElementDependency(map[key] + options.resourceQuery, key);
|
2016-01-07 02:56:17 +08:00
|
|
|
});
|
|
|
|
callback(null, dependencies);
|
|
|
|
});
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2017-11-09 03:49:41 +08:00
|
|
|
return resolveDependenciesFromContextMap;
|
2017-03-11 01:27:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ContextReplacementPlugin;
|