Chunk.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ChunkGraph = require("./ChunkGraph");
  7. const Entrypoint = require("./Entrypoint");
  8. const { intersect } = require("./util/SetHelpers");
  9. const SortableSet = require("./util/SortableSet");
  10. const StringXor = require("./util/StringXor");
  11. const {
  12. compareChunkGroupsByIndex,
  13. compareModulesById,
  14. compareModulesByIdentifier
  15. } = require("./util/comparators");
  16. const { createArrayToSetDeprecationSet } = require("./util/deprecation");
  17. const { mergeRuntime } = require("./util/runtime");
  18. /**
  19. * @import {
  20. * ChunkFilterPredicate,
  21. * ChunkSizeOptions,
  22. * ModuleFilterPredicate,
  23. * ModuleId
  24. * } from "./ChunkGraph"
  25. */
  26. /** @import ChunkGroup from "./ChunkGroup" */
  27. /** @import { EntryOptions } from "./Entrypoint" */
  28. /** @import Module from "./Module" */
  29. /** @import { PathDataChunk } from "./Compilation" */
  30. /** @typedef {import("./TemplatedPathPlugin").TemplatePathFn<PathDataChunk>} ChunkFilenameTemplateFn */
  31. /** @typedef {string | ChunkFilenameTemplateFn} ChunkFilenameTemplate */
  32. /** @import Hash from "./util/Hash" */
  33. /** @import { RuntimeSpec } from "./util/runtime" */
  34. /** @typedef {string | null} ChunkName */
  35. /** @typedef {string | number} ChunkId */
  36. /** @typedef {SortableSet<string>} IdNameHints */
  37. const ChunkFilesSet = createArrayToSetDeprecationSet("chunk.files");
  38. /**
  39. * Defines the chunk maps type used by this module.
  40. * @deprecated
  41. * @typedef {object} ChunkMaps
  42. * @property {Record<ChunkId, string>} hash
  43. * @property {Record<ChunkId, Record<string, string>>} contentHash
  44. * @property {Record<ChunkId, string>} name
  45. */
  46. /**
  47. * Defines the chunk module id map type used by this module.
  48. * @deprecated
  49. * @typedef {Record<ChunkId, ChunkId[]>} ChunkModuleIdMap
  50. */
  51. /**
  52. * Defines the chunk module hash map type used by this module.
  53. * @deprecated
  54. * @typedef {Record<ModuleId, string>} chunkModuleHashMap
  55. */
  56. /**
  57. * Defines the chunk module maps type used by this module.
  58. * @deprecated
  59. * @typedef {object} ChunkModuleMaps
  60. * @property {ChunkModuleIdMap} id
  61. * @property {chunkModuleHashMap} hash
  62. */
  63. /** @typedef {Set<Chunk>} Chunks */
  64. /** @typedef {Set<Entrypoint>} Entrypoints */
  65. /** @typedef {Set<ChunkGroup>} Queue */
  66. /** @typedef {SortableSet<ChunkGroup>} SortableChunkGroups */
  67. /** @typedef {Record<string, ChunkId[]>} ChunkChildIdsByOrdersMap */
  68. /** @typedef {Record<string, ChunkChildIdsByOrdersMap>} ChunkChildIdsByOrdersMapByData */
  69. /** @typedef {{ onChunks: Chunk[], chunks: Chunks }} ChunkChildOfTypeInOrder */
  70. let debugId = 1000;
  71. /**
  72. * A Chunk is a unit of encapsulation for Modules.
  73. * Chunks are "rendered" into bundles that get emitted when the build completes.
  74. */
  75. class Chunk {
  76. /**
  77. * Creates an instance of Chunk.
  78. * @param {ChunkName=} name of chunk being created, is optional (for subclasses)
  79. * @param {boolean} backCompat enable backward-compatibility
  80. */
  81. constructor(name, backCompat = true) {
  82. /** @type {ChunkId | null} */
  83. this.id = null;
  84. /** @type {ChunkId[] | null} */
  85. this.ids = null;
  86. /** @type {number} */
  87. this.debugId = debugId++;
  88. /** @type {ChunkName | undefined} */
  89. this.name = name;
  90. /** @type {IdNameHints} */
  91. this.idNameHints = new SortableSet();
  92. /** @type {boolean} */
  93. this.preventIntegration = false;
  94. /** @type {ChunkFilenameTemplate | undefined} */
  95. this.filenameTemplate = undefined;
  96. /** @type {ChunkFilenameTemplate | undefined} */
  97. this.cssFilenameTemplate = undefined;
  98. /**
  99. * @private
  100. * @type {SortableChunkGroups}
  101. */
  102. this._groups = new SortableSet(undefined, compareChunkGroupsByIndex);
  103. /** @type {RuntimeSpec} */
  104. this.runtime = undefined;
  105. /** @type {Set<string>} */
  106. this.files = backCompat ? new ChunkFilesSet() : new Set();
  107. /** @type {Set<string>} */
  108. this.auxiliaryFiles = new Set();
  109. /** @type {boolean} */
  110. this.rendered = false;
  111. /** @type {string=} */
  112. this.hash = undefined;
  113. /** @type {Record<string, string>} */
  114. this.contentHash = Object.create(null);
  115. // Full (untruncated) content digests, so `[contenthash:<digest>]` re-encodes
  116. // from full entropy. Transient: repopulated by the `contentHash` hook each seal.
  117. /** @type {Record<string, string>} */
  118. this.contentHashFull = Object.create(null);
  119. /** @type {string=} */
  120. this.renderedHash = undefined;
  121. /** @type {string=} */
  122. this.chunkReason = undefined;
  123. /** @type {boolean} */
  124. this.extraAsync = false;
  125. }
  126. // TODO remove in webpack 6
  127. // BACKWARD-COMPAT START
  128. /**
  129. * Returns entry module.
  130. * @deprecated
  131. * @returns {Module | undefined} entry module
  132. */
  133. get entryModule() {
  134. const entryModules = [
  135. ...ChunkGraph.getChunkGraphForChunk(
  136. this,
  137. "Chunk.entryModule",
  138. "DEP_WEBPACK_CHUNK_ENTRY_MODULE"
  139. ).getChunkEntryModulesIterable(this)
  140. ];
  141. if (entryModules.length === 0) {
  142. return undefined;
  143. } else if (entryModules.length === 1) {
  144. return entryModules[0];
  145. }
  146. throw new Error(
  147. "Module.entryModule: Multiple entry modules are not supported by the deprecated API (Use the new ChunkGroup API)"
  148. );
  149. }
  150. /**
  151. * Checks whether this chunk has an entry module.
  152. * @deprecated
  153. * @returns {boolean} true, if the chunk contains an entry module
  154. */
  155. hasEntryModule() {
  156. return (
  157. ChunkGraph.getChunkGraphForChunk(
  158. this,
  159. "Chunk.hasEntryModule",
  160. "DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE"
  161. ).getNumberOfEntryModules(this) > 0
  162. );
  163. }
  164. /**
  165. * Adds the provided module to the chunk.
  166. * @deprecated
  167. * @param {Module} module the module
  168. * @returns {boolean} true, if the chunk could be added
  169. */
  170. addModule(module) {
  171. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  172. this,
  173. "Chunk.addModule",
  174. "DEP_WEBPACK_CHUNK_ADD_MODULE"
  175. );
  176. if (chunkGraph.isModuleInChunk(module, this)) return false;
  177. chunkGraph.connectChunkAndModule(this, module);
  178. return true;
  179. }
  180. /**
  181. * Removes the provided module from the chunk.
  182. * @deprecated
  183. * @param {Module} module the module
  184. * @returns {void}
  185. */
  186. removeModule(module) {
  187. ChunkGraph.getChunkGraphForChunk(
  188. this,
  189. "Chunk.removeModule",
  190. "DEP_WEBPACK_CHUNK_REMOVE_MODULE"
  191. ).disconnectChunkAndModule(this, module);
  192. }
  193. /**
  194. * Gets the number of modules in this chunk.
  195. * @deprecated
  196. * @returns {number} the number of module which are contained in this chunk
  197. */
  198. getNumberOfModules() {
  199. return ChunkGraph.getChunkGraphForChunk(
  200. this,
  201. "Chunk.getNumberOfModules",
  202. "DEP_WEBPACK_CHUNK_GET_NUMBER_OF_MODULES"
  203. ).getNumberOfChunkModules(this);
  204. }
  205. /**
  206. * @deprecated
  207. * @returns {Iterable<Module>} modules
  208. */
  209. get modulesIterable() {
  210. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  211. this,
  212. "Chunk.modulesIterable",
  213. "DEP_WEBPACK_CHUNK_MODULES_ITERABLE"
  214. );
  215. return chunkGraph.getOrderedChunkModulesIterable(
  216. this,
  217. compareModulesByIdentifier
  218. );
  219. }
  220. /**
  221. * Compares this chunk with another chunk.
  222. * @deprecated
  223. * @param {Chunk} otherChunk the chunk to compare with
  224. * @returns {-1 | 0 | 1} the comparison result
  225. */
  226. compareTo(otherChunk) {
  227. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  228. this,
  229. "Chunk.compareTo",
  230. "DEP_WEBPACK_CHUNK_COMPARE_TO"
  231. );
  232. return chunkGraph.compareChunks(this, otherChunk);
  233. }
  234. /**
  235. * Checks whether this chunk contains the module.
  236. * @deprecated
  237. * @param {Module} module the module
  238. * @returns {boolean} true, if the chunk contains the module
  239. */
  240. containsModule(module) {
  241. return ChunkGraph.getChunkGraphForChunk(
  242. this,
  243. "Chunk.containsModule",
  244. "DEP_WEBPACK_CHUNK_CONTAINS_MODULE"
  245. ).isModuleInChunk(module, this);
  246. }
  247. /**
  248. * Returns the modules for this chunk.
  249. * @deprecated
  250. * @returns {Module[]} the modules for this chunk
  251. */
  252. getModules() {
  253. return ChunkGraph.getChunkGraphForChunk(
  254. this,
  255. "Chunk.getModules",
  256. "DEP_WEBPACK_CHUNK_GET_MODULES"
  257. ).getChunkModules(this);
  258. }
  259. /**
  260. * Removes this chunk from the chunk graph and chunk groups.
  261. * @deprecated
  262. * @returns {void}
  263. */
  264. remove() {
  265. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  266. this,
  267. "Chunk.remove",
  268. "DEP_WEBPACK_CHUNK_REMOVE"
  269. );
  270. chunkGraph.disconnectChunk(this);
  271. this.disconnectFromGroups();
  272. }
  273. /**
  274. * Moves a module from this chunk to another chunk.
  275. * @deprecated
  276. * @param {Module} module the module
  277. * @param {Chunk} otherChunk the target chunk
  278. * @returns {void}
  279. */
  280. moveModule(module, otherChunk) {
  281. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  282. this,
  283. "Chunk.moveModule",
  284. "DEP_WEBPACK_CHUNK_MOVE_MODULE"
  285. );
  286. chunkGraph.disconnectChunkAndModule(this, module);
  287. chunkGraph.connectChunkAndModule(otherChunk, module);
  288. }
  289. /**
  290. * Integrates another chunk into this chunk when possible.
  291. * @deprecated
  292. * @param {Chunk} otherChunk the other chunk
  293. * @returns {boolean} true, if the specified chunk has been integrated
  294. */
  295. integrate(otherChunk) {
  296. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  297. this,
  298. "Chunk.integrate",
  299. "DEP_WEBPACK_CHUNK_INTEGRATE"
  300. );
  301. if (chunkGraph.canChunksBeIntegrated(this, otherChunk)) {
  302. chunkGraph.integrateChunks(this, otherChunk);
  303. return true;
  304. }
  305. return false;
  306. }
  307. /**
  308. * Checks whether this chunk can be integrated with another chunk.
  309. * @deprecated
  310. * @param {Chunk} otherChunk the other chunk
  311. * @returns {boolean} true, if chunks could be integrated
  312. */
  313. canBeIntegrated(otherChunk) {
  314. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  315. this,
  316. "Chunk.canBeIntegrated",
  317. "DEP_WEBPACK_CHUNK_CAN_BE_INTEGRATED"
  318. );
  319. return chunkGraph.canChunksBeIntegrated(this, otherChunk);
  320. }
  321. /**
  322. * Checks whether this chunk is empty.
  323. * @deprecated
  324. * @returns {boolean} true, if this chunk contains no module
  325. */
  326. isEmpty() {
  327. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  328. this,
  329. "Chunk.isEmpty",
  330. "DEP_WEBPACK_CHUNK_IS_EMPTY"
  331. );
  332. return chunkGraph.getNumberOfChunkModules(this) === 0;
  333. }
  334. /**
  335. * Returns the total size of all modules in this chunk.
  336. * @deprecated
  337. * @returns {number} total size of all modules in this chunk
  338. */
  339. modulesSize() {
  340. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  341. this,
  342. "Chunk.modulesSize",
  343. "DEP_WEBPACK_CHUNK_MODULES_SIZE"
  344. );
  345. return chunkGraph.getChunkModulesSize(this);
  346. }
  347. /**
  348. * Returns the estimated size for the requested source type.
  349. * @deprecated
  350. * @param {ChunkSizeOptions} options options object
  351. * @returns {number} total size of this chunk
  352. */
  353. size(options = {}) {
  354. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  355. this,
  356. "Chunk.size",
  357. "DEP_WEBPACK_CHUNK_SIZE"
  358. );
  359. return chunkGraph.getChunkSize(this, options);
  360. }
  361. /**
  362. * Returns the integrated size with another chunk.
  363. * @deprecated
  364. * @param {Chunk} otherChunk the other chunk
  365. * @param {ChunkSizeOptions} options options object
  366. * @returns {number} total size of the chunk or false if the chunk can't be integrated
  367. */
  368. integratedSize(otherChunk, options) {
  369. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  370. this,
  371. "Chunk.integratedSize",
  372. "DEP_WEBPACK_CHUNK_INTEGRATED_SIZE"
  373. );
  374. return chunkGraph.getIntegratedChunksSize(this, otherChunk, options);
  375. }
  376. /**
  377. * Gets chunk module maps.
  378. * @deprecated
  379. * @param {ModuleFilterPredicate} filterFn function used to filter modules
  380. * @returns {ChunkModuleMaps} module map information
  381. */
  382. getChunkModuleMaps(filterFn) {
  383. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  384. this,
  385. "Chunk.getChunkModuleMaps",
  386. "DEP_WEBPACK_CHUNK_GET_CHUNK_MODULE_MAPS"
  387. );
  388. /** @type {ChunkModuleIdMap} */
  389. const chunkModuleIdMap = Object.create(null);
  390. /** @type {chunkModuleHashMap} */
  391. const chunkModuleHashMap = Object.create(null);
  392. for (const asyncChunk of this.getAllAsyncChunks()) {
  393. /** @type {ChunkId[] | undefined} */
  394. let array;
  395. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  396. asyncChunk,
  397. compareModulesById(chunkGraph)
  398. )) {
  399. if (filterFn(module)) {
  400. if (array === undefined) {
  401. array = [];
  402. chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array;
  403. }
  404. const moduleId =
  405. /** @type {ModuleId} */
  406. (chunkGraph.getModuleId(module));
  407. array.push(moduleId);
  408. chunkModuleHashMap[moduleId] = chunkGraph.getRenderedModuleHash(
  409. module,
  410. undefined
  411. );
  412. }
  413. }
  414. }
  415. return {
  416. id: chunkModuleIdMap,
  417. hash: chunkModuleHashMap
  418. };
  419. }
  420. /**
  421. * Checks whether this chunk contains a matching module in the graph.
  422. * @deprecated
  423. * @param {ModuleFilterPredicate} filterFn predicate function used to filter modules
  424. * @param {ChunkFilterPredicate=} filterChunkFn predicate function used to filter chunks
  425. * @returns {boolean} return true if module exists in graph
  426. */
  427. hasModuleInGraph(filterFn, filterChunkFn) {
  428. const chunkGraph = ChunkGraph.getChunkGraphForChunk(
  429. this,
  430. "Chunk.hasModuleInGraph",
  431. "DEP_WEBPACK_CHUNK_HAS_MODULE_IN_GRAPH"
  432. );
  433. return chunkGraph.hasModuleInGraph(this, filterFn, filterChunkFn);
  434. }
  435. /**
  436. * Returns the chunk map information.
  437. * @deprecated
  438. * @param {boolean} realHash whether the full hash or the rendered hash is to be used
  439. * @returns {ChunkMaps} the chunk map information
  440. */
  441. getChunkMaps(realHash) {
  442. /** @type {Record<ChunkId, string>} */
  443. const chunkHashMap = Object.create(null);
  444. /** @type {Record<string, Record<ChunkId, string>>} */
  445. const chunkContentHashMap = Object.create(null);
  446. /** @type {Record<ChunkId, string>} */
  447. const chunkNameMap = Object.create(null);
  448. for (const chunk of this.getAllAsyncChunks()) {
  449. const id = /** @type {ChunkId} */ (chunk.id);
  450. chunkHashMap[id] =
  451. /** @type {string} */
  452. (realHash ? chunk.hash : chunk.renderedHash);
  453. for (const key of Object.keys(chunk.contentHash)) {
  454. if (!chunkContentHashMap[key]) {
  455. chunkContentHashMap[key] = Object.create(null);
  456. }
  457. chunkContentHashMap[key][id] = chunk.contentHash[key];
  458. }
  459. if (chunk.name) {
  460. chunkNameMap[id] = chunk.name;
  461. }
  462. }
  463. return {
  464. hash: chunkHashMap,
  465. contentHash: chunkContentHashMap,
  466. name: chunkNameMap
  467. };
  468. }
  469. // BACKWARD-COMPAT END
  470. /**
  471. * Checks whether this chunk has runtime.
  472. * @returns {boolean} whether or not the Chunk will have a runtime
  473. */
  474. hasRuntime() {
  475. for (const chunkGroup of this._groups) {
  476. if (
  477. chunkGroup instanceof Entrypoint &&
  478. chunkGroup.getRuntimeChunk() === this
  479. ) {
  480. return true;
  481. }
  482. }
  483. return false;
  484. }
  485. /**
  486. * Checks whether it can be initial.
  487. * @returns {boolean} whether or not this chunk can be an initial chunk
  488. */
  489. canBeInitial() {
  490. for (const chunkGroup of this._groups) {
  491. if (chunkGroup.isInitial()) return true;
  492. }
  493. return false;
  494. }
  495. /**
  496. * Checks whether this chunk is only initial.
  497. * @returns {boolean} whether this chunk can only be an initial chunk
  498. */
  499. isOnlyInitial() {
  500. if (this._groups.size <= 0) return false;
  501. for (const chunkGroup of this._groups) {
  502. if (!chunkGroup.isInitial()) return false;
  503. }
  504. return true;
  505. }
  506. /**
  507. * Gets entry options.
  508. * @returns {EntryOptions | undefined} the entry options for this chunk
  509. */
  510. getEntryOptions() {
  511. for (const chunkGroup of this._groups) {
  512. if (chunkGroup instanceof Entrypoint) {
  513. return chunkGroup.options;
  514. }
  515. }
  516. return undefined;
  517. }
  518. /**
  519. * Adds the provided chunk group to the chunk.
  520. * @param {ChunkGroup} chunkGroup the chunkGroup the chunk is being added
  521. * @returns {void}
  522. */
  523. addGroup(chunkGroup) {
  524. this._groups.add(chunkGroup);
  525. }
  526. /**
  527. * Removes the provided chunk group from the chunk.
  528. * @param {ChunkGroup} chunkGroup the chunkGroup the chunk is being removed from
  529. * @returns {void}
  530. */
  531. removeGroup(chunkGroup) {
  532. this._groups.delete(chunkGroup);
  533. }
  534. /**
  535. * Checks whether this chunk is in group.
  536. * @param {ChunkGroup} chunkGroup the chunkGroup to check
  537. * @returns {boolean} returns true if chunk has chunkGroup reference and exists in chunkGroup
  538. */
  539. isInGroup(chunkGroup) {
  540. return this._groups.has(chunkGroup);
  541. }
  542. /**
  543. * Gets number of groups.
  544. * @returns {number} the amount of groups that the said chunk is in
  545. */
  546. getNumberOfGroups() {
  547. return this._groups.size;
  548. }
  549. /**
  550. * Gets groups iterable.
  551. * @returns {SortableChunkGroups} the chunkGroups that the said chunk is referenced in
  552. */
  553. get groupsIterable() {
  554. this._groups.sort();
  555. return this._groups;
  556. }
  557. /**
  558. * Disconnects from groups.
  559. * @returns {void}
  560. */
  561. disconnectFromGroups() {
  562. for (const chunkGroup of this._groups) {
  563. chunkGroup.removeChunk(this);
  564. }
  565. }
  566. /**
  567. * Processes the provided new chunk.
  568. * @param {Chunk} newChunk the new chunk that will be split out of
  569. * @returns {void}
  570. */
  571. split(newChunk) {
  572. for (const chunkGroup of this._groups) {
  573. chunkGroup.insertChunk(newChunk, this);
  574. newChunk.addGroup(chunkGroup);
  575. }
  576. for (const idHint of this.idNameHints) {
  577. newChunk.idNameHints.add(idHint);
  578. }
  579. newChunk.runtime = mergeRuntime(newChunk.runtime, this.runtime);
  580. }
  581. /**
  582. * Updates the hash with the data contributed by this instance.
  583. * @param {Hash} hash hash (will be modified)
  584. * @param {ChunkGraph} chunkGraph the chunk graph
  585. * @returns {void}
  586. */
  587. updateHash(hash, chunkGraph) {
  588. hash.update(
  589. `${this.id} ${this.ids ? this.ids.join() : ""} ${this.name || ""} `
  590. );
  591. const xor = new StringXor();
  592. for (const m of chunkGraph.getChunkModulesIterable(this)) {
  593. xor.add(chunkGraph.getModuleHash(m, this.runtime));
  594. }
  595. xor.updateHash(hash);
  596. const entryModules =
  597. chunkGraph.getChunkEntryModulesWithChunkGroupIterable(this);
  598. for (const [m, chunkGroup] of entryModules) {
  599. hash.update(
  600. `entry${chunkGraph.getModuleId(m)}${
  601. /** @type {ChunkGroup} */ (chunkGroup).id
  602. }`
  603. );
  604. }
  605. }
  606. /**
  607. * Gets all async chunks.
  608. * @returns {Chunks} a set of all the async chunks
  609. */
  610. getAllAsyncChunks() {
  611. /** @type {Queue} */
  612. const queue = new Set();
  613. /** @type {Chunks} */
  614. const chunks = new Set();
  615. const initialChunks = intersect(
  616. Array.from(this.groupsIterable, (g) => new Set(g.chunks))
  617. );
  618. /** @type {Queue} */
  619. const initialQueue = new Set(this.groupsIterable);
  620. for (const chunkGroup of initialQueue) {
  621. for (const child of chunkGroup.childrenIterable) {
  622. if (child instanceof Entrypoint) {
  623. initialQueue.add(child);
  624. } else {
  625. queue.add(child);
  626. }
  627. }
  628. }
  629. for (const chunkGroup of queue) {
  630. for (const chunk of chunkGroup.chunks) {
  631. if (!initialChunks.has(chunk)) {
  632. chunks.add(chunk);
  633. }
  634. }
  635. for (const child of chunkGroup.childrenIterable) {
  636. queue.add(child);
  637. }
  638. }
  639. return chunks;
  640. }
  641. /**
  642. * Gets all initial chunks.
  643. * @returns {Chunks} a set of all the initial chunks (including itself)
  644. */
  645. getAllInitialChunks() {
  646. /** @type {Chunks} */
  647. const chunks = new Set();
  648. /** @type {Queue} */
  649. const queue = new Set(this.groupsIterable);
  650. for (const group of queue) {
  651. if (group.isInitial()) {
  652. for (const c of group.chunks) chunks.add(c);
  653. for (const g of group.childrenIterable) queue.add(g);
  654. }
  655. }
  656. return chunks;
  657. }
  658. /**
  659. * Gets all referenced chunks.
  660. * @returns {Chunks} a set of all the referenced chunks (including itself)
  661. */
  662. getAllReferencedChunks() {
  663. /** @type {Queue} */
  664. const queue = new Set(this.groupsIterable);
  665. /** @type {Chunks} */
  666. const chunks = new Set();
  667. for (const chunkGroup of queue) {
  668. for (const chunk of chunkGroup.chunks) {
  669. chunks.add(chunk);
  670. }
  671. for (const child of chunkGroup.childrenIterable) {
  672. queue.add(child);
  673. }
  674. }
  675. return chunks;
  676. }
  677. /**
  678. * Gets all referenced async entrypoints.
  679. * @returns {Entrypoints} a set of all the referenced entrypoints
  680. */
  681. getAllReferencedAsyncEntrypoints() {
  682. /** @type {Queue} */
  683. const queue = new Set(this.groupsIterable);
  684. /** @type {Entrypoints} */
  685. const entrypoints = new Set();
  686. for (const chunkGroup of queue) {
  687. for (const entrypoint of chunkGroup.asyncEntrypointsIterable) {
  688. entrypoints.add(/** @type {Entrypoint} */ (entrypoint));
  689. }
  690. for (const child of chunkGroup.childrenIterable) {
  691. queue.add(child);
  692. }
  693. }
  694. return entrypoints;
  695. }
  696. /**
  697. * Checks whether this chunk has async chunks.
  698. * @returns {boolean} true, if the chunk references async chunks
  699. */
  700. hasAsyncChunks() {
  701. /** @type {Queue} */
  702. const queue = new Set();
  703. const initialChunks = intersect(
  704. Array.from(this.groupsIterable, (g) => new Set(g.chunks))
  705. );
  706. for (const chunkGroup of this.groupsIterable) {
  707. for (const child of chunkGroup.childrenIterable) {
  708. queue.add(child);
  709. }
  710. }
  711. for (const chunkGroup of queue) {
  712. for (const chunk of chunkGroup.chunks) {
  713. if (!initialChunks.has(chunk)) {
  714. return true;
  715. }
  716. }
  717. for (const child of chunkGroup.childrenIterable) {
  718. queue.add(child);
  719. }
  720. }
  721. return false;
  722. }
  723. /**
  724. * Gets child ids by orders.
  725. * @param {ChunkGraph} chunkGraph the chunk graph
  726. * @param {ChunkFilterPredicate=} filterFn function used to filter chunks
  727. * @returns {Record<string, ChunkId[]>} a record object of names to lists of child ids(?)
  728. */
  729. getChildIdsByOrders(chunkGraph, filterFn) {
  730. /** @type {Map<string, { order: number, group: ChunkGroup }[]>} */
  731. const lists = new Map();
  732. for (const group of this.groupsIterable) {
  733. if (group.chunks[group.chunks.length - 1] === this) {
  734. for (const childGroup of group.childrenIterable) {
  735. const edgeOptions = group.getChildOrderOptions(
  736. childGroup,
  737. chunkGraph
  738. );
  739. for (const key of Object.keys(edgeOptions)) {
  740. const name = key.slice(0, key.length - "Order".length);
  741. let list = lists.get(name);
  742. if (list === undefined) {
  743. list = [];
  744. lists.set(name, list);
  745. }
  746. list.push({
  747. order: edgeOptions[key],
  748. group: childGroup
  749. });
  750. }
  751. }
  752. }
  753. }
  754. /** @type {Record<string, ChunkId[]>} */
  755. const result = Object.create(null);
  756. for (const [name, list] of lists) {
  757. list.sort((a, b) => {
  758. const cmp = b.order - a.order;
  759. if (cmp !== 0) return cmp;
  760. return a.group.compareTo(chunkGraph, b.group);
  761. });
  762. /** @type {Set<ChunkId>} */
  763. const chunkIdSet = new Set();
  764. for (const item of list) {
  765. for (const chunk of item.group.chunks) {
  766. if (filterFn && !filterFn(chunk, chunkGraph)) continue;
  767. chunkIdSet.add(/** @type {ChunkId} */ (chunk.id));
  768. }
  769. }
  770. if (chunkIdSet.size > 0) {
  771. result[name] = [...chunkIdSet];
  772. }
  773. }
  774. return result;
  775. }
  776. /**
  777. * Gets children of type in order.
  778. * @param {ChunkGraph} chunkGraph the chunk graph
  779. * @param {string} type option name
  780. * @returns {ChunkChildOfTypeInOrder[] | undefined} referenced chunks for a specific type
  781. */
  782. getChildrenOfTypeInOrder(chunkGraph, type) {
  783. /** @type {{ order: number, group: ChunkGroup, childGroup: ChunkGroup }[]} */
  784. const list = [];
  785. for (const group of this.groupsIterable) {
  786. for (const childGroup of group.childrenIterable) {
  787. const edgeOptions = group.getChildOrderOptions(childGroup, chunkGraph);
  788. const order = edgeOptions[type];
  789. if (order === undefined) continue;
  790. list.push({
  791. order,
  792. group,
  793. childGroup
  794. });
  795. }
  796. }
  797. if (list.length === 0) return;
  798. list.sort((a, b) => {
  799. const cmp = b.order - a.order;
  800. if (cmp !== 0) return cmp;
  801. return a.group.compareTo(chunkGraph, b.group);
  802. });
  803. /** @type {ChunkChildOfTypeInOrder[]} */
  804. const result = [];
  805. /** @type {undefined | ChunkChildOfTypeInOrder} */
  806. let lastEntry;
  807. for (const { group, childGroup } of list) {
  808. if (lastEntry && lastEntry.onChunks === group.chunks) {
  809. for (const chunk of childGroup.chunks) {
  810. lastEntry.chunks.add(chunk);
  811. }
  812. } else {
  813. result.push(
  814. (lastEntry = {
  815. onChunks: group.chunks,
  816. chunks: new Set(childGroup.chunks)
  817. })
  818. );
  819. }
  820. }
  821. return result;
  822. }
  823. /**
  824. * Gets child ids by orders map.
  825. * @param {ChunkGraph} chunkGraph the chunk graph
  826. * @param {boolean=} includeDirectChildren include direct children (by default only children of async children are included)
  827. * @param {ChunkFilterPredicate=} filterFn function used to filter chunks
  828. * @returns {ChunkChildIdsByOrdersMapByData} a record object of names to lists of child ids(?) by chunk id
  829. */
  830. getChildIdsByOrdersMap(chunkGraph, includeDirectChildren, filterFn) {
  831. /** @type {ChunkChildIdsByOrdersMapByData} */
  832. const chunkMaps = Object.create(null);
  833. /**
  834. * Adds child ids by orders to map.
  835. * @param {Chunk} chunk a chunk
  836. * @returns {void}
  837. */
  838. const addChildIdsByOrdersToMap = (chunk) => {
  839. const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
  840. for (const key of Object.keys(data)) {
  841. let chunkMap = chunkMaps[key];
  842. if (chunkMap === undefined) {
  843. chunkMaps[key] = chunkMap = Object.create(null);
  844. }
  845. chunkMap[/** @type {ChunkId} */ (chunk.id)] = data[key];
  846. }
  847. };
  848. if (includeDirectChildren) {
  849. /** @type {Chunks} */
  850. const chunks = new Set();
  851. for (const chunkGroup of this.groupsIterable) {
  852. for (const chunk of chunkGroup.chunks) {
  853. chunks.add(chunk);
  854. }
  855. }
  856. for (const chunk of chunks) {
  857. addChildIdsByOrdersToMap(chunk);
  858. }
  859. }
  860. for (const chunk of this.getAllAsyncChunks()) {
  861. addChildIdsByOrdersToMap(chunk);
  862. }
  863. return chunkMaps;
  864. }
  865. /**
  866. * Checks whether this chunk contains the chunk graph.
  867. * @param {ChunkGraph} chunkGraph the chunk graph
  868. * @param {string} type option name
  869. * @param {boolean=} includeDirectChildren include direct children (by default only children of async children are included)
  870. * @param {ChunkFilterPredicate=} filterFn function used to filter chunks
  871. * @returns {boolean} true when the child is of type order, otherwise false
  872. */
  873. hasChildByOrder(chunkGraph, type, includeDirectChildren, filterFn) {
  874. if (includeDirectChildren) {
  875. /** @type {Chunks} */
  876. const chunks = new Set();
  877. for (const chunkGroup of this.groupsIterable) {
  878. for (const chunk of chunkGroup.chunks) {
  879. chunks.add(chunk);
  880. }
  881. }
  882. for (const chunk of chunks) {
  883. const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
  884. if (data[type] !== undefined) return true;
  885. }
  886. }
  887. for (const chunk of this.getAllAsyncChunks()) {
  888. const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
  889. if (data[type] !== undefined) return true;
  890. }
  891. return false;
  892. }
  893. }
  894. module.exports = Chunk;