UNPKG

8.04 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 { basename, extname } = require("path");
9const util = require("util");
10const Chunk = require("./Chunk");
11const Module = require("./Module");
12const { parseResource } = require("./util/identifier");
13
14/** @typedef {import("./Compilation").AssetInfo} AssetInfo */
15/** @typedef {import("./Compilation").PathData} PathData */
16/** @typedef {import("./Compiler")} Compiler */
17
18const REGEXP = /\[\\*([\w:]+)\\*\]/gi;
19
20const 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
32const 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
60const 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
81const deprecationCache = new Map();
82const deprecatedFunction = (() => () => {})();
83const 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 * @param {string | function(PathData, AssetInfo=): string} path the raw path
97 * @param {PathData} data context data
98 * @param {AssetInfo} assetInfo extra info about the asset (will be written to)
99 * @returns {string} the interpolated path
100 */
101const replacePathVariables = (path, data, assetInfo) => {
102 const chunkGraph = data.chunkGraph;
103
104 /** @type {Map<string, Function>} */
105 const replacements = new Map();
106
107 // Filename context
108 //
109 // Placeholders
110 //
111 // for /some/path/file.js?query#fragment:
112 // [file] - /some/path/file.js
113 // [query] - ?query
114 // [fragment] - #fragment
115 // [base] - file.js
116 // [path] - /some/path/
117 // [name] - file
118 // [ext] - .js
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 // Legacy
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 // Compilation context
148 //
149 // Placeholders
150 //
151 // [fullhash] - data.hash (3a4b5c6e7f)
152 //
153 // Legacy Placeholders
154 //
155 // [hash] - data.hash (3a4b5c6e7f)
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 // Legacy
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 // Chunk Context
178 //
179 // Placeholders
180 //
181 // [id] - chunk.id (0.js)
182 // [name] - chunk.name (app.js)
183 // [chunkhash] - chunk.hash (7823t4t4.js)
184 // [contenthash] - chunk.contentHash[type] (3256u3zg.js)
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 // Module Context
220 //
221 // Placeholders
222 //
223 // [id] - module.id (2.png)
224 // [hash] - module.hash (6237543873.png)
225 //
226 // Legacy Placeholders
227 //
228 // [moduleid] - module.id (2.png)
229 // [modulehash] - module.hash (6237543873.png)
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 // Legacy
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 // Other things
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
308const plugin = "TemplatedPathPlugin";
309
310class TemplatedPathPlugin {
311 /**
312 * Apply the plugin
313 * @param {Compiler} compiler the compiler instance
314 * @returns {void}
315 */
316 apply(compiler) {
317 compiler.hooks.compilation.tap(plugin, compilation => {
318 compilation.hooks.assetPath.tap(plugin, replacePathVariables);
319 });
320 }
321}
322
323module.exports = TemplatedPathPlugin;