10
0

ModuleFilenameHelpers.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 createHash = require("./util/createHash");
  8. const memoize = require("./util/memoize");
  9. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  10. /** @typedef {import("./Module")} Module */
  11. /** @typedef {import("./RequestShortener")} RequestShortener */
  12. /** @typedef {typeof import("./util/Hash")} Hash */
  13. /** @typedef {string | RegExp | (string | RegExp)[]} Matcher */
  14. /** @typedef {{test?: Matcher, include?: Matcher, exclude?: Matcher }} MatchObject */
  15. const ModuleFilenameHelpers = module.exports;
  16. // TODO webpack 6: consider removing these
  17. ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
  18. ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE =
  19. /\[all-?loaders\]\[resource\]/gi;
  20. ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
  21. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
  22. ModuleFilenameHelpers.RESOURCE = "[resource]";
  23. ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
  24. ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
  25. // cSpell:words olute
  26. ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
  27. /\[abs(olute)?-?resource-?path\]/gi;
  28. ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
  29. ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
  30. ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
  31. ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
  32. ModuleFilenameHelpers.LOADERS = "[loaders]";
  33. ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
  34. ModuleFilenameHelpers.QUERY = "[query]";
  35. ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
  36. ModuleFilenameHelpers.ID = "[id]";
  37. ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
  38. ModuleFilenameHelpers.HASH = "[hash]";
  39. ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
  40. ModuleFilenameHelpers.NAMESPACE = "[namespace]";
  41. ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
  42. /** @typedef {() => string} ReturnStringCallback */
  43. /**
  44. * Returns a function that returns the part of the string after the token
  45. * @param {ReturnStringCallback} strFn the function to get the string
  46. * @param {string} token the token to search for
  47. * @returns {ReturnStringCallback} a function that returns the part of the string after the token
  48. */
  49. const getAfter = (strFn, token) => () => {
  50. const str = strFn();
  51. const idx = str.indexOf(token);
  52. return idx < 0 ? "" : str.slice(idx);
  53. };
  54. /**
  55. * Returns a function that returns the part of the string before the token
  56. * @param {ReturnStringCallback} strFn the function to get the string
  57. * @param {string} token the token to search for
  58. * @returns {ReturnStringCallback} a function that returns the part of the string before the token
  59. */
  60. const getBefore = (strFn, token) => () => {
  61. const str = strFn();
  62. const idx = str.lastIndexOf(token);
  63. return idx < 0 ? "" : str.slice(0, idx);
  64. };
  65. /**
  66. * Returns a function that returns a hash of the string
  67. * @param {ReturnStringCallback} strFn the function to get the string
  68. * @param {string | Hash=} hashFunction the hash function to use
  69. * @returns {ReturnStringCallback} a function that returns the hash of the string
  70. */
  71. const getHash =
  72. (strFn, hashFunction = "md4") =>
  73. () => {
  74. const hash = createHash(hashFunction);
  75. hash.update(strFn());
  76. const digest = /** @type {string} */ (hash.digest("hex"));
  77. return digest.slice(0, 4);
  78. };
  79. /**
  80. * Returns a function that returns the string with the token replaced with the replacement
  81. * @param {string|RegExp} test A regular expression string or Regular Expression object
  82. * @returns {RegExp} A regular expression object
  83. * @example
  84. * ```js
  85. * const test = asRegExp("test");
  86. * test.test("test"); // true
  87. *
  88. * const test2 = asRegExp(/test/);
  89. * test2.test("test"); // true
  90. * ```
  91. */
  92. const asRegExp = test => {
  93. if (typeof test === "string") {
  94. // Escape special characters in the string to prevent them from being interpreted as special characters in a regular expression. Do this by
  95. // adding a backslash before each special character
  96. test = new RegExp(`^${test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")}`);
  97. }
  98. return test;
  99. };
  100. /**
  101. * @template T
  102. * Returns a lazy object. The object is lazy in the sense that the properties are
  103. * only evaluated when they are accessed. This is only obtained by setting a function as the value for each key.
  104. * @param {Record<string, () => T>} obj the object to convert to a lazy access object
  105. * @returns {object} the lazy access object
  106. */
  107. const lazyObject = obj => {
  108. const newObj = {};
  109. for (const key of Object.keys(obj)) {
  110. const fn = obj[key];
  111. Object.defineProperty(newObj, key, {
  112. get: () => fn(),
  113. set: v => {
  114. Object.defineProperty(newObj, key, {
  115. value: v,
  116. enumerable: true,
  117. writable: true
  118. });
  119. },
  120. enumerable: true,
  121. configurable: true
  122. });
  123. }
  124. return newObj;
  125. };
  126. const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/gi;
  127. /**
  128. * @param {Module | string} module the module
  129. * @param {TODO} options options
  130. * @param {object} contextInfo context info
  131. * @param {RequestShortener} contextInfo.requestShortener requestShortener
  132. * @param {ChunkGraph} contextInfo.chunkGraph chunk graph
  133. * @param {string | Hash=} contextInfo.hashFunction the hash function to use
  134. * @returns {string} the filename
  135. */
  136. ModuleFilenameHelpers.createFilename = (
  137. // eslint-disable-next-line default-param-last
  138. module = "",
  139. options,
  140. { requestShortener, chunkGraph, hashFunction = "md4" }
  141. ) => {
  142. const opts = {
  143. namespace: "",
  144. moduleFilenameTemplate: "",
  145. ...(typeof options === "object"
  146. ? options
  147. : {
  148. moduleFilenameTemplate: options
  149. })
  150. };
  151. let absoluteResourcePath;
  152. let hash;
  153. /** @type {ReturnStringCallback} */
  154. let identifier;
  155. /** @type {ReturnStringCallback} */
  156. let moduleId;
  157. /** @type {ReturnStringCallback} */
  158. let shortIdentifier;
  159. if (typeof module === "string") {
  160. shortIdentifier =
  161. /** @type {ReturnStringCallback} */
  162. (memoize(() => requestShortener.shorten(module)));
  163. identifier = shortIdentifier;
  164. moduleId = () => "";
  165. absoluteResourcePath = () => 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. : 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. lazyObject({
  196. identifier,
  197. shortIdentifier,
  198. resource,
  199. resourcePath: memoize(resourcePath),
  200. absoluteResourcePath: memoize(absoluteResourcePath),
  201. loaders: memoize(loaders),
  202. allLoaders: memoize(allLoaders),
  203. query: memoize(query),
  204. moduleId: memoize(moduleId),
  205. hash: memoize(hash),
  206. namespace: () => opts.namespace
  207. })
  208. );
  209. }
  210. // TODO webpack 6: consider removing alternatives without dashes
  211. /** @type {Map<string, function(): 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. ModuleFilenameHelpers.matchPart = (str, test) => {
  318. if (!test) return true;
  319. if (Array.isArray(test)) {
  320. return test.map(asRegExp).some(regExp => regExp.test(str));
  321. }
  322. return asRegExp(test).test(str);
  323. };
  324. /**
  325. * Tests if a string matches a match object. The match object can have the following properties:
  326. * - `test`: a RegExp or an array of RegExp
  327. * - `include`: a RegExp or an array of RegExp
  328. * - `exclude`: a RegExp or an array of RegExp
  329. *
  330. * The `test` property is tested first, then `include` and then `exclude`.
  331. * @param {MatchObject} obj a match object to test against the string
  332. * @param {string} str string to test against the matching object
  333. * @returns {boolean} true, when the object matches
  334. * @example
  335. * ```js
  336. * ModuleFilenameHelpers.matchObject({ test: "foo.js" }, "foo.js"); // true
  337. * ModuleFilenameHelpers.matchObject({ test: /^foo/ }, "foo.js"); // true
  338. * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "foo.js"); // true
  339. * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "baz.js"); // false
  340. * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "foo.js"); // true
  341. * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "bar.js"); // false
  342. * ModuleFilenameHelpers.matchObject({ include: /^foo/ }, "foo.js"); // true
  343. * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "foo.js"); // true
  344. * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "baz.js"); // false
  345. * ModuleFilenameHelpers.matchObject({ exclude: "foo.js" }, "foo.js"); // false
  346. * ModuleFilenameHelpers.matchObject({ exclude: [/^foo/, "bar"] }, "foo.js"); // false
  347. * ```
  348. */
  349. ModuleFilenameHelpers.matchObject = (obj, str) => {
  350. if (obj.test && !ModuleFilenameHelpers.matchPart(str, obj.test)) {
  351. return false;
  352. }
  353. if (obj.include && !ModuleFilenameHelpers.matchPart(str, obj.include)) {
  354. return false;
  355. }
  356. if (obj.exclude && ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
  357. return false;
  358. }
  359. return true;
  360. };