IdHelpers.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. module.exports.requestToId = requestToId;
  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. return "";
  83. };
  84. module.exports.getShortModuleName = getShortModuleName;
  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. module.exports.getLongModuleName = getLongModuleName;
  104. /**
  105. * @param {Module} module the module
  106. * @param {string} context context directory
  107. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  108. * @returns {string} full module name
  109. */
  110. const getFullModuleName = (module, context, associatedObjectForCache) =>
  111. makePathsRelative(context, module.identifier(), associatedObjectForCache);
  112. module.exports.getFullModuleName = getFullModuleName;
  113. /**
  114. * @param {Chunk} chunk the chunk
  115. * @param {ChunkGraph} chunkGraph the chunk graph
  116. * @param {string} context context directory
  117. * @param {string} delimiter delimiter for names
  118. * @param {string | Hash} hashFunction hash function to use
  119. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  120. * @returns {string} short chunk name
  121. */
  122. const getShortChunkName = (
  123. chunk,
  124. chunkGraph,
  125. context,
  126. delimiter,
  127. hashFunction,
  128. associatedObjectForCache
  129. ) => {
  130. const modules = chunkGraph.getChunkRootModules(chunk);
  131. const shortModuleNames = modules.map(m =>
  132. requestToId(getShortModuleName(m, context, associatedObjectForCache))
  133. );
  134. chunk.idNameHints.sort();
  135. const chunkName = Array.from(chunk.idNameHints)
  136. .concat(shortModuleNames)
  137. .filter(Boolean)
  138. .join(delimiter);
  139. return shortenLongString(chunkName, delimiter, hashFunction);
  140. };
  141. module.exports.getShortChunkName = getShortChunkName;
  142. /**
  143. * @param {Chunk} chunk the chunk
  144. * @param {ChunkGraph} chunkGraph the chunk graph
  145. * @param {string} context context directory
  146. * @param {string} delimiter delimiter for names
  147. * @param {string | Hash} hashFunction hash function to use
  148. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  149. * @returns {string} short chunk name
  150. */
  151. const getLongChunkName = (
  152. chunk,
  153. chunkGraph,
  154. context,
  155. delimiter,
  156. hashFunction,
  157. associatedObjectForCache
  158. ) => {
  159. const modules = chunkGraph.getChunkRootModules(chunk);
  160. const shortModuleNames = modules.map(m =>
  161. requestToId(getShortModuleName(m, context, associatedObjectForCache))
  162. );
  163. const longModuleNames = modules.map(m =>
  164. requestToId(
  165. getLongModuleName("", m, context, hashFunction, associatedObjectForCache)
  166. )
  167. );
  168. chunk.idNameHints.sort();
  169. const chunkName = Array.from(chunk.idNameHints)
  170. .concat(shortModuleNames, longModuleNames)
  171. .filter(Boolean)
  172. .join(delimiter);
  173. return shortenLongString(chunkName, delimiter, hashFunction);
  174. };
  175. module.exports.getLongChunkName = getLongChunkName;
  176. /**
  177. * @param {Chunk} chunk the chunk
  178. * @param {ChunkGraph} chunkGraph the chunk graph
  179. * @param {string} context context directory
  180. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  181. * @returns {string} full chunk name
  182. */
  183. const getFullChunkName = (
  184. chunk,
  185. chunkGraph,
  186. context,
  187. associatedObjectForCache
  188. ) => {
  189. if (chunk.name) return chunk.name;
  190. const modules = chunkGraph.getChunkRootModules(chunk);
  191. const fullModuleNames = modules.map(m =>
  192. makePathsRelative(context, m.identifier(), associatedObjectForCache)
  193. );
  194. return fullModuleNames.join();
  195. };
  196. module.exports.getFullChunkName = getFullChunkName;
  197. /**
  198. * @template K
  199. * @template V
  200. * @param {Map<K, V[]>} map a map from key to values
  201. * @param {K} key key
  202. * @param {V} value value
  203. * @returns {void}
  204. */
  205. const addToMapOfItems = (map, key, value) => {
  206. let array = map.get(key);
  207. if (array === undefined) {
  208. array = [];
  209. map.set(key, array);
  210. }
  211. array.push(value);
  212. };
  213. /**
  214. * @param {Compilation} compilation the compilation
  215. * @param {((module: Module) => boolean)=} filter filter modules
  216. * @returns {[Set<string>, Module[]]} used module ids as strings and modules without id matching the filter
  217. */
  218. const getUsedModuleIdsAndModules = (compilation, filter) => {
  219. const chunkGraph = compilation.chunkGraph;
  220. const modules = [];
  221. /** @type {Set<string>} */
  222. const usedIds = new Set();
  223. if (compilation.usedModuleIds) {
  224. for (const id of compilation.usedModuleIds) {
  225. usedIds.add(String(id));
  226. }
  227. }
  228. for (const module of compilation.modules) {
  229. if (!module.needId) continue;
  230. const moduleId = chunkGraph.getModuleId(module);
  231. if (moduleId !== null) {
  232. usedIds.add(String(moduleId));
  233. } else if (
  234. (!filter || filter(module)) &&
  235. chunkGraph.getNumberOfModuleChunks(module) !== 0
  236. ) {
  237. modules.push(module);
  238. }
  239. }
  240. return [usedIds, modules];
  241. };
  242. module.exports.getUsedModuleIdsAndModules = getUsedModuleIdsAndModules;
  243. /**
  244. * @param {Compilation} compilation the compilation
  245. * @returns {Set<string>} used chunk ids as strings
  246. */
  247. const getUsedChunkIds = compilation => {
  248. /** @type {Set<string>} */
  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. module.exports.getUsedChunkIds = getUsedChunkIds;
  264. /**
  265. * @template T
  266. * @param {Iterable<T>} items list of items to be named
  267. * @param {(item: T) => string} getShortName get a short name for an item
  268. * @param {(item: T, name: string) => string} getLongName get a long name for an item
  269. * @param {(a: T, b: T) => -1 | 0 | 1} comparator order of items
  270. * @param {Set<string>} usedIds already used ids, will not be assigned
  271. * @param {(item: T, name: string) => void} assignName assign a name to an item
  272. * @returns {T[]} list of items without a name
  273. */
  274. const assignNames = (
  275. items,
  276. getShortName,
  277. getLongName,
  278. comparator,
  279. usedIds,
  280. assignName
  281. ) => {
  282. /** @type {Map<string, T[]>} */
  283. const nameToItems = new Map();
  284. for (const item of items) {
  285. const name = getShortName(item);
  286. addToMapOfItems(nameToItems, name, item);
  287. }
  288. /** @type {Map<string, T[]>} */
  289. const nameToItems2 = new Map();
  290. for (const [name, items] of nameToItems) {
  291. if (items.length > 1 || !name) {
  292. for (const item of items) {
  293. const longName = getLongName(item, name);
  294. addToMapOfItems(nameToItems2, longName, item);
  295. }
  296. } else {
  297. addToMapOfItems(nameToItems2, name, items[0]);
  298. }
  299. }
  300. /** @type {T[]} */
  301. const unnamedItems = [];
  302. for (const [name, items] of nameToItems2) {
  303. if (!name) {
  304. for (const item of items) {
  305. unnamedItems.push(item);
  306. }
  307. } else if (items.length === 1 && !usedIds.has(name)) {
  308. assignName(items[0], name);
  309. usedIds.add(name);
  310. } else {
  311. items.sort(comparator);
  312. let i = 0;
  313. for (const item of items) {
  314. while (nameToItems2.has(name + i) && usedIds.has(name + i)) i++;
  315. assignName(item, name + i);
  316. usedIds.add(name + i);
  317. i++;
  318. }
  319. }
  320. }
  321. unnamedItems.sort(comparator);
  322. return unnamedItems;
  323. };
  324. module.exports.assignNames = assignNames;
  325. /**
  326. * @template T
  327. * @param {T[]} items list of items to be named
  328. * @param {(item: T) => string} getName get a name for an item
  329. * @param {(a: T, n: T) => -1 | 0 | 1} comparator order of items
  330. * @param {(item: T, id: number) => boolean} assignId assign an id to an item
  331. * @param {number[]} ranges usable ranges for ids
  332. * @param {number} expandFactor factor to create more ranges
  333. * @param {number} extraSpace extra space to allocate, i. e. when some ids are already used
  334. * @param {number} salt salting number to initialize hashing
  335. * @returns {void}
  336. */
  337. const assignDeterministicIds = (
  338. items,
  339. getName,
  340. comparator,
  341. assignId,
  342. ranges = [10],
  343. expandFactor = 10,
  344. extraSpace = 0,
  345. salt = 0
  346. ) => {
  347. items.sort(comparator);
  348. // max 5% fill rate
  349. const optimalRange = Math.min(
  350. items.length * 20 + extraSpace,
  351. Number.MAX_SAFE_INTEGER
  352. );
  353. let i = 0;
  354. let range = ranges[i];
  355. while (range < optimalRange) {
  356. i++;
  357. if (i < ranges.length) {
  358. range = Math.min(ranges[i], Number.MAX_SAFE_INTEGER);
  359. } else if (expandFactor) {
  360. range = Math.min(range * expandFactor, Number.MAX_SAFE_INTEGER);
  361. } else {
  362. break;
  363. }
  364. }
  365. for (const item of items) {
  366. const ident = getName(item);
  367. let id;
  368. let i = salt;
  369. do {
  370. id = numberHash(ident + i++, range);
  371. } while (!assignId(item, id));
  372. }
  373. };
  374. module.exports.assignDeterministicIds = assignDeterministicIds;
  375. /**
  376. * @param {Set<string>} usedIds used ids
  377. * @param {Iterable<Module>} modules the modules
  378. * @param {Compilation} compilation the compilation
  379. * @returns {void}
  380. */
  381. const assignAscendingModuleIds = (usedIds, modules, compilation) => {
  382. const chunkGraph = compilation.chunkGraph;
  383. let nextId = 0;
  384. let assignId;
  385. if (usedIds.size > 0) {
  386. /**
  387. * @param {Module} module the module
  388. */
  389. assignId = module => {
  390. if (chunkGraph.getModuleId(module) === null) {
  391. while (usedIds.has(String(nextId))) nextId++;
  392. chunkGraph.setModuleId(module, nextId++);
  393. }
  394. };
  395. } else {
  396. /**
  397. * @param {Module} module the module
  398. */
  399. assignId = module => {
  400. if (chunkGraph.getModuleId(module) === null) {
  401. chunkGraph.setModuleId(module, nextId++);
  402. }
  403. };
  404. }
  405. for (const module of modules) {
  406. assignId(module);
  407. }
  408. };
  409. module.exports.assignAscendingModuleIds = assignAscendingModuleIds;
  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;