10
0

Chunk.js 23 KB

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