webpack/lib/ContextReplacementPlugin.js

113 lines
4.0 KiB
JavaScript
Raw Normal View History

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";
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
}
}
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 04:35:39 +08:00
cmf.hooks.beforeResolve.tapAsync("ContextReplacementPlugin", (result, callback) => {
2017-03-11 01:27:53 +08:00
if(!result) return callback();
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 {
result.dependencies.forEach((d) => {
if(d.critical)
d.critical = false;
});
}
2014-10-23 16:58:26 +08:00
}
2017-03-11 01:27:53 +08:00
return callback(null, result);
});
2017-12-14 04:35:39 +08:00
cmf.hooks.afterResolve.tapAsync("ContextReplacementPlugin", (result, callback) => {
2017-03-11 01:27:53 +08:00
if(!result) return callback();
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 {
result.dependencies.forEach((d) => {
if(d.critical)
d.critical = false;
});
2014-10-23 16:58:26 +08:00
}
}
2017-03-11 01:27:53 +08:00
return callback(null, result);
});
2013-01-31 09:33:11 +08:00
});
2017-03-11 01:27:53 +08:00
}
}
2017-03-11 01:27:53 +08:00
const createResolveDependenciesFromContextMap = (createContextMap) => {
const resolveDependenciesFromContextMap = (fs, options, callback) => {
2017-03-11 01:27:53 +08:00
createContextMap(fs, (err, map) => {
if(err) return callback(err);
2017-03-11 01:27:53 +08:00
const dependencies = Object.keys(map).map((key) => {
return new ContextElementDependency(map[key] + options.resourceQuery, key);
});
callback(null, dependencies);
});
2017-01-11 17:51:58 +08:00
};
return resolveDependenciesFromContextMap;
2017-03-11 01:27:53 +08:00
};
module.exports = ContextReplacementPlugin;