|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var path = require("path"); |
6 | | -var ContextElementDependency = require("./dependencies/ContextElementDependency"); |
| 5 | +"use strict"; |
7 | 6 |
|
8 | | -function ContextReplacementPlugin(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) { |
9 | | - this.resourceRegExp = resourceRegExp; |
10 | | - if(typeof newContentResource === "function") { |
11 | | - this.newContentCallback = newContentResource; |
12 | | - } else if(typeof newContentResource === "string" && typeof newContentRecursive === "object") { |
13 | | - this.newContentResource = newContentResource; |
14 | | - this.newContentCreateContextMap = function(fs, callback) { |
15 | | - callback(null, newContentRecursive); |
16 | | - }; |
17 | | - } else if(typeof newContentResource === "string" && typeof newContentRecursive === "function") { |
18 | | - this.newContentResource = newContentResource; |
19 | | - this.newContentCreateContextMap = newContentRecursive; |
20 | | - } else { |
21 | | - if(typeof newContentResource !== "string") { |
22 | | - newContentRegExp = newContentRecursive; |
23 | | - newContentRecursive = newContentResource; |
24 | | - newContentResource = undefined; |
25 | | - } |
26 | | - if(typeof newContentRecursive !== "boolean") { |
27 | | - newContentRegExp = newContentRecursive; |
28 | | - newContentRecursive = undefined; |
| 7 | +const path = require("path"); |
| 8 | +const ContextElementDependency = require("./dependencies/ContextElementDependency"); |
| 9 | + |
| 10 | +class ContextReplacementPlugin { |
| 11 | + constructor(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) { |
| 12 | + this.resourceRegExp = resourceRegExp; |
| 13 | + |
| 14 | + if(typeof newContentResource === "function") { |
| 15 | + this.newContentCallback = newContentResource; |
| 16 | + } else if(typeof newContentResource === "string" && typeof newContentRecursive === "object") { |
| 17 | + this.newContentResource = newContentResource; |
| 18 | + this.newContentCreateContextMap = (fs, callback) => { |
| 19 | + callback(null, newContentRecursive); |
| 20 | + }; |
| 21 | + } else if(typeof newContentResource === "string" && typeof newContentRecursive === "function") { |
| 22 | + this.newContentResource = newContentResource; |
| 23 | + this.newContentCreateContextMap = newContentRecursive; |
| 24 | + } else { |
| 25 | + if(typeof newContentResource !== "string") { |
| 26 | + newContentRegExp = newContentRecursive; |
| 27 | + newContentRecursive = newContentResource; |
| 28 | + newContentResource = undefined; |
| 29 | + } |
| 30 | + if(typeof newContentRecursive !== "boolean") { |
| 31 | + newContentRegExp = newContentRecursive; |
| 32 | + newContentRecursive = undefined; |
| 33 | + } |
| 34 | + this.newContentResource = newContentResource; |
| 35 | + this.newContentRecursive = newContentRecursive; |
| 36 | + this.newContentRegExp = newContentRegExp; |
29 | 37 | } |
30 | | - this.newContentResource = newContentResource; |
31 | | - this.newContentRecursive = newContentRecursive; |
32 | | - this.newContentRegExp = newContentRegExp; |
33 | 38 | } |
34 | | -} |
35 | | -module.exports = ContextReplacementPlugin; |
36 | | -ContextReplacementPlugin.prototype.apply = function(compiler) { |
37 | | - var resourceRegExp = this.resourceRegExp; |
38 | | - var newContentCallback = this.newContentCallback; |
39 | | - var newContentResource = this.newContentResource; |
40 | | - var newContentRecursive = this.newContentRecursive; |
41 | | - var newContentRegExp = this.newContentRegExp; |
42 | | - var newContentCreateContextMap = this.newContentCreateContextMap; |
43 | | - compiler.plugin("context-module-factory", function(cmf) { |
44 | | - cmf.plugin("before-resolve", function(result, callback) { |
45 | | - if(!result) return callback(); |
46 | | - if(resourceRegExp.test(result.request)) { |
47 | | - if(typeof newContentResource !== "undefined") |
48 | | - result.request = newContentResource; |
49 | | - if(typeof newContentRecursive !== "undefined") |
50 | | - result.recursive = newContentRecursive; |
51 | | - if(typeof newContentRegExp !== "undefined") |
52 | | - result.regExp = newContentRegExp; |
53 | | - if(typeof newContentCallback === "function") { |
54 | | - newContentCallback(result); |
55 | | - } else { |
56 | | - result.dependencies.forEach(function(d) { |
57 | | - if(d.critical) |
58 | | - d.critical = false; |
59 | | - }); |
| 39 | + |
| 40 | + apply(compiler) { |
| 41 | + const resourceRegExp = this.resourceRegExp; |
| 42 | + const newContentCallback = this.newContentCallback; |
| 43 | + const newContentResource = this.newContentResource; |
| 44 | + const newContentRecursive = this.newContentRecursive; |
| 45 | + const newContentRegExp = this.newContentRegExp; |
| 46 | + const newContentCreateContextMap = this.newContentCreateContextMap; |
| 47 | + |
| 48 | + compiler.plugin("context-module-factory", (cmf) => { |
| 49 | + cmf.plugin("before-resolve", (result, callback) => { |
| 50 | + if(!result) return callback(); |
| 51 | + if(resourceRegExp.test(result.request)) { |
| 52 | + if(typeof newContentResource !== "undefined") |
| 53 | + result.request = newContentResource; |
| 54 | + if(typeof newContentRecursive !== "undefined") |
| 55 | + result.recursive = newContentRecursive; |
| 56 | + if(typeof newContentRegExp !== "undefined") |
| 57 | + result.regExp = newContentRegExp; |
| 58 | + if(typeof newContentCallback === "function") { |
| 59 | + newContentCallback(result); |
| 60 | + } else { |
| 61 | + result.dependencies.forEach((d) => { |
| 62 | + if(d.critical) |
| 63 | + d.critical = false; |
| 64 | + }); |
| 65 | + } |
60 | 66 | } |
61 | | - } |
62 | | - return callback(null, result); |
63 | | - }); |
64 | | - cmf.plugin("after-resolve", function(result, callback) { |
65 | | - if(!result) return callback(); |
66 | | - if(resourceRegExp.test(result.resource)) { |
67 | | - if(typeof newContentResource !== "undefined") |
68 | | - result.resource = path.resolve(result.resource, newContentResource); |
69 | | - if(typeof newContentRecursive !== "undefined") |
70 | | - result.recursive = newContentRecursive; |
71 | | - if(typeof newContentRegExp !== "undefined") |
72 | | - result.regExp = newContentRegExp; |
73 | | - if(typeof newContentCreateContextMap === "function") |
74 | | - result.resolveDependencies = createResolveDependenciesFromContextMap(newContentCreateContextMap); |
75 | | - if(typeof newContentCallback === "function") { |
76 | | - var origResource = result.resource; |
77 | | - newContentCallback(result); |
78 | | - if(result.resource !== origResource) { |
79 | | - result.resource = path.resolve(origResource, result.resource); |
| 67 | + return callback(null, result); |
| 68 | + }); |
| 69 | + cmf.plugin("after-resolve", (result, callback) => { |
| 70 | + if(!result) return callback(); |
| 71 | + if(resourceRegExp.test(result.resource)) { |
| 72 | + if(typeof newContentResource !== "undefined") |
| 73 | + result.resource = path.resolve(result.resource, newContentResource); |
| 74 | + if(typeof newContentRecursive !== "undefined") |
| 75 | + result.recursive = newContentRecursive; |
| 76 | + if(typeof newContentRegExp !== "undefined") |
| 77 | + result.regExp = newContentRegExp; |
| 78 | + if(typeof newContentCreateContextMap === "function") |
| 79 | + result.resolveDependencies = createResolveDependenciesFromContextMap(newContentCreateContextMap); |
| 80 | + if(typeof newContentCallback === "function") { |
| 81 | + const origResource = result.resource; |
| 82 | + newContentCallback(result); |
| 83 | + if(result.resource !== origResource) { |
| 84 | + result.resource = path.resolve(origResource, result.resource); |
| 85 | + } |
| 86 | + } else { |
| 87 | + result.dependencies.forEach((d) => { |
| 88 | + if(d.critical) |
| 89 | + d.critical = false; |
| 90 | + }); |
80 | 91 | } |
81 | | - } else { |
82 | | - result.dependencies.forEach(function(d) { |
83 | | - if(d.critical) |
84 | | - d.critical = false; |
85 | | - }); |
86 | 92 | } |
87 | | - } |
88 | | - return callback(null, result); |
| 93 | + return callback(null, result); |
| 94 | + }); |
89 | 95 | }); |
90 | | - }); |
91 | | -}; |
| 96 | + } |
| 97 | +} |
92 | 98 |
|
93 | | -function createResolveDependenciesFromContextMap(createContextMap) { |
| 99 | +const createResolveDependenciesFromContextMap = (createContextMap) => { |
94 | 100 | return function resolveDependenciesFromContextMap(fs, resource, recursive, regExp, callback) { |
95 | | - createContextMap(fs, function(err, map) { |
| 101 | + createContextMap(fs, (err, map) => { |
96 | 102 | if(err) return callback(err); |
97 | | - var dependencies = Object.keys(map).map(function(key) { |
| 103 | + const dependencies = Object.keys(map).map((key) => { |
98 | 104 | return new ContextElementDependency(map[key], key); |
99 | 105 | }); |
100 | 106 | callback(null, dependencies); |
101 | 107 | }); |
102 | 108 | }; |
103 | | -} |
| 109 | +}; |
| 110 | + |
| 111 | +module.exports = ContextReplacementPlugin; |
0 commit comments