IdHelpers.js 13 KB

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