ModuleFilenameHelpers.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NormalModule = require("./NormalModule");
  7. const { DEFAULTS } = require("./config/defaults");
  8. const createHash = require("./util/createHash");
  9. const memoize = require("./util/memoize");
  10. /** @typedef {import("../declarations/WebpackOptions").HashFunction} HashFunction */
  11. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  12. /** @typedef {import("./Module")} Module */
  13. /** @typedef {import("./RequestShortener")} RequestShortener */
  14. /** @typedef {(str: string) => boolean} MatcherFn */
  15. /** @typedef {string | RegExp | MatcherFn | (string | RegExp | MatcherFn)[]} Matcher */
  16. /** @typedef {{ test?: Matcher, include?: Matcher, exclude?: Matcher }} MatchObject */
  17. const ModuleFilenameHelpers = module.exports;
  18. // TODO webpack 6: consider removing these
  19. ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
  20. ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE =
  21. /\[all-?loaders\]\[resource\]/gi;
  22. ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
  23. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
  24. ModuleFilenameHelpers.RESOURCE = "[resource]";
  25. ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
  26. ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
  27. // cSpell:words olute
  28. ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
  29. /\[abs(olute)?-?resource-?path\]/gi;
  30. ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
  31. ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
  32. ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
  33. ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
  34. ModuleFilenameHelpers.LOADERS = "[loaders]";
  35. ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
  36. ModuleFilenameHelpers.QUERY = "[query]";
  37. ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
  38. ModuleFilenameHelpers.ID = "[id]";
  39. ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
  40. ModuleFilenameHelpers.HASH = "[hash]";
  41. ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
  42. ModuleFilenameHelpers.NAMESPACE = "[namespace]";
  43. ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
  44. /** @typedef {() => string} ReturnStringCallback */
  45. /**
  46. * Returns a function that returns the part of the string after the token
  47. * @param {ReturnStringCallback} strFn the function to get the string
  48. * @param {string} token the token to search for
  49. * @returns {ReturnStringCallback} a function that returns the part of the string after the token
  50. */
  51. const getAfter = (strFn, token) => () => {
  52. const str = strFn();
  53. const idx = str.indexOf(token);
  54. return idx < 0 ? "" : str.slice(idx);
  55. };
  56. /**
  57. * Returns a function that returns the part of the string before the token
  58. * @param {ReturnStringCallback} strFn the function to get the string
  59. * @param {string} token the token to search for
  60. * @returns {ReturnStringCallback} a function that returns the part of the string before the token
  61. */
  62. const getBefore = (strFn, token) => () => {
  63. const str = strFn();
  64. const idx = str.lastIndexOf(token);
  65. return idx < 0 ? "" : str.slice(0, idx);
  66. };
  67. /**
  68. * Returns a function that returns a hash of the string
  69. * @param {ReturnStringCallback} strFn the function to get the string
  70. * @param {HashFunction=} hashFunction the hash function to use
  71. * @returns {ReturnStringCallback} a function that returns the hash of the string
  72. */
  73. const getHash =
  74. (strFn, hashFunction = DEFAULTS.HASH_FUNCTION) =>
  75. () => {
  76. const hash = createHash(hashFunction);
  77. hash.update(strFn());
  78. const digest = hash.digest("hex");
  79. return digest.slice(0, 4);
  80. };
  81. /**
  82. * @template T
  83. * Returns a lazy object. The object is lazy in the sense that the properties are
  84. * only evaluated when they are accessed. This is only obtained by setting a function as the value for each key.
  85. * @param {Record<string, () => T>} obj the object to convert to a lazy access object
  86. * @returns {Record<string, T>} the lazy access object
  87. */
  88. const lazyObject = (obj) => {
  89. const newObj = /** @type {Record<string, T>} */ ({});
  90. for (const key of Object.keys(obj)) {
  91. const fn = obj[key];
  92. Object.defineProperty(newObj, key, {
  93. get: () => fn(),
  94. set: (v) => {
  95. Object.defineProperty(newObj, key, {
  96. value: v,
  97. enumerable: true,
  98. writable: true
  99. });
  100. },
  101. enumerable: true,
  102. configurable: true
  103. });
  104. }
  105. return newObj;
  106. };
  107. const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/g;
  108. /**
  109. * @typedef {object} ModuleFilenameTemplateContext
  110. * @property {string} identifier the identifier of the module
  111. * @property {string} shortIdentifier the shortened identifier of the module
  112. * @property {string} resource the resource of the module request
  113. * @property {string} resourcePath the resource path of the module request
  114. * @property {string} absoluteResourcePath the absolute resource path of the module request
  115. * @property {string} loaders the loaders of the module request
  116. * @property {string} allLoaders the all loaders of the module request
  117. * @property {string} query the query of the module identifier
  118. * @property {string} moduleId the module id of the module
  119. * @property {string} hash the hash of the module identifier
  120. * @property {string} namespace the module namespace
  121. */
  122. /** @typedef {((context: ModuleFilenameTemplateContext) => string)} ModuleFilenameTemplateFunction */
  123. /** @typedef {string | ModuleFilenameTemplateFunction} ModuleFilenameTemplate */
  124. /**
  125. * @param {Module | string} module the module
  126. * @param {{ namespace?: string, moduleFilenameTemplate?: ModuleFilenameTemplate }} options options
  127. * @param {{ requestShortener: RequestShortener, chunkGraph: ChunkGraph, hashFunction?: HashFunction }} contextInfo context info
  128. * @returns {string} the filename
  129. */
  130. ModuleFilenameHelpers.createFilename = (
  131. // eslint-disable-next-line default-param-last
  132. module = "",
  133. options,
  134. { requestShortener, chunkGraph, hashFunction = DEFAULTS.HASH_FUNCTION }
  135. ) => {
  136. const opts = {
  137. namespace: "",
  138. moduleFilenameTemplate: "",
  139. ...(typeof options === "object"
  140. ? options
  141. : {
  142. moduleFilenameTemplate: options
  143. })
  144. };
  145. /** @type {ReturnStringCallback} */
  146. let absoluteResourcePath;
  147. /** @type {ReturnStringCallback} */
  148. let hash;
  149. /** @type {ReturnStringCallback} */
  150. let identifier;
  151. /** @type {ReturnStringCallback} */
  152. let moduleId;
  153. /** @type {ReturnStringCallback} */
  154. let shortIdentifier;
  155. if (typeof module === "string") {
  156. shortIdentifier =
  157. /** @type {ReturnStringCallback} */
  158. (memoize(() => requestShortener.shorten(module)));
  159. identifier = shortIdentifier;
  160. moduleId = () => "";
  161. absoluteResourcePath = () =>
  162. /** @type {string} */ (module.split("!").pop());
  163. hash = getHash(identifier, hashFunction);
  164. } else {
  165. shortIdentifier = memoize(() =>
  166. module.readableIdentifier(requestShortener)
  167. );
  168. identifier =
  169. /** @type {ReturnStringCallback} */
  170. (memoize(() => requestShortener.shorten(module.identifier())));
  171. moduleId =
  172. /** @type {ReturnStringCallback} */
  173. (() => chunkGraph.getModuleId(module));
  174. absoluteResourcePath = () =>
  175. module instanceof NormalModule
  176. ? module.resource
  177. : /** @type {string} */ (module.identifier().split("!").pop());
  178. hash = getHash(identifier, hashFunction);
  179. }
  180. const resource =
  181. /** @type {ReturnStringCallback} */
  182. (memoize(() => shortIdentifier().split("!").pop()));
  183. const loaders = getBefore(shortIdentifier, "!");
  184. const allLoaders = getBefore(identifier, "!");
  185. const query = getAfter(resource, "?");
  186. const resourcePath = () => {
  187. const q = query().length;
  188. return q === 0 ? resource() : resource().slice(0, -q);
  189. };
  190. if (typeof opts.moduleFilenameTemplate === "function") {
  191. return opts.moduleFilenameTemplate(
  192. /** @type {ModuleFilenameTemplateContext} */
  193. (
  194. lazyObject({
  195. identifier,
  196. shortIdentifier,
  197. resource,
  198. resourcePath: memoize(resourcePath),
  199. absoluteResourcePath: memoize(absoluteResourcePath),
  200. loaders: memoize(loaders),
  201. allLoaders: memoize(allLoaders),
  202. query: memoize(query),
  203. moduleId: memoize(moduleId),
  204. hash: memoize(hash),
  205. namespace: () => opts.namespace
  206. })
  207. )
  208. );
  209. }
  210. // TODO webpack 6: consider removing alternatives without dashes
  211. /** @type {Map<string, () => string>} */
  212. const replacements = new Map([
  213. ["identifier", identifier],
  214. ["short-identifier", shortIdentifier],
  215. ["resource", resource],
  216. ["resource-path", resourcePath],
  217. // cSpell:words resourcepath
  218. ["resourcepath", resourcePath],
  219. ["absolute-resource-path", absoluteResourcePath],
  220. ["abs-resource-path", absoluteResourcePath],
  221. // cSpell:words absoluteresource
  222. ["absoluteresource-path", absoluteResourcePath],
  223. // cSpell:words absresource
  224. ["absresource-path", absoluteResourcePath],
  225. // cSpell:words resourcepath
  226. ["absolute-resourcepath", absoluteResourcePath],
  227. // cSpell:words resourcepath
  228. ["abs-resourcepath", absoluteResourcePath],
  229. // cSpell:words absoluteresourcepath
  230. ["absoluteresourcepath", absoluteResourcePath],
  231. // cSpell:words absresourcepath
  232. ["absresourcepath", absoluteResourcePath],
  233. ["all-loaders", allLoaders],
  234. // cSpell:words allloaders
  235. ["allloaders", allLoaders],
  236. ["loaders", loaders],
  237. ["query", query],
  238. ["id", moduleId],
  239. ["hash", hash],
  240. ["namespace", () => opts.namespace]
  241. ]);
  242. // TODO webpack 6: consider removing weird double placeholders
  243. return /** @type {string} */ (opts.moduleFilenameTemplate)
  244. .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
  245. .replace(
  246. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
  247. "[short-identifier]"
  248. )
  249. .replace(SQUARE_BRACKET_TAG_REGEXP, (match, content) => {
  250. if (content.length + 2 === match.length) {
  251. const replacement = replacements.get(content.toLowerCase());
  252. if (replacement !== undefined) {
  253. return replacement();
  254. }
  255. } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
  256. return `[${match.slice(2, -2)}]`;
  257. }
  258. return match;
  259. });
  260. };
  261. /**
  262. * Replaces duplicate items in an array with new values generated by a callback function.
  263. * The callback function is called with the duplicate item, the index of the duplicate item, and the number of times the item has been replaced.
  264. * The callback function should return the new value for the duplicate item.
  265. * @template T
  266. * @param {T[]} array the array with duplicates to be replaced
  267. * @param {(duplicateItem: T, duplicateItemIndex: number, numberOfTimesReplaced: number) => T} fn callback function to generate new values for the duplicate items
  268. * @param {(firstElement: T, nextElement: T) => -1 | 0 | 1=} comparator optional comparator function to sort the duplicate items
  269. * @returns {T[]} the array with duplicates replaced
  270. * @example
  271. * ```js
  272. * const array = ["a", "b", "c", "a", "b", "a"];
  273. * const result = ModuleFilenameHelpers.replaceDuplicates(array, (item, index, count) => `${item}-${count}`);
  274. * // result: ["a-1", "b-1", "c", "a-2", "b-2", "a-3"]
  275. * ```
  276. */
  277. ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
  278. const countMap = Object.create(null);
  279. const posMap = Object.create(null);
  280. for (const [idx, item] of array.entries()) {
  281. countMap[item] = countMap[item] || [];
  282. countMap[item].push(idx);
  283. posMap[item] = 0;
  284. }
  285. if (comparator) {
  286. for (const item of Object.keys(countMap)) {
  287. countMap[item].sort(comparator);
  288. }
  289. }
  290. return array.map((item, i) => {
  291. if (countMap[item].length > 1) {
  292. if (comparator && countMap[item][0] === i) return item;
  293. return fn(item, i, posMap[item]++);
  294. }
  295. return item;
  296. });
  297. };
  298. /**
  299. * Tests if a string matches a RegExp or an array of RegExp.
  300. * @param {string} str string to test
  301. * @param {Matcher} test value which will be used to match against the string
  302. * @returns {boolean} true, when the RegExp matches
  303. * @example
  304. * ```js
  305. * ModuleFilenameHelpers.matchPart("foo.js", "foo"); // true
  306. * ModuleFilenameHelpers.matchPart("foo.js", "foo.js"); // true
  307. * ModuleFilenameHelpers.matchPart("foo.js", "foo."); // false
  308. * ModuleFilenameHelpers.matchPart("foo.js", "foo*"); // false
  309. * ModuleFilenameHelpers.matchPart("foo.js", "foo.*"); // true
  310. * ModuleFilenameHelpers.matchPart("foo.js", /^foo/); // true
  311. * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
  312. * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
  313. * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, /^bar/]); // true
  314. * ModuleFilenameHelpers.matchPart("foo.js", [/^baz/, /^bar/]); // false
  315. * ```
  316. */
  317. const matchPart = (str, test) => {
  318. if (!test) return true;
  319. if (test instanceof RegExp) {
  320. return test.test(str);
  321. } else if (typeof test === "string") {
  322. return str.startsWith(test);
  323. } else if (typeof test === "function") {
  324. return test(str);
  325. }
  326. return test.some((test) => matchPart(str, test));
  327. };
  328. ModuleFilenameHelpers.matchPart = matchPart;
  329. /**
  330. * Tests if a string matches a match object. The match object can have the following properties:
  331. * - `test`: a RegExp or an array of RegExp
  332. * - `include`: a RegExp or an array of RegExp
  333. * - `exclude`: a RegExp or an array of RegExp
  334. *
  335. * The `test` property is tested first, then `include` and then `exclude`.
  336. * @param {MatchObject} obj a match object to test against the string
  337. * @param {string} str string to test against the matching object
  338. * @returns {boolean} true, when the object matches
  339. * @example
  340. * ```js
  341. * ModuleFilenameHelpers.matchObject({ test: "foo.js" }, "foo.js"); // true
  342. * ModuleFilenameHelpers.matchObject({ test: /^foo/ }, "foo.js"); // true
  343. * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "foo.js"); // true
  344. * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "baz.js"); // false
  345. * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "foo.js"); // true
  346. * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "bar.js"); // false
  347. * ModuleFilenameHelpers.matchObject({ include: /^foo/ }, "foo.js"); // true
  348. * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "foo.js"); // true
  349. * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "baz.js"); // false
  350. * ModuleFilenameHelpers.matchObject({ exclude: "foo.js" }, "foo.js"); // false
  351. * ModuleFilenameHelpers.matchObject({ exclude: [/^foo/, "bar"] }, "foo.js"); // false
  352. * ```
  353. */
  354. ModuleFilenameHelpers.matchObject = (obj, str) => {
  355. if (obj.test && !ModuleFilenameHelpers.matchPart(str, obj.test)) {
  356. return false;
  357. }
  358. if (obj.include && !ModuleFilenameHelpers.matchPart(str, obj.include)) {
  359. return false;
  360. }
  361. if (obj.exclude && ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
  362. return false;
  363. }
  364. return true;
  365. };