Chunk.js 22 KB

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