ModuleFilenameHelpers.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. * Returns the lazy access object.
  83. * @template T
  84. * Returns a lazy object. The object is lazy in the sense that the properties are
  85. * only evaluated when they are accessed. This is only obtained by setting a function as the value for each key.
  86. * @param {Record<string, () => T>} obj the object to convert to a lazy access object
  87. * @returns {Record<string, T>} the lazy access object
  88. */
  89. const lazyObject = (obj) => {
  90. const newObj = /** @type {Record<string, T>} */ ({});
  91. for (const key of Object.keys(obj)) {
  92. const fn = obj[key];
  93. Object.defineProperty(newObj, key, {
  94. get: () => fn(),
  95. set: (v) => {
  96. Object.defineProperty(newObj, key, {
  97. value: v,
  98. enumerable: true,
  99. writable: true
  100. });
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. }
  106. return newObj;
  107. };
  108. const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/g;
  109. /**
  110. * Defines the module filename template context type used by this module.
  111. * @typedef {object} ModuleFilenameTemplateContext
  112. * @property {string} identifier the identifier of the module
  113. * @property {string} shortIdentifier the shortened identifier of the module
  114. * @property {string} resource the resource of the module request
  115. * @property {string} resourcePath the resource path of the module request
  116. * @property {string} absoluteResourcePath the absolute resource path of the module request
  117. * @property {string} loaders the loaders of the module request
  118. * @property {string} allLoaders the all loaders of the module request
  119. * @property {string} query the query of the module identifier
  120. * @property {string} moduleId the module id of the module
  121. * @property {string} hash the hash of the module identifier
  122. * @property {string} namespace the module namespace
  123. */
  124. /** @typedef {((context: ModuleFilenameTemplateContext) => string)} ModuleFilenameTemplateFunction */
  125. /** @typedef {string | ModuleFilenameTemplateFunction} ModuleFilenameTemplate */
  126. /**
  127. * Returns the filename.
  128. * @param {Module | string} module the module
  129. * @param {{ namespace?: string, moduleFilenameTemplate?: ModuleFilenameTemplate }} options options
  130. * @param {{ requestShortener: RequestShortener, chunkGraph: ChunkGraph, hashFunction?: HashFunction }} contextInfo context info
  131. * @returns {string} the filename
  132. */
  133. ModuleFilenameHelpers.createFilename = (
  134. // eslint-disable-next-line default-param-last
  135. module = "",
  136. options,
  137. { requestShortener, chunkGraph, hashFunction = DEFAULTS.HASH_FUNCTION }
  138. ) => {
  139. const opts = {
  140. namespace: "",
  141. moduleFilenameTemplate: "",
  142. ...(typeof options === "object"
  143. ? options
  144. : {
  145. moduleFilenameTemplate: options
  146. })
  147. };
  148. /** @type {ReturnStringCallback} */
  149. let absoluteResourcePath;
  150. /** @type {ReturnStringCallback} */
  151. let hash;
  152. /** @type {ReturnStringCallback} */
  153. let identifier;
  154. /** @type {ReturnStringCallback} */
  155. let moduleId;
  156. /** @type {ReturnStringCallback} */
  157. let shortIdentifier;
  158. if (typeof module === "string") {
  159. shortIdentifier =
  160. /** @type {ReturnStringCallback} */
  161. (memoize(() => requestShortener.shorten(module)));
  162. identifier = shortIdentifier;
  163. moduleId = () => "";
  164. absoluteResourcePath = () =>
  165. /** @type {string} */ (module.split("!").pop());
  166. hash = getHash(identifier, hashFunction);
  167. } else {
  168. shortIdentifier = memoize(() =>
  169. module.readableIdentifier(requestShortener)
  170. );
  171. identifier =
  172. /** @type {ReturnStringCallback} */
  173. (memoize(() => requestShortener.shorten(module.identifier())));
  174. moduleId =
  175. /** @type {ReturnStringCallback} */
  176. (() => chunkGraph.getModuleId(module));
  177. absoluteResourcePath = () =>
  178. module instanceof NormalModule
  179. ? module.resource
  180. : /** @type {string} */ (module.identifier().split("!").pop());
  181. hash = getHash(identifier, hashFunction);
  182. }
  183. const resource =
  184. /** @type {ReturnStringCallback} */
  185. (memoize(() => shortIdentifier().split("!").pop()));
  186. const loaders = getBefore(shortIdentifier, "!");
  187. const allLoaders = getBefore(identifier, "!");
  188. const query = getAfter(resource, "?");
  189. const resourcePath = () => {
  190. const q = query().length;
  191. return q === 0 ? resource() : resource().slice(0, -q);
  192. };
  193. if (typeof opts.moduleFilenameTemplate === "function") {
  194. return opts.moduleFilenameTemplate(
  195. /** @type {ModuleFilenameTemplateContext} */
  196. (
  197. lazyObject({
  198. identifier,
  199. shortIdentifier,
  200. resource,
  201. resourcePath: memoize(resourcePath),
  202. absoluteResourcePath: memoize(absoluteResourcePath),
  203. loaders: memoize(loaders),
  204. allLoaders: memoize(allLoaders),
  205. query: memoize(query),
  206. moduleId: memoize(moduleId),
  207. hash: memoize(hash),
  208. namespace: () => opts.namespace
  209. })
  210. )
  211. );
  212. }
  213. // TODO webpack 6: consider removing alternatives without dashes
  214. /** @type {Map<string, () => string>} */
  215. const replacements = new Map([
  216. ["identifier", identifier],
  217. ["short-identifier", shortIdentifier],
  218. ["resource", resource],
  219. ["resource-path", resourcePath],
  220. // cSpell:words resourcepath
  221. ["resourcepath", resourcePath],
  222. ["absolute-resource-path", absoluteResourcePath],
  223. ["abs-resource-path", absoluteResourcePath],
  224. // cSpell:words absoluteresource
  225. ["absoluteresource-path", absoluteResourcePath],
  226. // cSpell:words absresource
  227. ["absresource-path", absoluteResourcePath],
  228. // cSpell:words resourcepath
  229. ["absolute-resourcepath", absoluteResourcePath],
  230. // cSpell:words resourcepath
  231. ["abs-resourcepath", absoluteResourcePath],
  232. // cSpell:words absoluteresourcepath
  233. ["absoluteresourcepath", absoluteResourcePath],
  234. // cSpell:words absresourcepath
  235. ["absresourcepath", absoluteResourcePath],
  236. ["all-loaders", allLoaders],
  237. // cSpell:words allloaders
  238. ["allloaders", allLoaders],
  239. ["loaders", loaders],
  240. ["query", query],
  241. ["id", moduleId],
  242. ["hash", hash],
  243. ["namespace", () => opts.namespace]
  244. ]);
  245. // TODO webpack 6: consider removing weird double placeholders
  246. return /** @type {string} */ (opts.moduleFilenameTemplate)
  247. .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
  248. .replace(
  249. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
  250. "[short-identifier]"
  251. )
  252. .replace(SQUARE_BRACKET_TAG_REGEXP, (match, content) => {
  253. if (content.length + 2 === match.length) {
  254. const replacement = replacements.get(content.toLowerCase());
  255. if (replacement !== undefined) {
  256. return replacement();
  257. }
  258. } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
  259. return `[${match.slice(2, -2)}]`;
  260. }
  261. return match;
  262. });
  263. };
  264. /**
  265. * Replaces duplicate items in an array with new values generated by a callback function.
  266. * 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.
  267. * The callback function should return the new value for the duplicate item.
  268. * @template T
  269. * @param {T[]} array the array with duplicates to be replaced
  270. * @param {(duplicateItem: T, duplicateItemIndex: number, numberOfTimesReplaced: number) => T} fn callback function to generate new values for the duplicate items
  271. * @param {(firstElement: T, nextElement: T) => -1 | 0 | 1=} comparator optional comparator function to sort the duplicate items
  272. * @returns {T[]} the array with duplicates replaced
  273. * @example
  274. * ```js
  275. * const array = ["a", "b", "c", "a", "b", "a"];
  276. * const result = ModuleFilenameHelpers.replaceDuplicates(array, (item, index, count) => `${item}-${count}`);
  277. * // result: ["a-1", "b-1", "c", "a-2", "b-2", "a-3"]
  278. * ```
  279. */
  280. ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
  281. const countMap = Object.create(null);
  282. const posMap = Object.create(null);
  283. for (const [idx, item] of array.entries()) {
  284. countMap[item] = countMap[item] || [];
  285. countMap[item].push(idx);
  286. posMap[item] = 0;
  287. }
  288. if (comparator) {
  289. for (const item of Object.keys(countMap)) {
  290. countMap[item].sort(comparator);
  291. }
  292. }
  293. return array.map((item, i) => {
  294. if (countMap[item].length > 1) {
  295. if (comparator && countMap[item][0] === i) return item;
  296. return fn(item, i, posMap[item]++);
  297. }
  298. return item;
  299. });
  300. };
  301. /**
  302. * Tests if a string matches a RegExp or an array of RegExp.
  303. * @param {string} str string to test
  304. * @param {Matcher} test value which will be used to match against the string
  305. * @returns {boolean} true, when the RegExp matches
  306. * @example
  307. * ```js
  308. * ModuleFilenameHelpers.matchPart("foo.js", "foo"); // true
  309. * ModuleFilenameHelpers.matchPart("foo.js", "foo.js"); // true
  310. * ModuleFilenameHelpers.matchPart("foo.js", "foo."); // false
  311. * ModuleFilenameHelpers.matchPart("foo.js", "foo*"); // false
  312. * ModuleFilenameHelpers.matchPart("foo.js", "foo.*"); // true
  313. * ModuleFilenameHelpers.matchPart("foo.js", /^foo/); // true
  314. * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
  315. * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
  316. * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, /^bar/]); // true
  317. * ModuleFilenameHelpers.matchPart("foo.js", [/^baz/, /^bar/]); // false
  318. * ```
  319. */
  320. const matchPart = (str, test) => {
  321. if (!test) return true;
  322. if (test instanceof RegExp) {
  323. return test.test(str);
  324. } else if (typeof test === "string") {
  325. return str.startsWith(test);
  326. } else if (typeof test === "function") {
  327. return test(str);
  328. }
  329. return test.some((test) => matchPart(str, test));
  330. };
  331. ModuleFilenameHelpers.matchPart = matchPart;
  332. /**
  333. * Tests if a string matches a match object. The match object can have the following properties:
  334. * - `test`: a RegExp or an array of RegExp
  335. * - `include`: a RegExp or an array of RegExp
  336. * - `exclude`: a RegExp or an array of RegExp
  337. *
  338. * The `test` property is tested first, then `include` and then `exclude`.
  339. * @param {MatchObject} obj a match object to test against the string
  340. * @param {string} str string to test against the matching object
  341. * @returns {boolean} true, when the object matches
  342. * @example
  343. * ```js
  344. * ModuleFilenameHelpers.matchObject({ test: "foo.js" }, "foo.js"); // true
  345. * ModuleFilenameHelpers.matchObject({ test: /^foo/ }, "foo.js"); // true
  346. * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "foo.js"); // true
  347. * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "baz.js"); // false
  348. * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "foo.js"); // true
  349. * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "bar.js"); // false
  350. * ModuleFilenameHelpers.matchObject({ include: /^foo/ }, "foo.js"); // true
  351. * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "foo.js"); // true
  352. * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "baz.js"); // false
  353. * ModuleFilenameHelpers.matchObject({ exclude: "foo.js" }, "foo.js"); // false
  354. * ModuleFilenameHelpers.matchObject({ exclude: [/^foo/, "bar"] }, "foo.js"); // false
  355. * ```
  356. */
  357. ModuleFilenameHelpers.matchObject = (obj, str) => {
  358. if (obj.test && !ModuleFilenameHelpers.matchPart(str, obj.test)) {
  359. return false;
  360. }
  361. if (obj.include && !ModuleFilenameHelpers.matchPart(str, obj.include)) {
  362. return false;
  363. }
  364. if (obj.exclude && ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
  365. return false;
  366. }
  367. return true;
  368. };