IdHelpers.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const createHash = require("../util/createHash");
  7. const { makePathsRelative } = require("../util/identifier");
  8. const numberHash = require("../util/numberHash");
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  11. /** @typedef {import("../Compilation")} Compilation */
  12. /** @typedef {import("../Module")} Module */
  13. /** @typedef {typeof import("../util/Hash")} Hash */
  14. /** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
  15. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  16. /**
  17. * @param {string} str string to hash
  18. * @param {number} len max length of the hash
  19. * @param {string | Hash} hashFunction hash function to use
  20. * @returns {string} hash
  21. */
  22. const getHash = (str, len, hashFunction) => {
  23. const hash = createHash(hashFunction);
  24. hash.update(str);
  25. const digest = hash.digest("hex");
  26. return digest.slice(0, len);
  27. };
  28. /**
  29. * @param {string} str the string
  30. * @returns {string} string prefixed by an underscore if it is a number
  31. */
  32. const avoidNumber = (str) => {
  33. // max length of a number is 21 chars, bigger numbers a written as "...e+xx"
  34. if (str.length > 21) return str;
  35. const firstChar = str.charCodeAt(0);
  36. // skip everything that doesn't look like a number
  37. // charCodes: "-": 45, "1": 49, "9": 57
  38. if (firstChar < 49) {
  39. if (firstChar !== 45) return str;
  40. } else if (firstChar > 57) {
  41. return str;
  42. }
  43. if (str === String(Number(str))) {
  44. return `_${str}`;
  45. }
  46. return str;
  47. };
  48. /**
  49. * @param {string} request the request
  50. * @returns {string} id representation
  51. */
  52. const requestToId = (request) =>
  53. request.replace(/^(\.\.?\/)+/, "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
  54. /**
  55. * @param {string} string the string
  56. * @param {string} delimiter separator for string and hash
  57. * @param {string | Hash} hashFunction hash function to use
  58. * @returns {string} string with limited max length to 100 chars
  59. */
  60. const shortenLongString = (string, delimiter, hashFunction) => {
  61. if (string.length < 100) return string;
  62. return (
  63. string.slice(0, 100 - 6 - delimiter.length) +
  64. delimiter +
  65. getHash(string, 6, hashFunction)
  66. );
  67. };
  68. /**
  69. * @param {Module} module the module
  70. * @param {string} context context directory
  71. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  72. * @returns {string} short module name
  73. */
  74. const getShortModuleName = (module, context, associatedObjectForCache) => {
  75. const libIdent = module.libIdent({ context, associatedObjectForCache });
  76. if (libIdent) return avoidNumber(libIdent);
  77. const nameForCondition = module.nameForCondition();
  78. if (nameForCondition) {
  79. return avoidNumber(
  80. makePathsRelative(context, nameForCondition, associatedObjectForCache)
  81. );
  82. }
  83. return "";
  84. };
  85. /**
  86. * @param {string} shortName the short name
  87. * @param {Module} module the module
  88. * @param {string} context context directory
  89. * @param {string | Hash} hashFunction hash function to use
  90. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  91. * @returns {string} long module name
  92. */
  93. const getLongModuleName = (
  94. shortName,
  95. module,
  96. context,
  97. hashFunction,
  98. associatedObjectForCache
  99. ) => {
  100. const fullName = getFullModuleName(module, context, associatedObjectForCache);
  101. return `${shortName}?${getHash(fullName, 4, hashFunction)}`;
  102. };
  103. /**
  104. * @param {Module} module the module
  105. * @param {string} context context directory
  106. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  107. * @returns {string} full module name
  108. */
  109. const getFullModuleName = (module, context, associatedObjectForCache) =>
  110. makePathsRelative(context, module.identifier(), associatedObjectForCache);
  111. /**
  112. * @param {Chunk} chunk the chunk
  113. * @param {ChunkGraph} chunkGraph the chunk graph
  114. * @param {string} context context directory
  115. * @param {string} delimiter delimiter for names
  116. * @param {string | Hash} hashFunction hash function to use
  117. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  118. * @returns {string} short chunk name
  119. */
  120. const getShortChunkName = (
  121. chunk,
  122. chunkGraph,
  123. context,
  124. delimiter,
  125. hashFunction,
  126. associatedObjectForCache
  127. ) => {
  128. const modules = chunkGraph.getChunkRootModules(chunk);
  129. const shortModuleNames = modules.map((m) =>
  130. requestToId(getShortModuleName(m, context, associatedObjectForCache))
  131. );
  132. chunk.idNameHints.sort();
  133. const chunkName = [...chunk.idNameHints, ...shortModuleNames]
  134. .filter(Boolean)
  135. .join(delimiter);
  136. return shortenLongString(chunkName, delimiter, hashFunction);
  137. };
  138. /**
  139. * @param {Chunk} chunk the chunk
  140. * @param {ChunkGraph} chunkGraph the chunk graph
  141. * @param {string} context context directory
  142. * @param {string} delimiter delimiter for names
  143. * @param {string | Hash} hashFunction hash function to use
  144. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  145. * @returns {string} short chunk name
  146. */
  147. const getLongChunkName = (
  148. chunk,
  149. chunkGraph,
  150. context,
  151. delimiter,
  152. hashFunction,
  153. associatedObjectForCache
  154. ) => {
  155. const modules = chunkGraph.getChunkRootModules(chunk);
  156. const shortModuleNames = modules.map((m) =>
  157. requestToId(getShortModuleName(m, context, associatedObjectForCache))
  158. );
  159. const longModuleNames = modules.map((m) =>
  160. requestToId(
  161. getLongModuleName("", m, context, hashFunction, associatedObjectForCache)
  162. )
  163. );
  164. chunk.idNameHints.sort();
  165. const chunkName = [
  166. ...chunk.idNameHints,
  167. ...shortModuleNames,
  168. ...longModuleNames
  169. ]
  170. .filter(Boolean)
  171. .join(delimiter);
  172. return shortenLongString(chunkName, delimiter, hashFunction);
  173. };
  174. /**
  175. * @param {Chunk} chunk the chunk
  176. * @param {ChunkGraph} chunkGraph the chunk graph
  177. * @param {string} context context directory
  178. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  179. * @returns {string} full chunk name
  180. */
  181. const getFullChunkName = (
  182. chunk,
  183. chunkGraph,
  184. context,
  185. associatedObjectForCache
  186. ) => {
  187. if (chunk.name) return chunk.name;
  188. const modules = chunkGraph.getChunkRootModules(chunk);
  189. const fullModuleNames = modules.map((m) =>
  190. makePathsRelative(context, m.identifier(), associatedObjectForCache)
  191. );
  192. return fullModuleNames.join();
  193. };
  194. /**
  195. * @template K
  196. * @template V
  197. * @param {Map<K, V[]>} map a map from key to values
  198. * @param {K} key key
  199. * @param {V} value value
  200. * @returns {void}
  201. */
  202. const addToMapOfItems = (map, key, value) => {
  203. let array = map.get(key);
  204. if (array === undefined) {
  205. array = [];
  206. map.set(key, array);
  207. }
  208. array.push(value);
  209. };
  210. /** @typedef {Set<string>} UsedModuleIds */
  211. /**
  212. * @param {Compilation} compilation the compilation
  213. * @param {((module: Module) => boolean)=} filter filter modules
  214. * @returns {[UsedModuleIds, Module[]]} used module ids as strings and modules without id matching the filter
  215. */
  216. const getUsedModuleIdsAndModules = (compilation, filter) => {
  217. const chunkGraph = compilation.chunkGraph;
  218. const modules = [];
  219. /** @type {UsedModuleIds} */
  220. const usedIds = new Set();
  221. if (compilation.usedModuleIds) {
  222. for (const id of compilation.usedModuleIds) {
  223. usedIds.add(String(id));
  224. }
  225. }
  226. for (const module of compilation.modules) {
  227. if (!module.needId) continue;
  228. const moduleId = chunkGraph.getModuleId(module);
  229. if (moduleId !== null) {
  230. usedIds.add(String(moduleId));
  231. } else if (
  232. (!filter || filter(module)) &&
  233. (chunkGraph.getNumberOfModuleChunks(module) !== 0 ||
  234. // CSS modules need IDs even when not in chunks, for generating CSS class names(i.e. [id]-[local])
  235. /** @type {BuildMeta} */ (module.buildMeta).isCSSModule)
  236. ) {
  237. modules.push(module);
  238. }
  239. }
  240. return [usedIds, modules];
  241. };
  242. /** @typedef {Set<string>} UsedChunkIds */
  243. /**
  244. * @param {Compilation} compilation the compilation
  245. * @returns {UsedChunkIds} used chunk ids as strings
  246. */
  247. const getUsedChunkIds = (compilation) => {
  248. /** @type {UsedChunkIds} */
  249. const usedIds = new Set();
  250. if (compilation.usedChunkIds) {
  251. for (const id of compilation.usedChunkIds) {
  252. usedIds.add(String(id));
  253. }
  254. }
  255. for (const chunk of compilation.chunks) {
  256. const chunkId = chunk.id;
  257. if (chunkId !== null) {
  258. usedIds.add(String(chunkId));
  259. }
  260. }
  261. return usedIds;
  262. };
  263. /**
  264. * @template T
  265. * @param {Iterable<T>} items list of items to be named
  266. * @param {(item: T) => string} getShortName get a short name for an item
  267. * @param {(item: T, name: string) => string} getLongName get a long name for an item
  268. * @param {(a: T, b: T) => -1 | 0 | 1} comparator order of items
  269. * @param {Set<string>} usedIds already used ids, will not be assigned
  270. * @param {(item: T, name: string) => void} assignName assign a name to an item
  271. * @returns {T[]} list of items without a name
  272. */
  273. const assignNames = (
  274. items,
  275. getShortName,
  276. getLongName,
  277. comparator,
  278. usedIds,
  279. assignName
  280. ) => {
  281. /**
  282. * @template T
  283. * @typedef {Map<string, T[]>} MapToItem
  284. */
  285. /** @type {MapToItem<T>} */
  286. const nameToItems = new Map();
  287. for (const item of items) {
  288. const name = getShortName(item);
  289. addToMapOfItems(nameToItems, name, item);
  290. }
  291. /** @type {MapToItem<T>} */
  292. const nameToItems2 = new Map();
  293. for (const [name, items] of nameToItems) {
  294. if (items.length > 1 || !name) {
  295. for (const item of items) {
  296. const longName = getLongName(item, name);
  297. addToMapOfItems(nameToItems2, longName, item);
  298. }
  299. } else {
  300. addToMapOfItems(nameToItems2, name, items[0]);
  301. }
  302. }
  303. /** @type {T[]} */
  304. const unnamedItems = [];
  305. for (const [name, items] of nameToItems2) {
  306. if (!name) {
  307. for (const item of items) {
  308. unnamedItems.push(item);
  309. }
  310. } else if (items.length === 1 && !usedIds.has(name)) {
  311. assignName(items[0], name);
  312. usedIds.add(name);
  313. } else {
  314. items.sort(comparator);
  315. let i = 0;
  316. for (const item of items) {
  317. while (nameToItems2.has(name + i) && usedIds.has(name + i)) i++;
  318. assignName(item, name + i);
  319. usedIds.add(name + i);
  320. i++;
  321. }
  322. }
  323. }
  324. unnamedItems.sort(comparator);
  325. return unnamedItems;
  326. };
  327. /**
  328. * @template T
  329. * @param {T[]} items list of items to be named
  330. * @param {(item: T) => string} getName get a name for an item
  331. * @param {(a: T, n: T) => -1 | 0 | 1} comparator order of items
  332. * @param {(item: T, id: number) => boolean} assignId assign an id to an item
  333. * @param {number[]} ranges usable ranges for ids
  334. * @param {number} expandFactor factor to create more ranges
  335. * @param {number} extraSpace extra space to allocate, i. e. when some ids are already used
  336. * @param {number} salt salting number to initialize hashing
  337. * @returns {void}
  338. */
  339. const assignDeterministicIds = (
  340. items,
  341. getName,
  342. comparator,
  343. assignId,
  344. ranges = [10],
  345. expandFactor = 10,
  346. extraSpace = 0,
  347. salt = 0
  348. ) => {
  349. items.sort(comparator);
  350. // max 5% fill rate
  351. const optimalRange = Math.min(
  352. items.length * 20 + extraSpace,
  353. Number.MAX_SAFE_INTEGER
  354. );
  355. let i = 0;
  356. let range = ranges[i];
  357. while (range < optimalRange) {
  358. i++;
  359. if (i < ranges.length) {
  360. range = Math.min(ranges[i], Number.MAX_SAFE_INTEGER);
  361. } else if (expandFactor) {
  362. range = Math.min(range * expandFactor, Number.MAX_SAFE_INTEGER);
  363. } else {
  364. break;
  365. }
  366. }
  367. for (const item of items) {
  368. const ident = getName(item);
  369. let id;
  370. let i = salt;
  371. do {
  372. id = numberHash(ident + i++, range);
  373. } while (!assignId(item, id));
  374. }
  375. };
  376. /**
  377. * @param {UsedModuleIds} usedIds used ids
  378. * @param {Iterable<Module>} modules the modules
  379. * @param {Compilation} compilation the compilation
  380. * @returns {void}
  381. */
  382. const assignAscendingModuleIds = (usedIds, modules, compilation) => {
  383. const chunkGraph = compilation.chunkGraph;
  384. let nextId = 0;
  385. let assignId;
  386. if (usedIds.size > 0) {
  387. /**
  388. * @param {Module} module the module
  389. */
  390. assignId = (module) => {
  391. if (chunkGraph.getModuleId(module) === null) {
  392. while (usedIds.has(String(nextId))) nextId++;
  393. chunkGraph.setModuleId(module, nextId++);
  394. }
  395. };
  396. } else {
  397. /**
  398. * @param {Module} module the module
  399. */
  400. assignId = (module) => {
  401. if (chunkGraph.getModuleId(module) === null) {
  402. chunkGraph.setModuleId(module, nextId++);
  403. }
  404. };
  405. }
  406. for (const module of modules) {
  407. assignId(module);
  408. }
  409. };
  410. /**
  411. * @param {Iterable<Chunk>} chunks the chunks
  412. * @param {Compilation} compilation the compilation
  413. * @returns {void}
  414. */
  415. const assignAscendingChunkIds = (chunks, compilation) => {
  416. const usedIds = getUsedChunkIds(compilation);
  417. let nextId = 0;
  418. if (usedIds.size > 0) {
  419. for (const chunk of chunks) {
  420. if (chunk.id === null) {
  421. while (usedIds.has(String(nextId))) nextId++;
  422. chunk.id = nextId;
  423. chunk.ids = [nextId];
  424. nextId++;
  425. }
  426. }
  427. } else {
  428. for (const chunk of chunks) {
  429. if (chunk.id === null) {
  430. chunk.id = nextId;
  431. chunk.ids = [nextId];
  432. nextId++;
  433. }
  434. }
  435. }
  436. };
  437. module.exports.assignAscendingChunkIds = assignAscendingChunkIds;
  438. module.exports.assignAscendingModuleIds = assignAscendingModuleIds;
  439. module.exports.assignDeterministicIds = assignDeterministicIds;
  440. module.exports.assignNames = assignNames;
  441. module.exports.getFullChunkName = getFullChunkName;
  442. module.exports.getFullModuleName = getFullModuleName;
  443. module.exports.getLongChunkName = getLongChunkName;
  444. module.exports.getLongModuleName = getLongModuleName;
  445. module.exports.getShortChunkName = getShortChunkName;
  446. module.exports.getShortModuleName = getShortModuleName;
  447. module.exports.getUsedChunkIds = getUsedChunkIds;
  448. module.exports.getUsedModuleIdsAndModules = getUsedModuleIdsAndModules;
  449. module.exports.requestToId = requestToId;