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