IdHelpers.js 13 KB

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