UNPKG

8.79 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Jason Anderson @diurnalist
4*/
5
6"use strict";
7
8const mime = require("mime-types");
9const { basename, extname } = require("path");
10const util = require("util");
11const Chunk = require("./Chunk");
12const Module = require("./Module");
13const { parseResource } = require("./util/identifier");
14
15/** @typedef {import("./Compilation").AssetInfo} AssetInfo */
16/** @typedef {import("./Compilation").PathData} PathData */
17/** @typedef {import("./Compiler")} Compiler */
18
19const REGEXP = /\[\\*([\w:]+)\\*\]/gi;
20
21const 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
33const 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
61const 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
82const deprecationCache = new Map();
83const deprecatedFunction = (() => () => {})();
84const 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 * @param {string | function(PathData, AssetInfo=): string} path the raw path
98 * @param {PathData} data context data
99 * @param {AssetInfo} assetInfo extra info about the asset (will be written to)
100 * @returns {string} the interpolated path
101 */
102const replacePathVariables = (path, data, assetInfo) => {
103 const chunkGraph = data.chunkGraph;
104
105 /** @type {Map<string, Function>} */
106 const replacements = new Map();
107
108 // Filename context
109 //
110 // Placeholders
111 //
112 // for /some/path/file.js?query#fragment:
113 // [file] - /some/path/file.js
114 // [query] - ?query
115 // [fragment] - #fragment
116 // [base] - file.js
117 // [path] - /some/path/
118 // [name] - file
119 // [ext] - .js
120 if (typeof data.filename === "string") {
121 // check that filename is data uri
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 // Legacy
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 // Legacy
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 // Compilation context
171 //
172 // Placeholders
173 //
174 // [fullhash] - data.hash (3a4b5c6e7f)
175 //
176 // Legacy Placeholders
177 //
178 // [hash] - data.hash (3a4b5c6e7f)
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 // Legacy
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 // Chunk Context
201 //
202 // Placeholders
203 //
204 // [id] - chunk.id (0.js)
205 // [name] - chunk.name (app.js)
206 // [chunkhash] - chunk.hash (7823t4t4.js)
207 // [contenthash] - chunk.contentHash[type] (3256u3zg.js)
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 // Module Context
243 //
244 // Placeholders
245 //
246 // [id] - module.id (2.png)
247 // [hash] - module.hash (6237543873.png)
248 //
249 // Legacy Placeholders
250 //
251 // [moduleid] - module.id (2.png)
252 // [modulehash] - module.hash (6237543873.png)
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 // Legacy
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 // Other things
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
331const plugin = "TemplatedPathPlugin";
332
333class TemplatedPathPlugin {
334 /**
335 * Apply the plugin
336 * @param {Compiler} compiler the compiler instance
337 * @returns {void}
338 */
339 apply(compiler) {
340 compiler.hooks.compilation.tap(plugin, compilation => {
341 compilation.hooks.assetPath.tap(plugin, replacePathVariables);
342 });
343 }
344}
345
346module.exports = TemplatedPathPlugin;