ChunkGraph.js 54 KB

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