ChunkGraph.js 54 KB

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