ChunkGraph.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const Entrypoint = require("./Entrypoint");
  8. const ModuleGraphConnection = require("./ModuleGraphConnection");
  9. const { DEFAULTS } = require("./config/defaults");
  10. const { first } = require("./util/SetHelpers");
  11. const SortableSet = require("./util/SortableSet");
  12. const {
  13. compareModulesById,
  14. compareIterables,
  15. compareModulesByIdentifier,
  16. concatComparators,
  17. compareSelect,
  18. compareIds
  19. } = require("./util/comparators");
  20. const createHash = require("./util/createHash");
  21. const findGraphRoots = require("./util/findGraphRoots");
  22. const {
  23. RuntimeSpecMap,
  24. RuntimeSpecSet,
  25. runtimeToString,
  26. mergeRuntime,
  27. forEachRuntime
  28. } = require("./util/runtime");
  29. /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
  30. /** @typedef {import("./Chunk")} Chunk */
  31. /** @typedef {import("./Chunk").ChunkId} ChunkId */
  32. /** @typedef {import("./ChunkGroup")} ChunkGroup */
  33. /** @typedef {import("./Generator").SourceTypes} SourceTypes */
  34. /** @typedef {import("./Module")} Module */
  35. /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
  36. /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
  37. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  38. /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
  39. /** @typedef {import("./RuntimeModule")} RuntimeModule */
  40. /** @typedef {typeof import("./util/Hash")} Hash */
  41. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  42. /** @type {ReadonlySet<string>} */
  43. const EMPTY_SET = new Set();
  44. const ZERO_BIG_INT = BigInt(0);
  45. const compareModuleIterables = compareIterables(compareModulesByIdentifier);
  46. /** @typedef {(c: Chunk, chunkGraph: ChunkGraph) => boolean} ChunkFilterPredicate */
  47. /** @typedef {(m: Module) => boolean} ModuleFilterPredicate */
  48. /** @typedef {[Module, Entrypoint | undefined]} EntryModuleWithChunkGroup */
  49. /**
  50. * @typedef {object} ChunkSizeOptions
  51. * @property {number=} chunkOverhead constant overhead for a chunk
  52. * @property {number=} entryChunkMultiplicator multiplicator for initial chunks
  53. */
  54. class ModuleHashInfo {
  55. /**
  56. * @param {string} hash hash
  57. * @param {string} renderedHash rendered hash
  58. */
  59. constructor(hash, renderedHash) {
  60. this.hash = hash;
  61. this.renderedHash = renderedHash;
  62. }
  63. }
  64. /**
  65. * @template T
  66. * @param {SortableSet<T>} set the set
  67. * @returns {T[]} set as array
  68. */
  69. const getArray = set => Array.from(set);
  70. /**
  71. * @param {SortableSet<Chunk>} chunks the chunks
  72. * @returns {RuntimeSpecSet} runtimes
  73. */
  74. const getModuleRuntimes = chunks => {
  75. const runtimes = new RuntimeSpecSet();
  76. for (const chunk of chunks) {
  77. runtimes.add(chunk.runtime);
  78. }
  79. return runtimes;
  80. };
  81. /**
  82. * @param {WeakMap<Module, Set<string>> | undefined} sourceTypesByModule sourceTypesByModule
  83. * @returns {(set: SortableSet<Module>) => Map<string, SortableSet<Module>>} modules by source type
  84. */
  85. const modulesBySourceType = sourceTypesByModule => set => {
  86. /** @type {Map<string, SortableSet<Module>>} */
  87. const map = new Map();
  88. for (const module of set) {
  89. const sourceTypes =
  90. (sourceTypesByModule && sourceTypesByModule.get(module)) ||
  91. module.getSourceTypes();
  92. for (const sourceType of sourceTypes) {
  93. let innerSet = map.get(sourceType);
  94. if (innerSet === undefined) {
  95. innerSet = new SortableSet();
  96. map.set(sourceType, innerSet);
  97. }
  98. innerSet.add(module);
  99. }
  100. }
  101. for (const [key, innerSet] of map) {
  102. // When all modules have the source type, we reuse the original SortableSet
  103. // to benefit from the shared cache (especially for sorting)
  104. if (innerSet.size === set.size) {
  105. map.set(key, set);
  106. }
  107. }
  108. return map;
  109. };
  110. const defaultModulesBySourceType = modulesBySourceType(undefined);
  111. /**
  112. * @typedef {(set: SortableSet<Module>) => Module[]} ModuleSetToArrayFunction
  113. */
  114. /**
  115. * @template T
  116. * @type {WeakMap<ModuleComparator, ModuleSetToArrayFunction>}
  117. */
  118. const createOrderedArrayFunctionMap = new WeakMap();
  119. /**
  120. * @template T
  121. * @param {ModuleComparator} comparator comparator function
  122. * @returns {ModuleSetToArrayFunction} set as ordered array
  123. */
  124. const createOrderedArrayFunction = comparator => {
  125. let fn = createOrderedArrayFunctionMap.get(comparator);
  126. if (fn !== undefined) return fn;
  127. fn = set => {
  128. set.sortWith(comparator);
  129. return Array.from(set);
  130. };
  131. createOrderedArrayFunctionMap.set(comparator, fn);
  132. return fn;
  133. };
  134. /**
  135. * @param {Iterable<Module>} modules the modules to get the count/size of
  136. * @returns {number} the size of the modules
  137. */
  138. const getModulesSize = modules => {
  139. let size = 0;
  140. for (const module of modules) {
  141. for (const type of module.getSourceTypes()) {
  142. size += module.size(type);
  143. }
  144. }
  145. return size;
  146. };
  147. /**
  148. * @param {Iterable<Module>} modules the sortable Set to get the size of
  149. * @returns {Record<string, number>} the sizes of the modules
  150. */
  151. const getModulesSizes = modules => {
  152. const sizes = Object.create(null);
  153. for (const module of modules) {
  154. for (const type of module.getSourceTypes()) {
  155. sizes[type] = (sizes[type] || 0) + module.size(type);
  156. }
  157. }
  158. return sizes;
  159. };
  160. /**
  161. * @param {Chunk} a chunk
  162. * @param {Chunk} b chunk
  163. * @returns {boolean} true, if a is always a parent of b
  164. */
  165. const isAvailableChunk = (a, b) => {
  166. const queue = new Set(b.groupsIterable);
  167. for (const chunkGroup of queue) {
  168. if (a.isInGroup(chunkGroup)) continue;
  169. if (chunkGroup.isInitial()) return false;
  170. for (const parent of chunkGroup.parentsIterable) {
  171. queue.add(parent);
  172. }
  173. }
  174. return true;
  175. };
  176. /** @typedef {Set<Chunk>} EntryInChunks */
  177. /** @typedef {Set<Chunk>} RuntimeInChunks */
  178. /** @typedef {string | number} ModuleId */
  179. class ChunkGraphModule {
  180. constructor() {
  181. /** @type {SortableSet<Chunk>} */
  182. this.chunks = new SortableSet();
  183. /** @type {EntryInChunks | undefined} */
  184. this.entryInChunks = undefined;
  185. /** @type {RuntimeInChunks | undefined} */
  186. this.runtimeInChunks = undefined;
  187. /** @type {RuntimeSpecMap<ModuleHashInfo> | undefined} */
  188. this.hashes = undefined;
  189. /** @type {ModuleId | null} */
  190. this.id = null;
  191. /** @type {RuntimeSpecMap<Set<string>, RuntimeRequirements> | undefined} */
  192. this.runtimeRequirements = undefined;
  193. /** @type {RuntimeSpecMap<string, bigint> | undefined} */
  194. this.graphHashes = undefined;
  195. /** @type {RuntimeSpecMap<string, string> | undefined} */
  196. this.graphHashesWithConnections = undefined;
  197. }
  198. }
  199. class ChunkGraphChunk {
  200. constructor() {
  201. /** @type {SortableSet<Module>} */
  202. this.modules = new SortableSet();
  203. /** @type {WeakMap<Module, Set<string>> | undefined} */
  204. this.sourceTypesByModule = undefined;
  205. /** @type {Map<Module, Entrypoint>} */
  206. this.entryModules = new Map();
  207. /** @type {SortableSet<RuntimeModule>} */
  208. this.runtimeModules = new SortableSet();
  209. /** @type {Set<RuntimeModule> | undefined} */
  210. this.fullHashModules = undefined;
  211. /** @type {Set<RuntimeModule> | undefined} */
  212. this.dependentHashModules = undefined;
  213. /** @type {Set<string> | undefined} */
  214. this.runtimeRequirements = undefined;
  215. /** @type {Set<string>} */
  216. this.runtimeRequirementsInTree = new Set();
  217. this._modulesBySourceType = defaultModulesBySourceType;
  218. }
  219. }
  220. /** @typedef {(a: Module, b: Module) => -1 | 0 | 1} ModuleComparator */
  221. class ChunkGraph {
  222. /**
  223. * @param {ModuleGraph} moduleGraph the module graph
  224. * @param {string | Hash} hashFunction the hash function to use
  225. */
  226. constructor(moduleGraph, hashFunction = DEFAULTS.HASH_FUNCTION) {
  227. /**
  228. * @private
  229. * @type {WeakMap<Module, ChunkGraphModule>}
  230. */
  231. this._modules = new WeakMap();
  232. /**
  233. * @private
  234. * @type {WeakMap<Chunk, ChunkGraphChunk>}
  235. */
  236. this._chunks = new WeakMap();
  237. /**
  238. * @private
  239. * @type {WeakMap<AsyncDependenciesBlock, ChunkGroup>}
  240. */
  241. this._blockChunkGroups = new WeakMap();
  242. /**
  243. * @private
  244. * @type {Map<string, string | number>}
  245. */
  246. this._runtimeIds = new Map();
  247. /** @type {ModuleGraph} */
  248. this.moduleGraph = moduleGraph;
  249. this._hashFunction = hashFunction;
  250. this._getGraphRoots = this._getGraphRoots.bind(this);
  251. }
  252. /**
  253. * @private
  254. * @param {Module} module the module
  255. * @returns {ChunkGraphModule} internal module
  256. */
  257. _getChunkGraphModule(module) {
  258. let cgm = this._modules.get(module);
  259. if (cgm === undefined) {
  260. cgm = new ChunkGraphModule();
  261. this._modules.set(module, cgm);
  262. }
  263. return cgm;
  264. }
  265. /**
  266. * @private
  267. * @param {Chunk} chunk the chunk
  268. * @returns {ChunkGraphChunk} internal chunk
  269. */
  270. _getChunkGraphChunk(chunk) {
  271. let cgc = this._chunks.get(chunk);
  272. if (cgc === undefined) {
  273. cgc = new ChunkGraphChunk();
  274. this._chunks.set(chunk, cgc);
  275. }
  276. return cgc;
  277. }
  278. /**
  279. * @param {SortableSet<Module>} set the sortable Set to get the roots of
  280. * @returns {Module[]} the graph roots
  281. */
  282. _getGraphRoots(set) {
  283. const { moduleGraph } = this;
  284. return Array.from(
  285. findGraphRoots(set, module => {
  286. /** @type {Set<Module>} */
  287. const set = new Set();
  288. /**
  289. * @param {Module} module module
  290. */
  291. const addDependencies = module => {
  292. for (const connection of moduleGraph.getOutgoingConnections(module)) {
  293. if (!connection.module) continue;
  294. const activeState = connection.getActiveState(undefined);
  295. if (activeState === false) continue;
  296. if (activeState === ModuleGraphConnection.TRANSITIVE_ONLY) {
  297. addDependencies(connection.module);
  298. continue;
  299. }
  300. set.add(connection.module);
  301. }
  302. };
  303. addDependencies(module);
  304. return set;
  305. })
  306. ).sort(compareModulesByIdentifier);
  307. }
  308. /**
  309. * @param {Chunk} chunk the new chunk
  310. * @param {Module} module the module
  311. * @returns {void}
  312. */
  313. connectChunkAndModule(chunk, module) {
  314. const cgm = this._getChunkGraphModule(module);
  315. const cgc = this._getChunkGraphChunk(chunk);
  316. cgm.chunks.add(chunk);
  317. cgc.modules.add(module);
  318. }
  319. /**
  320. * @param {Chunk} chunk the chunk
  321. * @param {Module} module the module
  322. * @returns {void}
  323. */
  324. disconnectChunkAndModule(chunk, module) {
  325. const cgm = this._getChunkGraphModule(module);
  326. const cgc = this._getChunkGraphChunk(chunk);
  327. cgc.modules.delete(module);
  328. // No need to invalidate cgc._modulesBySourceType because we modified cgc.modules anyway
  329. if (cgc.sourceTypesByModule) cgc.sourceTypesByModule.delete(module);
  330. cgm.chunks.delete(chunk);
  331. }
  332. /**
  333. * @param {Chunk} chunk the chunk which will be disconnected
  334. * @returns {void}
  335. */
  336. disconnectChunk(chunk) {
  337. const cgc = this._getChunkGraphChunk(chunk);
  338. for (const module of cgc.modules) {
  339. const cgm = this._getChunkGraphModule(module);
  340. cgm.chunks.delete(chunk);
  341. }
  342. cgc.modules.clear();
  343. chunk.disconnectFromGroups();
  344. ChunkGraph.clearChunkGraphForChunk(chunk);
  345. }
  346. /**
  347. * @param {Chunk} chunk the chunk
  348. * @param {Iterable<Module>} modules the modules
  349. * @returns {void}
  350. */
  351. attachModules(chunk, modules) {
  352. const cgc = this._getChunkGraphChunk(chunk);
  353. for (const module of modules) {
  354. cgc.modules.add(module);
  355. }
  356. }
  357. /**
  358. * @param {Chunk} chunk the chunk
  359. * @param {Iterable<RuntimeModule>} modules the runtime modules
  360. * @returns {void}
  361. */
  362. attachRuntimeModules(chunk, modules) {
  363. const cgc = this._getChunkGraphChunk(chunk);
  364. for (const module of modules) {
  365. cgc.runtimeModules.add(module);
  366. }
  367. }
  368. /**
  369. * @param {Chunk} chunk the chunk
  370. * @param {Iterable<RuntimeModule>} modules the modules that require a full hash
  371. * @returns {void}
  372. */
  373. attachFullHashModules(chunk, modules) {
  374. const cgc = this._getChunkGraphChunk(chunk);
  375. if (cgc.fullHashModules === undefined) cgc.fullHashModules = new Set();
  376. for (const module of modules) {
  377. cgc.fullHashModules.add(module);
  378. }
  379. }
  380. /**
  381. * @param {Chunk} chunk the chunk
  382. * @param {Iterable<RuntimeModule>} modules the modules that require a full hash
  383. * @returns {void}
  384. */
  385. attachDependentHashModules(chunk, modules) {
  386. const cgc = this._getChunkGraphChunk(chunk);
  387. if (cgc.dependentHashModules === undefined)
  388. cgc.dependentHashModules = new Set();
  389. for (const module of modules) {
  390. cgc.dependentHashModules.add(module);
  391. }
  392. }
  393. /**
  394. * @param {Module} oldModule the replaced module
  395. * @param {Module} newModule the replacing module
  396. * @returns {void}
  397. */
  398. replaceModule(oldModule, newModule) {
  399. const oldCgm = this._getChunkGraphModule(oldModule);
  400. const newCgm = this._getChunkGraphModule(newModule);
  401. for (const chunk of oldCgm.chunks) {
  402. const cgc = this._getChunkGraphChunk(chunk);
  403. cgc.modules.delete(oldModule);
  404. cgc.modules.add(newModule);
  405. newCgm.chunks.add(chunk);
  406. }
  407. oldCgm.chunks.clear();
  408. if (oldCgm.entryInChunks !== undefined) {
  409. if (newCgm.entryInChunks === undefined) {
  410. newCgm.entryInChunks = new Set();
  411. }
  412. for (const chunk of oldCgm.entryInChunks) {
  413. const cgc = this._getChunkGraphChunk(chunk);
  414. const old = /** @type {Entrypoint} */ (cgc.entryModules.get(oldModule));
  415. /** @type {Map<Module, Entrypoint>} */
  416. const newEntryModules = new Map();
  417. for (const [m, cg] of cgc.entryModules) {
  418. if (m === oldModule) {
  419. newEntryModules.set(newModule, old);
  420. } else {
  421. newEntryModules.set(m, cg);
  422. }
  423. }
  424. cgc.entryModules = newEntryModules;
  425. newCgm.entryInChunks.add(chunk);
  426. }
  427. oldCgm.entryInChunks = undefined;
  428. }
  429. if (oldCgm.runtimeInChunks !== undefined) {
  430. if (newCgm.runtimeInChunks === undefined) {
  431. newCgm.runtimeInChunks = new Set();
  432. }
  433. for (const chunk of oldCgm.runtimeInChunks) {
  434. const cgc = this._getChunkGraphChunk(chunk);
  435. cgc.runtimeModules.delete(/** @type {RuntimeModule} */ (oldModule));
  436. cgc.runtimeModules.add(/** @type {RuntimeModule} */ (newModule));
  437. newCgm.runtimeInChunks.add(chunk);
  438. if (
  439. cgc.fullHashModules !== undefined &&
  440. cgc.fullHashModules.has(/** @type {RuntimeModule} */ (oldModule))
  441. ) {
  442. cgc.fullHashModules.delete(/** @type {RuntimeModule} */ (oldModule));
  443. cgc.fullHashModules.add(/** @type {RuntimeModule} */ (newModule));
  444. }
  445. if (
  446. cgc.dependentHashModules !== undefined &&
  447. cgc.dependentHashModules.has(/** @type {RuntimeModule} */ (oldModule))
  448. ) {
  449. cgc.dependentHashModules.delete(
  450. /** @type {RuntimeModule} */ (oldModule)
  451. );
  452. cgc.dependentHashModules.add(
  453. /** @type {RuntimeModule} */ (newModule)
  454. );
  455. }
  456. }
  457. oldCgm.runtimeInChunks = undefined;
  458. }
  459. }
  460. /**
  461. * @param {Module} module the checked module
  462. * @param {Chunk} chunk the checked chunk
  463. * @returns {boolean} true, if the chunk contains the module
  464. */
  465. isModuleInChunk(module, chunk) {
  466. const cgc = this._getChunkGraphChunk(chunk);
  467. return cgc.modules.has(module);
  468. }
  469. /**
  470. * @param {Module} module the checked module
  471. * @param {ChunkGroup} chunkGroup the checked chunk group
  472. * @returns {boolean} true, if the chunk contains the module
  473. */
  474. isModuleInChunkGroup(module, chunkGroup) {
  475. for (const chunk of chunkGroup.chunks) {
  476. if (this.isModuleInChunk(module, chunk)) return true;
  477. }
  478. return false;
  479. }
  480. /**
  481. * @param {Module} module the checked module
  482. * @returns {boolean} true, if the module is entry of any chunk
  483. */
  484. isEntryModule(module) {
  485. const cgm = this._getChunkGraphModule(module);
  486. return cgm.entryInChunks !== undefined;
  487. }
  488. /**
  489. * @param {Module} module the module
  490. * @returns {Iterable<Chunk>} iterable of chunks (do not modify)
  491. */
  492. getModuleChunksIterable(module) {
  493. const cgm = this._getChunkGraphModule(module);
  494. return cgm.chunks;
  495. }
  496. /**
  497. * @param {Module} module the module
  498. * @param {(a: Chunk, b: Chunk) => -1 | 0 | 1} sortFn sort function
  499. * @returns {Iterable<Chunk>} iterable of chunks (do not modify)
  500. */
  501. getOrderedModuleChunksIterable(module, sortFn) {
  502. const cgm = this._getChunkGraphModule(module);
  503. cgm.chunks.sortWith(sortFn);
  504. return cgm.chunks;
  505. }
  506. /**
  507. * @param {Module} module the module
  508. * @returns {Chunk[]} array of chunks (cached, do not modify)
  509. */
  510. getModuleChunks(module) {
  511. const cgm = this._getChunkGraphModule(module);
  512. return cgm.chunks.getFromCache(getArray);
  513. }
  514. /**
  515. * @param {Module} module the module
  516. * @returns {number} the number of chunk which contain the module
  517. */
  518. getNumberOfModuleChunks(module) {
  519. const cgm = this._getChunkGraphModule(module);
  520. return cgm.chunks.size;
  521. }
  522. /**
  523. * @param {Module} module the module
  524. * @returns {RuntimeSpecSet} runtimes
  525. */
  526. getModuleRuntimes(module) {
  527. const cgm = this._getChunkGraphModule(module);
  528. return cgm.chunks.getFromUnorderedCache(getModuleRuntimes);
  529. }
  530. /**
  531. * @param {Chunk} chunk the chunk
  532. * @returns {number} the number of modules which are contained in this chunk
  533. */
  534. getNumberOfChunkModules(chunk) {
  535. const cgc = this._getChunkGraphChunk(chunk);
  536. return cgc.modules.size;
  537. }
  538. /**
  539. * @param {Chunk} chunk the chunk
  540. * @returns {number} the number of full hash modules which are contained in this chunk
  541. */
  542. getNumberOfChunkFullHashModules(chunk) {
  543. const cgc = this._getChunkGraphChunk(chunk);
  544. return cgc.fullHashModules === undefined ? 0 : cgc.fullHashModules.size;
  545. }
  546. /**
  547. * @param {Chunk} chunk the chunk
  548. * @returns {Iterable<Module>} return the modules for this chunk
  549. */
  550. getChunkModulesIterable(chunk) {
  551. const cgc = this._getChunkGraphChunk(chunk);
  552. return cgc.modules;
  553. }
  554. /**
  555. * @param {Chunk} chunk the chunk
  556. * @param {string} sourceType source type
  557. * @returns {Iterable<Module> | undefined} return the modules for this chunk
  558. */
  559. getChunkModulesIterableBySourceType(chunk, sourceType) {
  560. const cgc = this._getChunkGraphChunk(chunk);
  561. const modulesWithSourceType = cgc.modules
  562. .getFromUnorderedCache(cgc._modulesBySourceType)
  563. .get(sourceType);
  564. return modulesWithSourceType;
  565. }
  566. /**
  567. * @param {Chunk} chunk chunk
  568. * @param {Module} module chunk module
  569. * @param {Set<string>} sourceTypes source types
  570. */
  571. setChunkModuleSourceTypes(chunk, module, sourceTypes) {
  572. const cgc = this._getChunkGraphChunk(chunk);
  573. if (cgc.sourceTypesByModule === undefined) {
  574. cgc.sourceTypesByModule = new WeakMap();
  575. }
  576. cgc.sourceTypesByModule.set(module, sourceTypes);
  577. // Update cgc._modulesBySourceType to invalidate the cache
  578. cgc._modulesBySourceType = modulesBySourceType(cgc.sourceTypesByModule);
  579. }
  580. /**
  581. * @param {Chunk} chunk chunk
  582. * @param {Module} module chunk module
  583. * @returns {SourceTypes} source types
  584. */
  585. getChunkModuleSourceTypes(chunk, module) {
  586. const cgc = this._getChunkGraphChunk(chunk);
  587. if (cgc.sourceTypesByModule === undefined) {
  588. return module.getSourceTypes();
  589. }
  590. return cgc.sourceTypesByModule.get(module) || module.getSourceTypes();
  591. }
  592. /**
  593. * @param {Module} module module
  594. * @returns {SourceTypes} source types
  595. */
  596. getModuleSourceTypes(module) {
  597. return (
  598. this._getOverwrittenModuleSourceTypes(module) || module.getSourceTypes()
  599. );
  600. }
  601. /**
  602. * @param {Module} module module
  603. * @returns {Set<string> | undefined} source types
  604. */
  605. _getOverwrittenModuleSourceTypes(module) {
  606. let newSet = false;
  607. let sourceTypes;
  608. for (const chunk of this.getModuleChunksIterable(module)) {
  609. const cgc = this._getChunkGraphChunk(chunk);
  610. if (cgc.sourceTypesByModule === undefined) return;
  611. const st = cgc.sourceTypesByModule.get(module);
  612. if (st === undefined) return;
  613. if (!sourceTypes) {
  614. sourceTypes = st;
  615. continue;
  616. } else if (!newSet) {
  617. for (const type of st) {
  618. if (!newSet) {
  619. if (!sourceTypes.has(type)) {
  620. newSet = true;
  621. sourceTypes = new Set(sourceTypes);
  622. sourceTypes.add(type);
  623. }
  624. } else {
  625. sourceTypes.add(type);
  626. }
  627. }
  628. } else {
  629. for (const type of st) sourceTypes.add(type);
  630. }
  631. }
  632. return sourceTypes;
  633. }
  634. /**
  635. * @param {Chunk} chunk the chunk
  636. * @param {ModuleComparator} comparator comparator function
  637. * @returns {Iterable<Module>} return the modules for this chunk
  638. */
  639. getOrderedChunkModulesIterable(chunk, comparator) {
  640. const cgc = this._getChunkGraphChunk(chunk);
  641. cgc.modules.sortWith(comparator);
  642. return cgc.modules;
  643. }
  644. /**
  645. * @param {Chunk} chunk the chunk
  646. * @param {string} sourceType source type
  647. * @param {ModuleComparator} comparator comparator function
  648. * @returns {Iterable<Module> | undefined} return the modules for this chunk
  649. */
  650. getOrderedChunkModulesIterableBySourceType(chunk, sourceType, comparator) {
  651. const cgc = this._getChunkGraphChunk(chunk);
  652. const modulesWithSourceType = cgc.modules
  653. .getFromUnorderedCache(cgc._modulesBySourceType)
  654. .get(sourceType);
  655. if (modulesWithSourceType === undefined) return;
  656. modulesWithSourceType.sortWith(comparator);
  657. return modulesWithSourceType;
  658. }
  659. /**
  660. * @param {Chunk} chunk the chunk
  661. * @returns {Module[]} return the modules for this chunk (cached, do not modify)
  662. */
  663. getChunkModules(chunk) {
  664. const cgc = this._getChunkGraphChunk(chunk);
  665. return cgc.modules.getFromUnorderedCache(getArray);
  666. }
  667. /**
  668. * @param {Chunk} chunk the chunk
  669. * @param {ModuleComparator} comparator comparator function
  670. * @returns {Module[]} return the modules for this chunk (cached, do not modify)
  671. */
  672. getOrderedChunkModules(chunk, comparator) {
  673. const cgc = this._getChunkGraphChunk(chunk);
  674. const arrayFunction = createOrderedArrayFunction(comparator);
  675. return cgc.modules.getFromUnorderedCache(arrayFunction);
  676. }
  677. /**
  678. * @param {Chunk} chunk the chunk
  679. * @param {ModuleFilterPredicate} filterFn function used to filter modules
  680. * @param {boolean} includeAllChunks all chunks or only async chunks
  681. * @returns {Record<string|number, (string|number)[]>} chunk to module ids object
  682. */
  683. getChunkModuleIdMap(chunk, filterFn, includeAllChunks = false) {
  684. /** @type {Record<string|number, (string|number)[]>} */
  685. const chunkModuleIdMap = Object.create(null);
  686. for (const asyncChunk of includeAllChunks
  687. ? chunk.getAllReferencedChunks()
  688. : chunk.getAllAsyncChunks()) {
  689. /** @type {(string | number)[] | undefined} */
  690. let array;
  691. for (const module of this.getOrderedChunkModulesIterable(
  692. asyncChunk,
  693. compareModulesById(this)
  694. )) {
  695. if (filterFn(module)) {
  696. if (array === undefined) {
  697. array = [];
  698. chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array;
  699. }
  700. const moduleId = /** @type {ModuleId} */ (this.getModuleId(module));
  701. array.push(moduleId);
  702. }
  703. }
  704. }
  705. return chunkModuleIdMap;
  706. }
  707. /**
  708. * @param {Chunk} chunk the chunk
  709. * @param {ModuleFilterPredicate} filterFn function used to filter modules
  710. * @param {number} hashLength length of the hash
  711. * @param {boolean} includeAllChunks all chunks or only async chunks
  712. * @returns {Record<string|number, Record<string|number, string>>} chunk to module id to module hash object
  713. */
  714. getChunkModuleRenderedHashMap(
  715. chunk,
  716. filterFn,
  717. hashLength = 0,
  718. includeAllChunks = false
  719. ) {
  720. /** @type {Record<ChunkId, Record<string|number, string>>} */
  721. const chunkModuleHashMap = Object.create(null);
  722. /** @typedef {Record<string|number, string>} IdToHashMap */
  723. for (const asyncChunk of includeAllChunks
  724. ? chunk.getAllReferencedChunks()
  725. : chunk.getAllAsyncChunks()) {
  726. /** @type {IdToHashMap | undefined} */
  727. let idToHashMap;
  728. for (const module of this.getOrderedChunkModulesIterable(
  729. asyncChunk,
  730. compareModulesById(this)
  731. )) {
  732. if (filterFn(module)) {
  733. if (idToHashMap === undefined) {
  734. idToHashMap = Object.create(null);
  735. chunkModuleHashMap[/** @type {ChunkId} */ (asyncChunk.id)] =
  736. /** @type {IdToHashMap} */ (idToHashMap);
  737. }
  738. const moduleId = this.getModuleId(module);
  739. const hash = this.getRenderedModuleHash(module, asyncChunk.runtime);
  740. /** @type {IdToHashMap} */
  741. (idToHashMap)[/** @type {ModuleId} */ (moduleId)] = hashLength
  742. ? hash.slice(0, hashLength)
  743. : hash;
  744. }
  745. }
  746. }
  747. return chunkModuleHashMap;
  748. }
  749. /**
  750. * @param {Chunk} chunk the chunk
  751. * @param {ChunkFilterPredicate} filterFn function used to filter chunks
  752. * @returns {Record<string|number, boolean>} chunk map
  753. */
  754. getChunkConditionMap(chunk, filterFn) {
  755. const map = Object.create(null);
  756. for (const c of chunk.getAllReferencedChunks()) {
  757. map[/** @type {ChunkId} */ (c.id)] = filterFn(c, this);
  758. }
  759. return map;
  760. }
  761. /**
  762. * @param {Chunk} chunk the chunk
  763. * @param {ModuleFilterPredicate} filterFn predicate function used to filter modules
  764. * @param {ChunkFilterPredicate=} filterChunkFn predicate function used to filter chunks
  765. * @returns {boolean} return true if module exists in graph
  766. */
  767. hasModuleInGraph(chunk, filterFn, filterChunkFn) {
  768. const queue = new Set(chunk.groupsIterable);
  769. const chunksProcessed = new Set();
  770. for (const chunkGroup of queue) {
  771. for (const innerChunk of chunkGroup.chunks) {
  772. if (!chunksProcessed.has(innerChunk)) {
  773. chunksProcessed.add(innerChunk);
  774. if (!filterChunkFn || filterChunkFn(innerChunk, this)) {
  775. for (const module of this.getChunkModulesIterable(innerChunk)) {
  776. if (filterFn(module)) {
  777. return true;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. for (const child of chunkGroup.childrenIterable) {
  784. queue.add(child);
  785. }
  786. }
  787. return false;
  788. }
  789. /**
  790. * @param {Chunk} chunkA first chunk
  791. * @param {Chunk} chunkB second chunk
  792. * @returns {-1|0|1} this is a comparator function like sort and returns -1, 0, or 1 based on sort order
  793. */
  794. compareChunks(chunkA, chunkB) {
  795. const cgcA = this._getChunkGraphChunk(chunkA);
  796. const cgcB = this._getChunkGraphChunk(chunkB);
  797. if (cgcA.modules.size > cgcB.modules.size) return -1;
  798. if (cgcA.modules.size < cgcB.modules.size) return 1;
  799. cgcA.modules.sortWith(compareModulesByIdentifier);
  800. cgcB.modules.sortWith(compareModulesByIdentifier);
  801. return compareModuleIterables(cgcA.modules, cgcB.modules);
  802. }
  803. /**
  804. * @param {Chunk} chunk the chunk
  805. * @returns {number} total size of all modules in the chunk
  806. */
  807. getChunkModulesSize(chunk) {
  808. const cgc = this._getChunkGraphChunk(chunk);
  809. return cgc.modules.getFromUnorderedCache(getModulesSize);
  810. }
  811. /**
  812. * @param {Chunk} chunk the chunk
  813. * @returns {Record<string, number>} total sizes of all modules in the chunk by source type
  814. */
  815. getChunkModulesSizes(chunk) {
  816. const cgc = this._getChunkGraphChunk(chunk);
  817. return cgc.modules.getFromUnorderedCache(getModulesSizes);
  818. }
  819. /**
  820. * @param {Chunk} chunk the chunk
  821. * @returns {Module[]} root modules of the chunks (ordered by identifier)
  822. */
  823. getChunkRootModules(chunk) {
  824. const cgc = this._getChunkGraphChunk(chunk);
  825. return cgc.modules.getFromUnorderedCache(this._getGraphRoots);
  826. }
  827. /**
  828. * @param {Chunk} chunk the chunk
  829. * @param {ChunkSizeOptions} options options object
  830. * @returns {number} total size of the chunk
  831. */
  832. getChunkSize(chunk, options = {}) {
  833. const cgc = this._getChunkGraphChunk(chunk);
  834. const modulesSize = cgc.modules.getFromUnorderedCache(getModulesSize);
  835. const chunkOverhead =
  836. typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
  837. const entryChunkMultiplicator =
  838. typeof options.entryChunkMultiplicator === "number"
  839. ? options.entryChunkMultiplicator
  840. : 10;
  841. return (
  842. chunkOverhead +
  843. modulesSize * (chunk.canBeInitial() ? entryChunkMultiplicator : 1)
  844. );
  845. }
  846. /**
  847. * @param {Chunk} chunkA chunk
  848. * @param {Chunk} chunkB chunk
  849. * @param {ChunkSizeOptions} options options object
  850. * @returns {number} total size of the chunk or false if chunks can't be integrated
  851. */
  852. getIntegratedChunksSize(chunkA, chunkB, options = {}) {
  853. const cgcA = this._getChunkGraphChunk(chunkA);
  854. const cgcB = this._getChunkGraphChunk(chunkB);
  855. const allModules = new Set(cgcA.modules);
  856. for (const m of cgcB.modules) allModules.add(m);
  857. const modulesSize = getModulesSize(allModules);
  858. const chunkOverhead =
  859. typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
  860. const entryChunkMultiplicator =
  861. typeof options.entryChunkMultiplicator === "number"
  862. ? options.entryChunkMultiplicator
  863. : 10;
  864. return (
  865. chunkOverhead +
  866. modulesSize *
  867. (chunkA.canBeInitial() || chunkB.canBeInitial()
  868. ? entryChunkMultiplicator
  869. : 1)
  870. );
  871. }
  872. /**
  873. * @param {Chunk} chunkA chunk
  874. * @param {Chunk} chunkB chunk
  875. * @returns {boolean} true, if chunks could be integrated
  876. */
  877. canChunksBeIntegrated(chunkA, chunkB) {
  878. if (chunkA.preventIntegration || chunkB.preventIntegration) {
  879. return false;
  880. }
  881. const hasRuntimeA = chunkA.hasRuntime();
  882. const hasRuntimeB = chunkB.hasRuntime();
  883. if (hasRuntimeA !== hasRuntimeB) {
  884. if (hasRuntimeA) {
  885. return isAvailableChunk(chunkA, chunkB);
  886. } else if (hasRuntimeB) {
  887. return isAvailableChunk(chunkB, chunkA);
  888. }
  889. return false;
  890. }
  891. if (
  892. this.getNumberOfEntryModules(chunkA) > 0 ||
  893. this.getNumberOfEntryModules(chunkB) > 0
  894. ) {
  895. return false;
  896. }
  897. return true;
  898. }
  899. /**
  900. * @param {Chunk} chunkA the target chunk
  901. * @param {Chunk} chunkB the chunk to integrate
  902. * @returns {void}
  903. */
  904. integrateChunks(chunkA, chunkB) {
  905. // Decide for one name (deterministic)
  906. if (chunkA.name && chunkB.name) {
  907. if (
  908. this.getNumberOfEntryModules(chunkA) > 0 ===
  909. this.getNumberOfEntryModules(chunkB) > 0
  910. ) {
  911. // When both chunks have entry modules or none have one, use
  912. // shortest name
  913. if (chunkA.name.length !== chunkB.name.length) {
  914. chunkA.name =
  915. chunkA.name.length < chunkB.name.length ? chunkA.name : chunkB.name;
  916. } else {
  917. chunkA.name = chunkA.name < chunkB.name ? chunkA.name : chunkB.name;
  918. }
  919. } else if (this.getNumberOfEntryModules(chunkB) > 0) {
  920. // Pick the name of the chunk with the entry module
  921. chunkA.name = chunkB.name;
  922. }
  923. } else if (chunkB.name) {
  924. chunkA.name = chunkB.name;
  925. }
  926. // Merge id name hints
  927. for (const hint of chunkB.idNameHints) {
  928. chunkA.idNameHints.add(hint);
  929. }
  930. // Merge runtime
  931. chunkA.runtime = mergeRuntime(chunkA.runtime, chunkB.runtime);
  932. // getChunkModules is used here to create a clone, because disconnectChunkAndModule modifies
  933. for (const module of this.getChunkModules(chunkB)) {
  934. this.disconnectChunkAndModule(chunkB, module);
  935. this.connectChunkAndModule(chunkA, module);
  936. }
  937. for (const [module, chunkGroup] of Array.from(
  938. this.getChunkEntryModulesWithChunkGroupIterable(chunkB)
  939. )) {
  940. this.disconnectChunkAndEntryModule(chunkB, module);
  941. this.connectChunkAndEntryModule(
  942. chunkA,
  943. module,
  944. /** @type {Entrypoint} */
  945. (chunkGroup)
  946. );
  947. }
  948. for (const chunkGroup of chunkB.groupsIterable) {
  949. chunkGroup.replaceChunk(chunkB, chunkA);
  950. chunkA.addGroup(chunkGroup);
  951. chunkB.removeGroup(chunkGroup);
  952. }
  953. ChunkGraph.clearChunkGraphForChunk(chunkB);
  954. }
  955. /**
  956. * @param {Chunk} chunk the chunk to upgrade
  957. * @returns {void}
  958. */
  959. upgradeDependentToFullHashModules(chunk) {
  960. const cgc = this._getChunkGraphChunk(chunk);
  961. if (cgc.dependentHashModules === undefined) return;
  962. if (cgc.fullHashModules === undefined) {
  963. cgc.fullHashModules = cgc.dependentHashModules;
  964. } else {
  965. for (const m of cgc.dependentHashModules) {
  966. cgc.fullHashModules.add(m);
  967. }
  968. cgc.dependentHashModules = undefined;
  969. }
  970. }
  971. /**
  972. * @param {Module} module the checked module
  973. * @param {Chunk} chunk the checked chunk
  974. * @returns {boolean} true, if the chunk contains the module as entry
  975. */
  976. isEntryModuleInChunk(module, chunk) {
  977. const cgc = this._getChunkGraphChunk(chunk);
  978. return cgc.entryModules.has(module);
  979. }
  980. /**
  981. * @param {Chunk} chunk the new chunk
  982. * @param {Module} module the entry module
  983. * @param {Entrypoint} entrypoint the chunk group which must be loaded before the module is executed
  984. * @returns {void}
  985. */
  986. connectChunkAndEntryModule(chunk, module, entrypoint) {
  987. const cgm = this._getChunkGraphModule(module);
  988. const cgc = this._getChunkGraphChunk(chunk);
  989. if (cgm.entryInChunks === undefined) {
  990. cgm.entryInChunks = new Set();
  991. }
  992. cgm.entryInChunks.add(chunk);
  993. cgc.entryModules.set(module, entrypoint);
  994. }
  995. /**
  996. * @param {Chunk} chunk the new chunk
  997. * @param {RuntimeModule} module the runtime module
  998. * @returns {void}
  999. */
  1000. connectChunkAndRuntimeModule(chunk, module) {
  1001. const cgm = this._getChunkGraphModule(module);
  1002. const cgc = this._getChunkGraphChunk(chunk);
  1003. if (cgm.runtimeInChunks === undefined) {
  1004. cgm.runtimeInChunks = new Set();
  1005. }
  1006. cgm.runtimeInChunks.add(chunk);
  1007. cgc.runtimeModules.add(module);
  1008. }
  1009. /**
  1010. * @param {Chunk} chunk the new chunk
  1011. * @param {RuntimeModule} module the module that require a full hash
  1012. * @returns {void}
  1013. */
  1014. addFullHashModuleToChunk(chunk, module) {
  1015. const cgc = this._getChunkGraphChunk(chunk);
  1016. if (cgc.fullHashModules === undefined) cgc.fullHashModules = new Set();
  1017. cgc.fullHashModules.add(module);
  1018. }
  1019. /**
  1020. * @param {Chunk} chunk the new chunk
  1021. * @param {RuntimeModule} module the module that require a full hash
  1022. * @returns {void}
  1023. */
  1024. addDependentHashModuleToChunk(chunk, module) {
  1025. const cgc = this._getChunkGraphChunk(chunk);
  1026. if (cgc.dependentHashModules === undefined)
  1027. cgc.dependentHashModules = new Set();
  1028. cgc.dependentHashModules.add(module);
  1029. }
  1030. /**
  1031. * @param {Chunk} chunk the new chunk
  1032. * @param {Module} module the entry module
  1033. * @returns {void}
  1034. */
  1035. disconnectChunkAndEntryModule(chunk, module) {
  1036. const cgm = this._getChunkGraphModule(module);
  1037. const cgc = this._getChunkGraphChunk(chunk);
  1038. /** @type {EntryInChunks} */
  1039. (cgm.entryInChunks).delete(chunk);
  1040. if (/** @type {EntryInChunks} */ (cgm.entryInChunks).size === 0) {
  1041. cgm.entryInChunks = undefined;
  1042. }
  1043. cgc.entryModules.delete(module);
  1044. }
  1045. /**
  1046. * @param {Chunk} chunk the new chunk
  1047. * @param {RuntimeModule} module the runtime module
  1048. * @returns {void}
  1049. */
  1050. disconnectChunkAndRuntimeModule(chunk, module) {
  1051. const cgm = this._getChunkGraphModule(module);
  1052. const cgc = this._getChunkGraphChunk(chunk);
  1053. /** @type {RuntimeInChunks} */
  1054. (cgm.runtimeInChunks).delete(chunk);
  1055. if (/** @type {RuntimeInChunks} */ (cgm.runtimeInChunks).size === 0) {
  1056. cgm.runtimeInChunks = undefined;
  1057. }
  1058. cgc.runtimeModules.delete(module);
  1059. }
  1060. /**
  1061. * @param {Module} module the entry module, it will no longer be entry
  1062. * @returns {void}
  1063. */
  1064. disconnectEntryModule(module) {
  1065. const cgm = this._getChunkGraphModule(module);
  1066. for (const chunk of /** @type {EntryInChunks} */ (cgm.entryInChunks)) {
  1067. const cgc = this._getChunkGraphChunk(chunk);
  1068. cgc.entryModules.delete(module);
  1069. }
  1070. cgm.entryInChunks = undefined;
  1071. }
  1072. /**
  1073. * @param {Chunk} chunk the chunk, for which all entries will be removed
  1074. * @returns {void}
  1075. */
  1076. disconnectEntries(chunk) {
  1077. const cgc = this._getChunkGraphChunk(chunk);
  1078. for (const module of cgc.entryModules.keys()) {
  1079. const cgm = this._getChunkGraphModule(module);
  1080. /** @type {EntryInChunks} */
  1081. (cgm.entryInChunks).delete(chunk);
  1082. if (/** @type {EntryInChunks} */ (cgm.entryInChunks).size === 0) {
  1083. cgm.entryInChunks = undefined;
  1084. }
  1085. }
  1086. cgc.entryModules.clear();
  1087. }
  1088. /**
  1089. * @param {Chunk} chunk the chunk
  1090. * @returns {number} the amount of entry modules in chunk
  1091. */
  1092. getNumberOfEntryModules(chunk) {
  1093. const cgc = this._getChunkGraphChunk(chunk);
  1094. return cgc.entryModules.size;
  1095. }
  1096. /**
  1097. * @param {Chunk} chunk the chunk
  1098. * @returns {number} the amount of entry modules in chunk
  1099. */
  1100. getNumberOfRuntimeModules(chunk) {
  1101. const cgc = this._getChunkGraphChunk(chunk);
  1102. return cgc.runtimeModules.size;
  1103. }
  1104. /**
  1105. * @param {Chunk} chunk the chunk
  1106. * @returns {Iterable<Module>} iterable of modules (do not modify)
  1107. */
  1108. getChunkEntryModulesIterable(chunk) {
  1109. const cgc = this._getChunkGraphChunk(chunk);
  1110. return cgc.entryModules.keys();
  1111. }
  1112. /**
  1113. * @param {Chunk} chunk the chunk
  1114. * @returns {Iterable<Chunk>} iterable of chunks
  1115. */
  1116. getChunkEntryDependentChunksIterable(chunk) {
  1117. /** @type {Set<Chunk>} */
  1118. const set = new Set();
  1119. for (const chunkGroup of chunk.groupsIterable) {
  1120. if (chunkGroup instanceof Entrypoint) {
  1121. const entrypointChunk = chunkGroup.getEntrypointChunk();
  1122. const cgc = this._getChunkGraphChunk(entrypointChunk);
  1123. for (const chunkGroup of cgc.entryModules.values()) {
  1124. for (const c of chunkGroup.chunks) {
  1125. if (c !== chunk && c !== entrypointChunk && !c.hasRuntime()) {
  1126. set.add(c);
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. return set;
  1133. }
  1134. /**
  1135. * @param {Chunk} chunk the chunk
  1136. * @returns {boolean} true, when it has dependent chunks
  1137. */
  1138. hasChunkEntryDependentChunks(chunk) {
  1139. const cgc = this._getChunkGraphChunk(chunk);
  1140. for (const chunkGroup of cgc.entryModules.values()) {
  1141. for (const c of chunkGroup.chunks) {
  1142. if (c !== chunk) {
  1143. return true;
  1144. }
  1145. }
  1146. }
  1147. return false;
  1148. }
  1149. /**
  1150. * @param {Chunk} chunk the chunk
  1151. * @returns {Iterable<RuntimeModule>} iterable of modules (do not modify)
  1152. */
  1153. getChunkRuntimeModulesIterable(chunk) {
  1154. const cgc = this._getChunkGraphChunk(chunk);
  1155. return cgc.runtimeModules;
  1156. }
  1157. /**
  1158. * @param {Chunk} chunk the chunk
  1159. * @returns {RuntimeModule[]} array of modules in order of execution
  1160. */
  1161. getChunkRuntimeModulesInOrder(chunk) {
  1162. const cgc = this._getChunkGraphChunk(chunk);
  1163. const array = Array.from(cgc.runtimeModules);
  1164. array.sort(
  1165. concatComparators(
  1166. compareSelect(r => /** @type {RuntimeModule} */ (r).stage, compareIds),
  1167. compareModulesByIdentifier
  1168. )
  1169. );
  1170. return array;
  1171. }
  1172. /**
  1173. * @param {Chunk} chunk the chunk
  1174. * @returns {Iterable<RuntimeModule> | undefined} iterable of modules (do not modify)
  1175. */
  1176. getChunkFullHashModulesIterable(chunk) {
  1177. const cgc = this._getChunkGraphChunk(chunk);
  1178. return cgc.fullHashModules;
  1179. }
  1180. /**
  1181. * @param {Chunk} chunk the chunk
  1182. * @returns {ReadonlySet<RuntimeModule> | undefined} set of modules (do not modify)
  1183. */
  1184. getChunkFullHashModulesSet(chunk) {
  1185. const cgc = this._getChunkGraphChunk(chunk);
  1186. return cgc.fullHashModules;
  1187. }
  1188. /**
  1189. * @param {Chunk} chunk the chunk
  1190. * @returns {Iterable<RuntimeModule> | undefined} iterable of modules (do not modify)
  1191. */
  1192. getChunkDependentHashModulesIterable(chunk) {
  1193. const cgc = this._getChunkGraphChunk(chunk);
  1194. return cgc.dependentHashModules;
  1195. }
  1196. /**
  1197. * @param {Chunk} chunk the chunk
  1198. * @returns {Iterable<EntryModuleWithChunkGroup>} iterable of modules (do not modify)
  1199. */
  1200. getChunkEntryModulesWithChunkGroupIterable(chunk) {
  1201. const cgc = this._getChunkGraphChunk(chunk);
  1202. return cgc.entryModules;
  1203. }
  1204. /**
  1205. * @param {AsyncDependenciesBlock} depBlock the async block
  1206. * @returns {ChunkGroup | undefined} the chunk group
  1207. */
  1208. getBlockChunkGroup(depBlock) {
  1209. return this._blockChunkGroups.get(depBlock);
  1210. }
  1211. /**
  1212. * @param {AsyncDependenciesBlock} depBlock the async block
  1213. * @param {ChunkGroup} chunkGroup the chunk group
  1214. * @returns {void}
  1215. */
  1216. connectBlockAndChunkGroup(depBlock, chunkGroup) {
  1217. this._blockChunkGroups.set(depBlock, chunkGroup);
  1218. chunkGroup.addBlock(depBlock);
  1219. }
  1220. /**
  1221. * @param {ChunkGroup} chunkGroup the chunk group
  1222. * @returns {void}
  1223. */
  1224. disconnectChunkGroup(chunkGroup) {
  1225. for (const block of chunkGroup.blocksIterable) {
  1226. this._blockChunkGroups.delete(block);
  1227. }
  1228. // TODO refactor by moving blocks list into ChunkGraph
  1229. chunkGroup._blocks.clear();
  1230. }
  1231. /**
  1232. * @param {Module} module the module
  1233. * @returns {ModuleId | null} the id of the module
  1234. */
  1235. getModuleId(module) {
  1236. const cgm = this._getChunkGraphModule(module);
  1237. return cgm.id;
  1238. }
  1239. /**
  1240. * @param {Module} module the module
  1241. * @param {ModuleId} id the id of the module
  1242. * @returns {void}
  1243. */
  1244. setModuleId(module, id) {
  1245. const cgm = this._getChunkGraphModule(module);
  1246. cgm.id = id;
  1247. }
  1248. /**
  1249. * @param {string} runtime runtime
  1250. * @returns {string | number} the id of the runtime
  1251. */
  1252. getRuntimeId(runtime) {
  1253. return /** @type {string | number} */ (this._runtimeIds.get(runtime));
  1254. }
  1255. /**
  1256. * @param {string} runtime runtime
  1257. * @param {string | number} id the id of the runtime
  1258. * @returns {void}
  1259. */
  1260. setRuntimeId(runtime, id) {
  1261. this._runtimeIds.set(runtime, id);
  1262. }
  1263. /**
  1264. * @template T
  1265. * @param {Module} module the module
  1266. * @param {RuntimeSpecMap<T>} hashes hashes data
  1267. * @param {RuntimeSpec} runtime the runtime
  1268. * @returns {T} hash
  1269. */
  1270. _getModuleHashInfo(module, hashes, runtime) {
  1271. if (!hashes) {
  1272. throw new Error(
  1273. `Module ${module.identifier()} has no hash info for runtime ${runtimeToString(
  1274. runtime
  1275. )} (hashes not set at all)`
  1276. );
  1277. } else if (runtime === undefined) {
  1278. const hashInfoItems = new Set(hashes.values());
  1279. if (hashInfoItems.size !== 1) {
  1280. throw new Error(
  1281. `No unique hash info entry for unspecified runtime for ${module.identifier()} (existing runtimes: ${Array.from(
  1282. hashes.keys(),
  1283. r => runtimeToString(r)
  1284. ).join(", ")}).
  1285. Caller might not support runtime-dependent code generation (opt-out via optimization.usedExports: "global").`
  1286. );
  1287. }
  1288. return /** @type {T} */ (first(hashInfoItems));
  1289. } else {
  1290. const hashInfo = hashes.get(runtime);
  1291. if (!hashInfo) {
  1292. throw new Error(
  1293. `Module ${module.identifier()} has no hash info for runtime ${runtimeToString(
  1294. runtime
  1295. )} (available runtimes ${Array.from(
  1296. hashes.keys(),
  1297. runtimeToString
  1298. ).join(", ")})`
  1299. );
  1300. }
  1301. return hashInfo;
  1302. }
  1303. }
  1304. /**
  1305. * @param {Module} module the module
  1306. * @param {RuntimeSpec} runtime the runtime
  1307. * @returns {boolean} true, if the module has hashes for this runtime
  1308. */
  1309. hasModuleHashes(module, runtime) {
  1310. const cgm = this._getChunkGraphModule(module);
  1311. const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
  1312. return hashes && hashes.has(runtime);
  1313. }
  1314. /**
  1315. * @param {Module} module the module
  1316. * @param {RuntimeSpec} runtime the runtime
  1317. * @returns {string} hash
  1318. */
  1319. getModuleHash(module, runtime) {
  1320. const cgm = this._getChunkGraphModule(module);
  1321. const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
  1322. return this._getModuleHashInfo(module, hashes, runtime).hash;
  1323. }
  1324. /**
  1325. * @param {Module} module the module
  1326. * @param {RuntimeSpec} runtime the runtime
  1327. * @returns {string} hash
  1328. */
  1329. getRenderedModuleHash(module, runtime) {
  1330. const cgm = this._getChunkGraphModule(module);
  1331. const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
  1332. return this._getModuleHashInfo(module, hashes, runtime).renderedHash;
  1333. }
  1334. /**
  1335. * @param {Module} module the module
  1336. * @param {RuntimeSpec} runtime the runtime
  1337. * @param {string} hash the full hash
  1338. * @param {string} renderedHash the shortened hash for rendering
  1339. * @returns {void}
  1340. */
  1341. setModuleHashes(module, runtime, hash, renderedHash) {
  1342. const cgm = this._getChunkGraphModule(module);
  1343. if (cgm.hashes === undefined) {
  1344. cgm.hashes = new RuntimeSpecMap();
  1345. }
  1346. cgm.hashes.set(runtime, new ModuleHashInfo(hash, renderedHash));
  1347. }
  1348. /**
  1349. * @param {Module} module the module
  1350. * @param {RuntimeSpec} runtime the runtime
  1351. * @param {Set<string>} items runtime requirements to be added (ownership of this Set is given to ChunkGraph when transferOwnership not false)
  1352. * @param {boolean} transferOwnership true: transfer ownership of the items object, false: items is immutable and shared and won't be modified
  1353. * @returns {void}
  1354. */
  1355. addModuleRuntimeRequirements(
  1356. module,
  1357. runtime,
  1358. items,
  1359. transferOwnership = true
  1360. ) {
  1361. const cgm = this._getChunkGraphModule(module);
  1362. const runtimeRequirementsMap = cgm.runtimeRequirements;
  1363. if (runtimeRequirementsMap === undefined) {
  1364. const map = new RuntimeSpecMap();
  1365. // TODO avoid cloning item and track ownership instead
  1366. map.set(runtime, transferOwnership ? items : new Set(items));
  1367. cgm.runtimeRequirements = map;
  1368. return;
  1369. }
  1370. runtimeRequirementsMap.update(runtime, runtimeRequirements => {
  1371. if (runtimeRequirements === undefined) {
  1372. return transferOwnership ? items : new Set(items);
  1373. } else if (!transferOwnership || runtimeRequirements.size >= items.size) {
  1374. for (const item of items) runtimeRequirements.add(item);
  1375. return runtimeRequirements;
  1376. }
  1377. for (const item of runtimeRequirements) items.add(item);
  1378. return items;
  1379. });
  1380. }
  1381. /**
  1382. * @param {Chunk} chunk the chunk
  1383. * @param {Set<string>} items runtime requirements to be added (ownership of this Set is given to ChunkGraph)
  1384. * @returns {void}
  1385. */
  1386. addChunkRuntimeRequirements(chunk, items) {
  1387. const cgc = this._getChunkGraphChunk(chunk);
  1388. const runtimeRequirements = cgc.runtimeRequirements;
  1389. if (runtimeRequirements === undefined) {
  1390. cgc.runtimeRequirements = items;
  1391. } else if (runtimeRequirements.size >= items.size) {
  1392. for (const item of items) runtimeRequirements.add(item);
  1393. } else {
  1394. for (const item of runtimeRequirements) items.add(item);
  1395. cgc.runtimeRequirements = items;
  1396. }
  1397. }
  1398. /**
  1399. * @param {Chunk} chunk the chunk
  1400. * @param {Iterable<string>} items runtime requirements to be added
  1401. * @returns {void}
  1402. */
  1403. addTreeRuntimeRequirements(chunk, items) {
  1404. const cgc = this._getChunkGraphChunk(chunk);
  1405. const runtimeRequirements = cgc.runtimeRequirementsInTree;
  1406. for (const item of items) runtimeRequirements.add(item);
  1407. }
  1408. /**
  1409. * @param {Module} module the module
  1410. * @param {RuntimeSpec} runtime the runtime
  1411. * @returns {ReadOnlyRuntimeRequirements} runtime requirements
  1412. */
  1413. getModuleRuntimeRequirements(module, runtime) {
  1414. const cgm = this._getChunkGraphModule(module);
  1415. const runtimeRequirements =
  1416. cgm.runtimeRequirements && cgm.runtimeRequirements.get(runtime);
  1417. return runtimeRequirements === undefined ? EMPTY_SET : runtimeRequirements;
  1418. }
  1419. /**
  1420. * @param {Chunk} chunk the chunk
  1421. * @returns {ReadOnlyRuntimeRequirements} runtime requirements
  1422. */
  1423. getChunkRuntimeRequirements(chunk) {
  1424. const cgc = this._getChunkGraphChunk(chunk);
  1425. const runtimeRequirements = cgc.runtimeRequirements;
  1426. return runtimeRequirements === undefined ? EMPTY_SET : runtimeRequirements;
  1427. }
  1428. /**
  1429. * @param {Module} module the module
  1430. * @param {RuntimeSpec} runtime the runtime
  1431. * @param {boolean} withConnections include connections
  1432. * @returns {string} hash
  1433. */
  1434. getModuleGraphHash(module, runtime, withConnections = true) {
  1435. const cgm = this._getChunkGraphModule(module);
  1436. return withConnections
  1437. ? this._getModuleGraphHashWithConnections(cgm, module, runtime)
  1438. : this._getModuleGraphHashBigInt(cgm, module, runtime).toString(16);
  1439. }
  1440. /**
  1441. * @param {Module} module the module
  1442. * @param {RuntimeSpec} runtime the runtime
  1443. * @param {boolean} withConnections include connections
  1444. * @returns {bigint} hash
  1445. */
  1446. getModuleGraphHashBigInt(module, runtime, withConnections = true) {
  1447. const cgm = this._getChunkGraphModule(module);
  1448. return withConnections
  1449. ? BigInt(
  1450. `0x${this._getModuleGraphHashWithConnections(cgm, module, runtime)}`
  1451. )
  1452. : this._getModuleGraphHashBigInt(cgm, module, runtime);
  1453. }
  1454. /**
  1455. * @param {ChunkGraphModule} cgm the ChunkGraphModule
  1456. * @param {Module} module the module
  1457. * @param {RuntimeSpec} runtime the runtime
  1458. * @returns {bigint} hash as big int
  1459. */
  1460. _getModuleGraphHashBigInt(cgm, module, runtime) {
  1461. if (cgm.graphHashes === undefined) {
  1462. cgm.graphHashes = new RuntimeSpecMap();
  1463. }
  1464. const graphHash = cgm.graphHashes.provide(runtime, () => {
  1465. const hash = createHash(this._hashFunction);
  1466. hash.update(`${cgm.id}${this.moduleGraph.isAsync(module)}`);
  1467. const sourceTypes = this._getOverwrittenModuleSourceTypes(module);
  1468. if (sourceTypes !== undefined) {
  1469. for (const type of sourceTypes) hash.update(type);
  1470. }
  1471. this.moduleGraph.getExportsInfo(module).updateHash(hash, runtime);
  1472. return BigInt(`0x${/** @type {string} */ (hash.digest("hex"))}`);
  1473. });
  1474. return graphHash;
  1475. }
  1476. /**
  1477. * @param {ChunkGraphModule} cgm the ChunkGraphModule
  1478. * @param {Module} module the module
  1479. * @param {RuntimeSpec} runtime the runtime
  1480. * @returns {string} hash
  1481. */
  1482. _getModuleGraphHashWithConnections(cgm, module, runtime) {
  1483. if (cgm.graphHashesWithConnections === undefined) {
  1484. cgm.graphHashesWithConnections = new RuntimeSpecMap();
  1485. }
  1486. /**
  1487. * @param {ConnectionState} state state
  1488. * @returns {"F" | "T" | "O"} result
  1489. */
  1490. const activeStateToString = state => {
  1491. if (state === false) return "F";
  1492. if (state === true) return "T";
  1493. if (state === ModuleGraphConnection.TRANSITIVE_ONLY) return "O";
  1494. throw new Error("Not implemented active state");
  1495. };
  1496. const strict = module.buildMeta && module.buildMeta.strictHarmonyModule;
  1497. return cgm.graphHashesWithConnections.provide(runtime, () => {
  1498. const graphHash = this._getModuleGraphHashBigInt(
  1499. cgm,
  1500. module,
  1501. runtime
  1502. ).toString(16);
  1503. const connections = this.moduleGraph.getOutgoingConnections(module);
  1504. /** @type {Set<Module>} */
  1505. const activeNamespaceModules = new Set();
  1506. /** @type {Map<string, Module | Set<Module>>} */
  1507. const connectedModules = new Map();
  1508. /**
  1509. * @param {ModuleGraphConnection} connection connection
  1510. * @param {string} stateInfo state info
  1511. */
  1512. const processConnection = (connection, stateInfo) => {
  1513. const module = connection.module;
  1514. stateInfo += module.getExportsType(this.moduleGraph, strict);
  1515. // cspell:word Tnamespace
  1516. if (stateInfo === "Tnamespace") activeNamespaceModules.add(module);
  1517. else {
  1518. const oldModule = connectedModules.get(stateInfo);
  1519. if (oldModule === undefined) {
  1520. connectedModules.set(stateInfo, module);
  1521. } else if (oldModule instanceof Set) {
  1522. oldModule.add(module);
  1523. } else if (oldModule !== module) {
  1524. connectedModules.set(stateInfo, new Set([oldModule, module]));
  1525. }
  1526. }
  1527. };
  1528. if (runtime === undefined || typeof runtime === "string") {
  1529. for (const connection of connections) {
  1530. const state = connection.getActiveState(runtime);
  1531. if (state === false) continue;
  1532. processConnection(connection, state === true ? "T" : "O");
  1533. }
  1534. } else {
  1535. // cspell:word Tnamespace
  1536. for (const connection of connections) {
  1537. const states = new Set();
  1538. let stateInfo = "";
  1539. forEachRuntime(
  1540. runtime,
  1541. runtime => {
  1542. const state = connection.getActiveState(runtime);
  1543. states.add(state);
  1544. stateInfo += activeStateToString(state) + runtime;
  1545. },
  1546. true
  1547. );
  1548. if (states.size === 1) {
  1549. const state = first(states);
  1550. if (state === false) continue;
  1551. stateInfo = activeStateToString(state);
  1552. }
  1553. processConnection(connection, stateInfo);
  1554. }
  1555. }
  1556. // cspell:word Tnamespace
  1557. if (activeNamespaceModules.size === 0 && connectedModules.size === 0)
  1558. return graphHash;
  1559. const connectedModulesInOrder =
  1560. connectedModules.size > 1
  1561. ? Array.from(connectedModules).sort(([a], [b]) => (a < b ? -1 : 1))
  1562. : connectedModules;
  1563. const hash = createHash(this._hashFunction);
  1564. /**
  1565. * @param {Module} module module
  1566. */
  1567. const addModuleToHash = module => {
  1568. hash.update(
  1569. this._getModuleGraphHashBigInt(
  1570. this._getChunkGraphModule(module),
  1571. module,
  1572. runtime
  1573. ).toString(16)
  1574. );
  1575. };
  1576. /**
  1577. * @param {Set<Module>} modules modules
  1578. */
  1579. const addModulesToHash = modules => {
  1580. let xor = ZERO_BIG_INT;
  1581. for (const m of modules) {
  1582. xor =
  1583. xor ^
  1584. this._getModuleGraphHashBigInt(
  1585. this._getChunkGraphModule(m),
  1586. m,
  1587. runtime
  1588. );
  1589. }
  1590. hash.update(xor.toString(16));
  1591. };
  1592. if (activeNamespaceModules.size === 1)
  1593. addModuleToHash(
  1594. /** @type {Module} */ (activeNamespaceModules.values().next().value)
  1595. );
  1596. else if (activeNamespaceModules.size > 1)
  1597. addModulesToHash(activeNamespaceModules);
  1598. for (const [stateInfo, modules] of connectedModulesInOrder) {
  1599. hash.update(stateInfo);
  1600. if (modules instanceof Set) {
  1601. addModulesToHash(modules);
  1602. } else {
  1603. addModuleToHash(modules);
  1604. }
  1605. }
  1606. hash.update(graphHash);
  1607. return /** @type {string} */ (hash.digest("hex"));
  1608. });
  1609. }
  1610. /**
  1611. * @param {Chunk} chunk the chunk
  1612. * @returns {ReadOnlyRuntimeRequirements} runtime requirements
  1613. */
  1614. getTreeRuntimeRequirements(chunk) {
  1615. const cgc = this._getChunkGraphChunk(chunk);
  1616. return cgc.runtimeRequirementsInTree;
  1617. }
  1618. // TODO remove in webpack 6
  1619. /**
  1620. * @param {Module} module the module
  1621. * @param {string} deprecateMessage message for the deprecation message
  1622. * @param {string} deprecationCode code for the deprecation
  1623. * @returns {ChunkGraph} the chunk graph
  1624. */
  1625. static getChunkGraphForModule(module, deprecateMessage, deprecationCode) {
  1626. const fn = deprecateGetChunkGraphForModuleMap.get(deprecateMessage);
  1627. if (fn) return fn(module);
  1628. const newFn = util.deprecate(
  1629. /**
  1630. * @param {Module} module the module
  1631. * @returns {ChunkGraph} the chunk graph
  1632. */
  1633. module => {
  1634. const chunkGraph = chunkGraphForModuleMap.get(module);
  1635. if (!chunkGraph)
  1636. throw new Error(
  1637. `${
  1638. deprecateMessage
  1639. }: There was no ChunkGraph assigned to the Module for backward-compat (Use the new API)`
  1640. );
  1641. return chunkGraph;
  1642. },
  1643. `${deprecateMessage}: Use new ChunkGraph API`,
  1644. deprecationCode
  1645. );
  1646. deprecateGetChunkGraphForModuleMap.set(deprecateMessage, newFn);
  1647. return newFn(module);
  1648. }
  1649. // TODO remove in webpack 6
  1650. /**
  1651. * @param {Module} module the module
  1652. * @param {ChunkGraph} chunkGraph the chunk graph
  1653. * @returns {void}
  1654. */
  1655. static setChunkGraphForModule(module, chunkGraph) {
  1656. chunkGraphForModuleMap.set(module, chunkGraph);
  1657. }
  1658. // TODO remove in webpack 6
  1659. /**
  1660. * @param {Module} module the module
  1661. * @returns {void}
  1662. */
  1663. static clearChunkGraphForModule(module) {
  1664. chunkGraphForModuleMap.delete(module);
  1665. }
  1666. // TODO remove in webpack 6
  1667. /**
  1668. * @param {Chunk} chunk the chunk
  1669. * @param {string} deprecateMessage message for the deprecation message
  1670. * @param {string} deprecationCode code for the deprecation
  1671. * @returns {ChunkGraph} the chunk graph
  1672. */
  1673. static getChunkGraphForChunk(chunk, deprecateMessage, deprecationCode) {
  1674. const fn = deprecateGetChunkGraphForChunkMap.get(deprecateMessage);
  1675. if (fn) return fn(chunk);
  1676. const newFn = util.deprecate(
  1677. /**
  1678. * @param {Chunk} chunk the chunk
  1679. * @returns {ChunkGraph} the chunk graph
  1680. */
  1681. chunk => {
  1682. const chunkGraph = chunkGraphForChunkMap.get(chunk);
  1683. if (!chunkGraph)
  1684. throw new Error(
  1685. `${
  1686. deprecateMessage
  1687. }There was no ChunkGraph assigned to the Chunk for backward-compat (Use the new API)`
  1688. );
  1689. return chunkGraph;
  1690. },
  1691. `${deprecateMessage}: Use new ChunkGraph API`,
  1692. deprecationCode
  1693. );
  1694. deprecateGetChunkGraphForChunkMap.set(deprecateMessage, newFn);
  1695. return newFn(chunk);
  1696. }
  1697. // TODO remove in webpack 6
  1698. /**
  1699. * @param {Chunk} chunk the chunk
  1700. * @param {ChunkGraph} chunkGraph the chunk graph
  1701. * @returns {void}
  1702. */
  1703. static setChunkGraphForChunk(chunk, chunkGraph) {
  1704. chunkGraphForChunkMap.set(chunk, chunkGraph);
  1705. }
  1706. // TODO remove in webpack 6
  1707. /**
  1708. * @param {Chunk} chunk the chunk
  1709. * @returns {void}
  1710. */
  1711. static clearChunkGraphForChunk(chunk) {
  1712. chunkGraphForChunkMap.delete(chunk);
  1713. }
  1714. }
  1715. // TODO remove in webpack 6
  1716. /** @type {WeakMap<Module, ChunkGraph>} */
  1717. const chunkGraphForModuleMap = new WeakMap();
  1718. // TODO remove in webpack 6
  1719. /** @type {WeakMap<Chunk, ChunkGraph>} */
  1720. const chunkGraphForChunkMap = new WeakMap();
  1721. // TODO remove in webpack 6
  1722. /** @type {Map<string, (module: Module) => ChunkGraph>} */
  1723. const deprecateGetChunkGraphForModuleMap = new Map();
  1724. // TODO remove in webpack 6
  1725. /** @type {Map<string, (chunk: Chunk) => ChunkGraph>} */
  1726. const deprecateGetChunkGraphForChunkMap = new Map();
  1727. module.exports = ChunkGraph;