You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1864 lines
54 KiB

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