提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
14 KiB

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