IdHelpers.js 13 KB

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