Chunk.js 22 KB

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