mirror of https://github.com/webpack/webpack.git
refactor(ES6): upgrade RequireContextPlugin to ES6
This commit is contained in:
parent
f745f02910
commit
bca84363f8
|
|
@ -2,74 +2,77 @@
|
||||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||||
Author Tobias Koppers @sokra
|
Author Tobias Koppers @sokra
|
||||||
*/
|
*/
|
||||||
var RequireContextDependency = require("./RequireContextDependency");
|
"use strict";
|
||||||
var ContextElementDependency = require("./ContextElementDependency");
|
|
||||||
|
|
||||||
var RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin");
|
const RequireContextDependency = require("./RequireContextDependency");
|
||||||
|
const ContextElementDependency = require("./ContextElementDependency");
|
||||||
|
|
||||||
function RequireContextPlugin(modulesDirectories, extensions) {
|
const RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin");
|
||||||
if(!Array.isArray(modulesDirectories))
|
|
||||||
throw new Error("modulesDirectories must be an array");
|
class RequireContextPlugin {
|
||||||
if(!Array.isArray(extensions))
|
constructor(modulesDirectories, extensions) {
|
||||||
throw new Error("extensions must be an array");
|
if(!Array.isArray(modulesDirectories))
|
||||||
this.modulesDirectories = modulesDirectories;
|
throw new Error("modulesDirectories must be an array");
|
||||||
this.extensions = extensions;
|
if(!Array.isArray(extensions))
|
||||||
|
throw new Error("extensions must be an array");
|
||||||
|
this.modulesDirectories = modulesDirectories;
|
||||||
|
this.extensions = extensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
apply(compiler) {
|
||||||
|
const modulesDirectories = this.modulesDirectories;
|
||||||
|
const extensions = this.extensions;
|
||||||
|
compiler.plugin("compilation", (compilation, params) => {
|
||||||
|
const contextModuleFactory = params.contextModuleFactory;
|
||||||
|
const normalModuleFactory = params.normalModuleFactory;
|
||||||
|
|
||||||
|
compilation.dependencyFactories.set(RequireContextDependency, contextModuleFactory);
|
||||||
|
compilation.dependencyTemplates.set(RequireContextDependency, new RequireContextDependency.Template());
|
||||||
|
|
||||||
|
compilation.dependencyFactories.set(ContextElementDependency, normalModuleFactory);
|
||||||
|
|
||||||
|
params.normalModuleFactory.plugin("parser", (parser, parserOptions) => {
|
||||||
|
|
||||||
|
if(typeof parserOptions.requireContext !== "undefined" && !parserOptions.requireContext)
|
||||||
|
return;
|
||||||
|
|
||||||
|
parser.apply(new RequireContextDependencyParserPlugin());
|
||||||
|
});
|
||||||
|
|
||||||
|
params.contextModuleFactory.plugin("alternatives", (items, callback) => {
|
||||||
|
if(items.length === 0) return callback(null, items);
|
||||||
|
|
||||||
|
callback(null, items.map((obj) => {
|
||||||
|
return extensions.filter((ext) => {
|
||||||
|
const l = obj.request.length;
|
||||||
|
return l > ext.length && obj.request.substr(l - ext.length, l) === ext;
|
||||||
|
}).map((ext) => {
|
||||||
|
const l = obj.request.length;
|
||||||
|
return {
|
||||||
|
context: obj.context,
|
||||||
|
request: obj.request.substr(0, l - ext.length)
|
||||||
|
};
|
||||||
|
}).concat(obj);
|
||||||
|
}).reduce((a, b) => a.concat(b), []));
|
||||||
|
});
|
||||||
|
|
||||||
|
params.contextModuleFactory.plugin("alternatives", (items, callback) => {
|
||||||
|
if(items.length === 0) return callback(null, items);
|
||||||
|
|
||||||
|
callback(null, items.map((obj) => {
|
||||||
|
for(let i = 0; i < modulesDirectories.length; i++) {
|
||||||
|
const dir = modulesDirectories[i];
|
||||||
|
const idx = obj.request.indexOf("./" + dir + "/");
|
||||||
|
if(idx === 0) {
|
||||||
|
obj.request = obj.request.slice(dir.length + 3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
module.exports = RequireContextPlugin;
|
module.exports = RequireContextPlugin;
|
||||||
|
|
||||||
RequireContextPlugin.prototype.apply = function(compiler) {
|
|
||||||
var modulesDirectories = this.modulesDirectories;
|
|
||||||
var extensions = this.extensions;
|
|
||||||
compiler.plugin("compilation", function(compilation, params) {
|
|
||||||
var contextModuleFactory = params.contextModuleFactory;
|
|
||||||
var normalModuleFactory = params.normalModuleFactory;
|
|
||||||
|
|
||||||
compilation.dependencyFactories.set(RequireContextDependency, contextModuleFactory);
|
|
||||||
compilation.dependencyTemplates.set(RequireContextDependency, new RequireContextDependency.Template());
|
|
||||||
|
|
||||||
compilation.dependencyFactories.set(ContextElementDependency, normalModuleFactory);
|
|
||||||
|
|
||||||
params.normalModuleFactory.plugin("parser", function(parser, parserOptions) {
|
|
||||||
|
|
||||||
if(typeof parserOptions.requireContext !== "undefined" && !parserOptions.requireContext)
|
|
||||||
return;
|
|
||||||
|
|
||||||
parser.apply(new RequireContextDependencyParserPlugin());
|
|
||||||
});
|
|
||||||
|
|
||||||
params.contextModuleFactory.plugin("alternatives", function(items, callback) {
|
|
||||||
if(items.length === 0) return callback(null, items);
|
|
||||||
|
|
||||||
callback(null, items.map(function(obj) {
|
|
||||||
return extensions.filter(function(ext) {
|
|
||||||
var l = obj.request.length;
|
|
||||||
return l > ext.length && obj.request.substr(l - ext.length, l) === ext;
|
|
||||||
}).map(function(ext) {
|
|
||||||
var l = obj.request.length;
|
|
||||||
return {
|
|
||||||
context: obj.context,
|
|
||||||
request: obj.request.substr(0, l - ext.length)
|
|
||||||
};
|
|
||||||
}).concat(obj);
|
|
||||||
}).reduce(function(a, b) {
|
|
||||||
return a.concat(b);
|
|
||||||
}, []));
|
|
||||||
});
|
|
||||||
|
|
||||||
params.contextModuleFactory.plugin("alternatives", function(items, callback) {
|
|
||||||
if(items.length === 0) return callback(null, items);
|
|
||||||
|
|
||||||
callback(null, items.map(function(obj) {
|
|
||||||
for(var i = 0; i < modulesDirectories.length; i++) {
|
|
||||||
var dir = modulesDirectories[i];
|
|
||||||
var idx = obj.request.indexOf("./" + dir + "/");
|
|
||||||
if(idx === 0) {
|
|
||||||
obj.request = obj.request.slice(dir.length + 3);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
Loading…
Reference in New Issue