ChunkGraph.js 57 KB

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