ModuleFilenameHelpers.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. const ModuleFilenameHelpers = exports;
  14. // TODO webpack 6: consider removing these
  15. ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
  16. ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE =
  17. /\[all-?loaders\]\[resource\]/gi;
  18. ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
  19. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
  20. ModuleFilenameHelpers.RESOURCE = "[resource]";
  21. ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
  22. ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
  23. // cSpell:words olute
  24. ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
  25. /\[abs(olute)?-?resource-?path\]/gi;
  26. ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
  27. ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
  28. ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
  29. ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
  30. ModuleFilenameHelpers.LOADERS = "[loaders]";
  31. ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
  32. ModuleFilenameHelpers.QUERY = "[query]";
  33. ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
  34. ModuleFilenameHelpers.ID = "[id]";
  35. ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
  36. ModuleFilenameHelpers.HASH = "[hash]";
  37. ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
  38. ModuleFilenameHelpers.NAMESPACE = "[namespace]";
  39. ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
  40. const getAfter = (strFn, token) => {
  41. return () => {
  42. const str = strFn();
  43. const idx = str.indexOf(token);
  44. return idx < 0 ? "" : str.slice(idx);
  45. };
  46. };
  47. const getBefore = (strFn, token) => {
  48. return () => {
  49. const str = strFn();
  50. const idx = str.lastIndexOf(token);
  51. return idx < 0 ? "" : str.slice(0, idx);
  52. };
  53. };
  54. const getHash = (strFn, hashFunction) => {
  55. return () => {
  56. const hash = createHash(hashFunction);
  57. hash.update(strFn());
  58. const digest = /** @type {string} */ (hash.digest("hex"));
  59. return digest.slice(0, 4);
  60. };
  61. };
  62. const asRegExp = test => {
  63. if (typeof test === "string") {
  64. test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
  65. }
  66. return test;
  67. };
  68. const lazyObject = obj => {
  69. const newObj = {};
  70. for (const key of Object.keys(obj)) {
  71. const fn = obj[key];
  72. Object.defineProperty(newObj, key, {
  73. get: () => fn(),
  74. set: v => {
  75. Object.defineProperty(newObj, key, {
  76. value: v,
  77. enumerable: true,
  78. writable: true
  79. });
  80. },
  81. enumerable: true,
  82. configurable: true
  83. });
  84. }
  85. return newObj;
  86. };
  87. const REGEXP = /\[\\*([\w-]+)\\*\]/gi;
  88. /**
  89. *
  90. * @param {Module | string} module the module
  91. * @param {TODO} options options
  92. * @param {Object} contextInfo context info
  93. * @param {RequestShortener} contextInfo.requestShortener requestShortener
  94. * @param {ChunkGraph} contextInfo.chunkGraph chunk graph
  95. * @param {string | Hash} contextInfo.hashFunction the hash function to use
  96. * @returns {string} the filename
  97. */
  98. ModuleFilenameHelpers.createFilename = (
  99. module = "",
  100. options,
  101. { requestShortener, chunkGraph, hashFunction = "md4" }
  102. ) => {
  103. const opts = {
  104. namespace: "",
  105. moduleFilenameTemplate: "",
  106. ...(typeof options === "object"
  107. ? options
  108. : {
  109. moduleFilenameTemplate: options
  110. })
  111. };
  112. let absoluteResourcePath;
  113. let hash;
  114. let identifier;
  115. let moduleId;
  116. let shortIdentifier;
  117. if (typeof module === "string") {
  118. shortIdentifier = memoize(() => requestShortener.shorten(module));
  119. identifier = shortIdentifier;
  120. moduleId = () => "";
  121. absoluteResourcePath = () => module.split("!").pop();
  122. hash = getHash(identifier, hashFunction);
  123. } else {
  124. shortIdentifier = memoize(() =>
  125. module.readableIdentifier(requestShortener)
  126. );
  127. identifier = memoize(() => requestShortener.shorten(module.identifier()));
  128. moduleId = () => chunkGraph.getModuleId(module);
  129. absoluteResourcePath = () =>
  130. module instanceof NormalModule
  131. ? module.resource
  132. : module.identifier().split("!").pop();
  133. hash = getHash(identifier, hashFunction);
  134. }
  135. const resource = memoize(() => shortIdentifier().split("!").pop());
  136. const loaders = getBefore(shortIdentifier, "!");
  137. const allLoaders = getBefore(identifier, "!");
  138. const query = getAfter(resource, "?");
  139. const resourcePath = () => {
  140. const q = query().length;
  141. return q === 0 ? resource() : resource().slice(0, -q);
  142. };
  143. if (typeof opts.moduleFilenameTemplate === "function") {
  144. return opts.moduleFilenameTemplate(
  145. lazyObject({
  146. identifier: identifier,
  147. shortIdentifier: shortIdentifier,
  148. resource: resource,
  149. resourcePath: memoize(resourcePath),
  150. absoluteResourcePath: memoize(absoluteResourcePath),
  151. allLoaders: memoize(allLoaders),
  152. query: memoize(query),
  153. moduleId: memoize(moduleId),
  154. hash: memoize(hash),
  155. namespace: () => opts.namespace
  156. })
  157. );
  158. }
  159. // TODO webpack 6: consider removing alternatives without dashes
  160. /** @type {Map<string, function(): string>} */
  161. const replacements = new Map([
  162. ["identifier", identifier],
  163. ["short-identifier", shortIdentifier],
  164. ["resource", resource],
  165. ["resource-path", resourcePath],
  166. // cSpell:words resourcepath
  167. ["resourcepath", resourcePath],
  168. ["absolute-resource-path", absoluteResourcePath],
  169. ["abs-resource-path", absoluteResourcePath],
  170. // cSpell:words absoluteresource
  171. ["absoluteresource-path", absoluteResourcePath],
  172. // cSpell:words absresource
  173. ["absresource-path", absoluteResourcePath],
  174. // cSpell:words resourcepath
  175. ["absolute-resourcepath", absoluteResourcePath],
  176. // cSpell:words resourcepath
  177. ["abs-resourcepath", absoluteResourcePath],
  178. // cSpell:words absoluteresourcepath
  179. ["absoluteresourcepath", absoluteResourcePath],
  180. // cSpell:words absresourcepath
  181. ["absresourcepath", absoluteResourcePath],
  182. ["all-loaders", allLoaders],
  183. // cSpell:words allloaders
  184. ["allloaders", allLoaders],
  185. ["loaders", loaders],
  186. ["query", query],
  187. ["id", moduleId],
  188. ["hash", hash],
  189. ["namespace", () => opts.namespace]
  190. ]);
  191. // TODO webpack 6: consider removing weird double placeholders
  192. return opts.moduleFilenameTemplate
  193. .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
  194. .replace(
  195. ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
  196. "[short-identifier]"
  197. )
  198. .replace(REGEXP, (match, content) => {
  199. if (content.length + 2 === match.length) {
  200. const replacement = replacements.get(content.toLowerCase());
  201. if (replacement !== undefined) {
  202. return replacement();
  203. }
  204. } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
  205. return `[${match.slice(2, -2)}]`;
  206. }
  207. return match;
  208. });
  209. };
  210. ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
  211. const countMap = Object.create(null);
  212. const posMap = Object.create(null);
  213. array.forEach((item, idx) => {
  214. countMap[item] = countMap[item] || [];
  215. countMap[item].push(idx);
  216. posMap[item] = 0;
  217. });
  218. if (comparator) {
  219. Object.keys(countMap).forEach(item => {
  220. countMap[item].sort(comparator);
  221. });
  222. }
  223. return array.map((item, i) => {
  224. if (countMap[item].length > 1) {
  225. if (comparator && countMap[item][0] === i) return item;
  226. return fn(item, i, posMap[item]++);
  227. } else {
  228. return item;
  229. }
  230. });
  231. };
  232. ModuleFilenameHelpers.matchPart = (str, test) => {
  233. if (!test) return true;
  234. test = asRegExp(test);
  235. if (Array.isArray(test)) {
  236. return test.map(asRegExp).some(regExp => regExp.test(str));
  237. } else {
  238. return test.test(str);
  239. }
  240. };
  241. ModuleFilenameHelpers.matchObject = (obj, str) => {
  242. if (obj.test) {
  243. if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
  244. return false;
  245. }
  246. }
  247. if (obj.include) {
  248. if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
  249. return false;
  250. }
  251. }
  252. if (obj.exclude) {
  253. if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
  254. return false;
  255. }
  256. }
  257. return true;
  258. };