mirror of https://github.com/webpack/webpack.git
ES6ify the ContextReplacementPlugin
This commit is contained in:
parent
5dd1d0cb13
commit
376d31fb6c
|
|
@ -2,102 +2,110 @@
|
|||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
var path = require("path");
|
||||
var ContextElementDependency = require("./dependencies/ContextElementDependency");
|
||||
"use strict";
|
||||
|
||||
function ContextReplacementPlugin(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 = function(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;
|
||||
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;
|
||||
}
|
||||
if(typeof newContentRecursive !== "boolean") {
|
||||
newContentRegExp = newContentRecursive;
|
||||
newContentRecursive = undefined;
|
||||
}
|
||||
this.newContentResource = newContentResource;
|
||||
this.newContentRecursive = newContentRecursive;
|
||||
this.newContentRegExp = newContentRegExp;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
compiler.plugin("context-module-factory", (cmf) => {
|
||||
cmf.plugin("before-resolve", (result, callback) => {
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
return callback(null, result);
|
||||
});
|
||||
cmf.plugin("after-resolve", (result, callback) => {
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
return callback(null, result);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
module.exports = ContextReplacementPlugin;
|
||||
ContextReplacementPlugin.prototype.apply = function(compiler) {
|
||||
var resourceRegExp = this.resourceRegExp;
|
||||
var newContentCallback = this.newContentCallback;
|
||||
var newContentResource = this.newContentResource;
|
||||
var newContentRecursive = this.newContentRecursive;
|
||||
var newContentRegExp = this.newContentRegExp;
|
||||
var newContentCreateContextMap = this.newContentCreateContextMap;
|
||||
compiler.plugin("context-module-factory", function(cmf) {
|
||||
cmf.plugin("before-resolve", function(result, callback) {
|
||||
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(function(d) {
|
||||
if(d.critical)
|
||||
d.critical = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
return callback(null, result);
|
||||
});
|
||||
cmf.plugin("after-resolve", function(result, callback) {
|
||||
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") {
|
||||
var origResource = result.resource;
|
||||
newContentCallback(result);
|
||||
if(result.resource !== origResource) {
|
||||
result.resource = path.resolve(origResource, result.resource);
|
||||
}
|
||||
} else {
|
||||
result.dependencies.forEach(function(d) {
|
||||
if(d.critical)
|
||||
d.critical = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
return callback(null, result);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function createResolveDependenciesFromContextMap(createContextMap) {
|
||||
const createResolveDependenciesFromContextMap = (createContextMap) => {
|
||||
return function resolveDependenciesFromContextMap(fs, resource, recursive, regExp, callback) {
|
||||
createContextMap(fs, function(err, map) {
|
||||
createContextMap(fs, (err, map) => {
|
||||
if(err) return callback(err);
|
||||
var dependencies = Object.keys(map).map(function(key) {
|
||||
const dependencies = Object.keys(map).map((key) => {
|
||||
return new ContextElementDependency(map[key], key);
|
||||
});
|
||||
callback(null, dependencies);
|
||||
});
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = ContextReplacementPlugin;
|
||||
|
|
|
|||
Loading…
Reference in New Issue