|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var RequireContextDependency = require("./RequireContextDependency"); |
6 | | -var ContextElementDependency = require("./ContextElementDependency"); |
| 5 | +"use strict"; |
7 | 6 |
|
8 | | -var RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin"); |
| 7 | +const RequireContextDependency = require("./RequireContextDependency"); |
| 8 | +const ContextElementDependency = require("./ContextElementDependency"); |
9 | 9 |
|
10 | | -function RequireContextPlugin(modulesDirectories, extensions) { |
11 | | - if(!Array.isArray(modulesDirectories)) |
12 | | - throw new Error("modulesDirectories must be an array"); |
13 | | - if(!Array.isArray(extensions)) |
14 | | - throw new Error("extensions must be an array"); |
15 | | - this.modulesDirectories = modulesDirectories; |
16 | | - this.extensions = extensions; |
17 | | -} |
18 | | -module.exports = RequireContextPlugin; |
| 10 | +const RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin"); |
19 | 11 |
|
20 | | -RequireContextPlugin.prototype.apply = function(compiler) { |
21 | | - var modulesDirectories = this.modulesDirectories; |
22 | | - var extensions = this.extensions; |
23 | | - compiler.plugin("compilation", function(compilation, params) { |
24 | | - var contextModuleFactory = params.contextModuleFactory; |
25 | | - var normalModuleFactory = params.normalModuleFactory; |
| 12 | +class RequireContextPlugin { |
| 13 | + constructor(modulesDirectories, extensions) { |
| 14 | + if(!Array.isArray(modulesDirectories)) |
| 15 | + throw new Error("modulesDirectories must be an array"); |
| 16 | + if(!Array.isArray(extensions)) |
| 17 | + throw new Error("extensions must be an array"); |
| 18 | + this.modulesDirectories = modulesDirectories; |
| 19 | + this.extensions = extensions; |
| 20 | + } |
26 | 21 |
|
27 | | - compilation.dependencyFactories.set(RequireContextDependency, contextModuleFactory); |
28 | | - compilation.dependencyTemplates.set(RequireContextDependency, new RequireContextDependency.Template()); |
| 22 | + apply(compiler) { |
| 23 | + const modulesDirectories = this.modulesDirectories; |
| 24 | + const extensions = this.extensions; |
| 25 | + compiler.plugin("compilation", (compilation, params) => { |
| 26 | + const contextModuleFactory = params.contextModuleFactory; |
| 27 | + const normalModuleFactory = params.normalModuleFactory; |
29 | 28 |
|
30 | | - compilation.dependencyFactories.set(ContextElementDependency, normalModuleFactory); |
| 29 | + compilation.dependencyFactories.set(RequireContextDependency, contextModuleFactory); |
| 30 | + compilation.dependencyTemplates.set(RequireContextDependency, new RequireContextDependency.Template()); |
31 | 31 |
|
32 | | - params.normalModuleFactory.plugin("parser", function(parser, parserOptions) { |
| 32 | + compilation.dependencyFactories.set(ContextElementDependency, normalModuleFactory); |
33 | 33 |
|
34 | | - if(typeof parserOptions.requireContext !== "undefined" && !parserOptions.requireContext) |
35 | | - return; |
| 34 | + params.normalModuleFactory.plugin("parser", (parser, parserOptions) => { |
36 | 35 |
|
37 | | - parser.apply(new RequireContextDependencyParserPlugin()); |
38 | | - }); |
| 36 | + if(typeof parserOptions.requireContext !== "undefined" && !parserOptions.requireContext) |
| 37 | + return; |
39 | 38 |
|
40 | | - params.contextModuleFactory.plugin("alternatives", function(items, callback) { |
41 | | - if(items.length === 0) return callback(null, items); |
| 39 | + parser.apply(new RequireContextDependencyParserPlugin()); |
| 40 | + }); |
42 | 41 |
|
43 | | - callback(null, items.map(function(obj) { |
44 | | - return extensions.filter(function(ext) { |
45 | | - var l = obj.request.length; |
46 | | - return l > ext.length && obj.request.substr(l - ext.length, l) === ext; |
47 | | - }).map(function(ext) { |
48 | | - var l = obj.request.length; |
49 | | - return { |
50 | | - context: obj.context, |
51 | | - request: obj.request.substr(0, l - ext.length) |
52 | | - }; |
53 | | - }).concat(obj); |
54 | | - }).reduce(function(a, b) { |
55 | | - return a.concat(b); |
56 | | - }, [])); |
57 | | - }); |
| 42 | + params.contextModuleFactory.plugin("alternatives", (items, callback) => { |
| 43 | + if(items.length === 0) return callback(null, items); |
58 | 44 |
|
59 | | - params.contextModuleFactory.plugin("alternatives", function(items, callback) { |
60 | | - if(items.length === 0) return callback(null, items); |
| 45 | + callback(null, items.map((obj) => { |
| 46 | + return extensions.filter((ext) => { |
| 47 | + const l = obj.request.length; |
| 48 | + return l > ext.length && obj.request.substr(l - ext.length, l) === ext; |
| 49 | + }).map((ext) => { |
| 50 | + const l = obj.request.length; |
| 51 | + return { |
| 52 | + context: obj.context, |
| 53 | + request: obj.request.substr(0, l - ext.length) |
| 54 | + }; |
| 55 | + }).concat(obj); |
| 56 | + }).reduce((a, b) => a.concat(b), [])); |
| 57 | + }); |
61 | 58 |
|
62 | | - callback(null, items.map(function(obj) { |
63 | | - for(var i = 0; i < modulesDirectories.length; i++) { |
64 | | - var dir = modulesDirectories[i]; |
65 | | - var idx = obj.request.indexOf("./" + dir + "/"); |
66 | | - if(idx === 0) { |
67 | | - obj.request = obj.request.slice(dir.length + 3); |
68 | | - break; |
| 59 | + params.contextModuleFactory.plugin("alternatives", (items, callback) => { |
| 60 | + if(items.length === 0) return callback(null, items); |
| 61 | + |
| 62 | + callback(null, items.map((obj) => { |
| 63 | + for(let i = 0; i < modulesDirectories.length; i++) { |
| 64 | + const dir = modulesDirectories[i]; |
| 65 | + const idx = obj.request.indexOf("./" + dir + "/"); |
| 66 | + if(idx === 0) { |
| 67 | + obj.request = obj.request.slice(dir.length + 3); |
| 68 | + break; |
| 69 | + } |
69 | 70 | } |
70 | | - } |
71 | | - return obj; |
72 | | - })); |
| 71 | + return obj; |
| 72 | + })); |
| 73 | + }); |
73 | 74 | }); |
74 | | - }); |
75 | | -}; |
| 75 | + } |
| 76 | +} |
| 77 | +module.exports = RequireContextPlugin; |
0 commit comments