ModuleFilenameHelpers.js 14 KB

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