ChunkGraph.js 56 KB

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