ChunkGraph.js 52 KB

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