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