1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { basename, extname } = require("path");
|
9 | const util = require("util");
|
10 | const Chunk = require("./Chunk");
|
11 | const Module = require("./Module");
|
12 | const { parseResource } = require("./util/identifier");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const REGEXP = /\[\\*([\w:]+)\\*\]/gi;
|
19 |
|
20 | const prepareId = id => {
|
21 | if (typeof id !== "string") return id;
|
22 |
|
23 | if (/^"\s\+*.*\+\s*"$/.test(id)) {
|
24 | const match = /^"\s\+*\s*(.*)\s*\+\s*"$/.exec(id);
|
25 |
|
26 | return `" + (${match[1]} + "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_") + "`;
|
27 | }
|
28 |
|
29 | return id.replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
|
30 | };
|
31 |
|
32 | const hashLength = (replacer, handler, assetInfo, hashName) => {
|
33 | const fn = (match, arg, input) => {
|
34 | let result;
|
35 | const length = arg && parseInt(arg, 10);
|
36 |
|
37 | if (length && handler) {
|
38 | result = handler(length);
|
39 | } else {
|
40 | const hash = replacer(match, arg, input);
|
41 |
|
42 | result = length ? hash.slice(0, length) : hash;
|
43 | }
|
44 | if (assetInfo) {
|
45 | assetInfo.immutable = true;
|
46 | if (Array.isArray(assetInfo[hashName])) {
|
47 | assetInfo[hashName] = [...assetInfo[hashName], result];
|
48 | } else if (assetInfo[hashName]) {
|
49 | assetInfo[hashName] = [assetInfo[hashName], result];
|
50 | } else {
|
51 | assetInfo[hashName] = result;
|
52 | }
|
53 | }
|
54 | return result;
|
55 | };
|
56 |
|
57 | return fn;
|
58 | };
|
59 |
|
60 | const replacer = (value, allowEmpty) => {
|
61 | const fn = (match, arg, input) => {
|
62 | if (typeof value === "function") {
|
63 | value = value();
|
64 | }
|
65 | if (value === null || value === undefined) {
|
66 | if (!allowEmpty) {
|
67 | throw new Error(
|
68 | `Path variable ${match} not implemented in this context: ${input}`
|
69 | );
|
70 | }
|
71 |
|
72 | return "";
|
73 | } else {
|
74 | return `${value}`;
|
75 | }
|
76 | };
|
77 |
|
78 | return fn;
|
79 | };
|
80 |
|
81 | const deprecationCache = new Map();
|
82 | const deprecatedFunction = (() => () => {})();
|
83 | const deprecated = (fn, message, code) => {
|
84 | let d = deprecationCache.get(message);
|
85 | if (d === undefined) {
|
86 | d = util.deprecate(deprecatedFunction, message, code);
|
87 | deprecationCache.set(message, d);
|
88 | }
|
89 | return (...args) => {
|
90 | d();
|
91 | return fn(...args);
|
92 | };
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | const replacePathVariables = (path, data, assetInfo) => {
|
102 | const chunkGraph = data.chunkGraph;
|
103 |
|
104 |
|
105 | const replacements = new Map();
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | if (data.filename) {
|
120 | if (typeof data.filename === "string") {
|
121 | const { path: file, query, fragment } = parseResource(data.filename);
|
122 |
|
123 | const ext = extname(file);
|
124 | const base = basename(file);
|
125 | const name = base.slice(0, base.length - ext.length);
|
126 | const path = file.slice(0, file.length - base.length);
|
127 |
|
128 | replacements.set("file", replacer(file));
|
129 | replacements.set("query", replacer(query, true));
|
130 | replacements.set("fragment", replacer(fragment, true));
|
131 | replacements.set("path", replacer(path, true));
|
132 | replacements.set("base", replacer(base));
|
133 | replacements.set("name", replacer(name));
|
134 | replacements.set("ext", replacer(ext, true));
|
135 |
|
136 | replacements.set(
|
137 | "filebase",
|
138 | deprecated(
|
139 | replacer(base),
|
140 | "[filebase] is now [base]",
|
141 | "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
|
142 | )
|
143 | );
|
144 | }
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | if (data.hash) {
|
157 | const hashReplacer = hashLength(
|
158 | replacer(data.hash),
|
159 | data.hashWithLength,
|
160 | assetInfo,
|
161 | "fullhash"
|
162 | );
|
163 |
|
164 | replacements.set("fullhash", hashReplacer);
|
165 |
|
166 |
|
167 | replacements.set(
|
168 | "hash",
|
169 | deprecated(
|
170 | hashReplacer,
|
171 | "[hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)",
|
172 | "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH"
|
173 | )
|
174 | );
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | if (data.chunk) {
|
186 | const chunk = data.chunk;
|
187 |
|
188 | const contentHashType = data.contentHashType;
|
189 |
|
190 | const idReplacer = replacer(chunk.id);
|
191 | const nameReplacer = replacer(chunk.name || chunk.id);
|
192 | const chunkhashReplacer = hashLength(
|
193 | replacer(chunk instanceof Chunk ? chunk.renderedHash : chunk.hash),
|
194 | "hashWithLength" in chunk ? chunk.hashWithLength : undefined,
|
195 | assetInfo,
|
196 | "chunkhash"
|
197 | );
|
198 | const contenthashReplacer = hashLength(
|
199 | replacer(
|
200 | data.contentHash ||
|
201 | (contentHashType &&
|
202 | chunk.contentHash &&
|
203 | chunk.contentHash[contentHashType])
|
204 | ),
|
205 | data.contentHashWithLength ||
|
206 | ("contentHashWithLength" in chunk && chunk.contentHashWithLength
|
207 | ? chunk.contentHashWithLength[contentHashType]
|
208 | : undefined),
|
209 | assetInfo,
|
210 | "contenthash"
|
211 | );
|
212 |
|
213 | replacements.set("id", idReplacer);
|
214 | replacements.set("name", nameReplacer);
|
215 | replacements.set("chunkhash", chunkhashReplacer);
|
216 | replacements.set("contenthash", contenthashReplacer);
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | if (data.module) {
|
231 | const module = data.module;
|
232 |
|
233 | const idReplacer = replacer(() =>
|
234 | prepareId(
|
235 | module instanceof Module ? chunkGraph.getModuleId(module) : module.id
|
236 | )
|
237 | );
|
238 | const moduleHashReplacer = hashLength(
|
239 | replacer(() =>
|
240 | module instanceof Module
|
241 | ? chunkGraph.getRenderedModuleHash(module, data.runtime)
|
242 | : module.hash
|
243 | ),
|
244 | "hashWithLength" in module ? module.hashWithLength : undefined,
|
245 | assetInfo,
|
246 | "modulehash"
|
247 | );
|
248 | const contentHashReplacer = hashLength(
|
249 | replacer(data.contentHash),
|
250 | undefined,
|
251 | assetInfo,
|
252 | "contenthash"
|
253 | );
|
254 |
|
255 | replacements.set("id", idReplacer);
|
256 | replacements.set("modulehash", moduleHashReplacer);
|
257 | replacements.set("contenthash", contentHashReplacer);
|
258 | replacements.set(
|
259 | "hash",
|
260 | data.contentHash ? contentHashReplacer : moduleHashReplacer
|
261 | );
|
262 |
|
263 | replacements.set(
|
264 | "moduleid",
|
265 | deprecated(
|
266 | idReplacer,
|
267 | "[moduleid] is now [id]",
|
268 | "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_MODULE_ID"
|
269 | )
|
270 | );
|
271 | }
|
272 |
|
273 |
|
274 | if (data.url) {
|
275 | replacements.set("url", replacer(data.url));
|
276 | }
|
277 | if (typeof data.runtime === "string") {
|
278 | replacements.set(
|
279 | "runtime",
|
280 | replacer(() => prepareId(data.runtime))
|
281 | );
|
282 | } else {
|
283 | replacements.set("runtime", replacer("_"));
|
284 | }
|
285 |
|
286 | if (typeof path === "function") {
|
287 | path = path(data, assetInfo);
|
288 | }
|
289 |
|
290 | path = path.replace(REGEXP, (match, content) => {
|
291 | if (content.length + 2 === match.length) {
|
292 | const contentMatch = /^(\w+)(?::(\w+))?$/.exec(content);
|
293 | if (!contentMatch) return match;
|
294 | const [, kind, arg] = contentMatch;
|
295 | const replacer = replacements.get(kind);
|
296 | if (replacer !== undefined) {
|
297 | return replacer(match, arg, path);
|
298 | }
|
299 | } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
|
300 | return `[${match.slice(2, -2)}]`;
|
301 | }
|
302 | return match;
|
303 | });
|
304 |
|
305 | return path;
|
306 | };
|
307 |
|
308 | const plugin = "TemplatedPathPlugin";
|
309 |
|
310 | class TemplatedPathPlugin {
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 | apply(compiler) {
|
317 | compiler.hooks.compilation.tap(plugin, compilation => {
|
318 | compilation.hooks.assetPath.tap(plugin, replacePathVariables);
|
319 | });
|
320 | }
|
321 | }
|
322 |
|
323 | module.exports = TemplatedPathPlugin;
|