ChunkGroup.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const SortableSet = require("./util/SortableSet");
  8. const {
  9. compareChunks,
  10. compareIterables,
  11. compareLocations
  12. } = require("./util/comparators");
  13. /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
  14. /** @typedef {import("./Chunk")} Chunk */
  15. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  16. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  17. /** @typedef {import("./Entrypoint")} Entrypoint */
  18. /** @typedef {import("./Module")} Module */
  19. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  20. /** @typedef {{ module: Module | null, loc: DependencyLocation, request: string }} OriginRecord */
  21. /**
  22. * Describes the scheduling hints that can be attached to a chunk group.
  23. * These values influence how child groups are ordered for preload/prefetch
  24. * and how their fetch priority is exposed to runtime code.
  25. * @typedef {object} RawChunkGroupOptions
  26. * @property {number=} preloadOrder
  27. * @property {number=} prefetchOrder
  28. * @property {("low" | "high" | "auto")=} fetchPriority
  29. */
  30. /** @typedef {RawChunkGroupOptions & { name?: string | null }} ChunkGroupOptions */
  31. let debugId = 5000;
  32. /**
  33. * Materializes a sortable set as an array without changing its current order.
  34. * Used with `SortableSet` caches that expect a stable array result.
  35. * @template T
  36. * @param {SortableSet<T>} set set to convert to array.
  37. * @returns {T[]} the array format of existing set
  38. */
  39. const getArray = (set) => [...set];
  40. /**
  41. * A convenience method used to sort chunks based on their id's
  42. * @param {ChunkGroup} a first sorting comparator
  43. * @param {ChunkGroup} b second sorting comparator
  44. * @returns {1 | 0 | -1} a sorting index to determine order
  45. */
  46. const sortById = (a, b) => {
  47. if (a.id < b.id) return -1;
  48. if (b.id < a.id) return 1;
  49. return 0;
  50. };
  51. /**
  52. * Orders origin records by referencing module and then by source location.
  53. * This keeps origin metadata deterministic for hashing and diagnostics.
  54. * @param {OriginRecord} a the first comparator in sort
  55. * @param {OriginRecord} b the second comparator in sort
  56. * @returns {1 | -1 | 0} returns sorting order as index
  57. */
  58. const sortOrigin = (a, b) => {
  59. const aIdent = a.module ? a.module.identifier() : "";
  60. const bIdent = b.module ? b.module.identifier() : "";
  61. if (aIdent < bIdent) return -1;
  62. if (aIdent > bIdent) return 1;
  63. return compareLocations(a.loc, b.loc);
  64. };
  65. /**
  66. * Represents a connected group of chunks along with the parent/child
  67. * relationships, async blocks, and traversal metadata webpack tracks for it.
  68. */
  69. class ChunkGroup {
  70. /**
  71. * Creates a chunk group and initializes the relationship sets and ordering
  72. * metadata used while building and optimizing the chunk graph.
  73. * @param {string | ChunkGroupOptions=} options chunk group options passed to chunkGroup
  74. */
  75. constructor(options) {
  76. if (typeof options === "string") {
  77. options = { name: options };
  78. } else if (!options) {
  79. options = { name: undefined };
  80. }
  81. /** @type {number} */
  82. this.groupDebugId = debugId++;
  83. /** @type {ChunkGroupOptions} */
  84. this.options = options;
  85. /** @type {SortableSet<ChunkGroup>} */
  86. this._children = new SortableSet(undefined, sortById);
  87. /** @type {SortableSet<ChunkGroup>} */
  88. this._parents = new SortableSet(undefined, sortById);
  89. /** @type {SortableSet<ChunkGroup>} */
  90. this._asyncEntrypoints = new SortableSet(undefined, sortById);
  91. /** @type {SortableSet<AsyncDependenciesBlock>} */
  92. this._blocks = new SortableSet();
  93. /** @type {Chunk[]} */
  94. this.chunks = [];
  95. /** @type {OriginRecord[]} */
  96. this.origins = [];
  97. /** @typedef {Map<Module, number>} OrderIndices */
  98. /** Indices in top-down order */
  99. /**
  100. * @private
  101. * @type {OrderIndices}
  102. */
  103. this._modulePreOrderIndices = new Map();
  104. /** Indices in bottom-up order */
  105. /**
  106. * @private
  107. * @type {OrderIndices}
  108. */
  109. this._modulePostOrderIndices = new Map();
  110. /** @type {number | undefined} */
  111. this.index = undefined;
  112. }
  113. /**
  114. * Merges additional options into the chunk group.
  115. * Order-based options are combined by taking the higher priority, while
  116. * unsupported conflicts surface as an explicit error.
  117. * @param {ChunkGroupOptions} options the chunkGroup options passed to addOptions
  118. * @returns {void}
  119. */
  120. addOptions(options) {
  121. for (const key of /** @type {(keyof ChunkGroupOptions)[]} */ (
  122. Object.keys(options)
  123. )) {
  124. if (this.options[key] === undefined) {
  125. /** @type {ChunkGroupOptions[keyof ChunkGroupOptions]} */
  126. (this.options[key]) = options[key];
  127. } else if (this.options[key] !== options[key]) {
  128. if (key.endsWith("Order")) {
  129. const orderKey =
  130. /** @type {Exclude<keyof ChunkGroupOptions, "name" | "fetchPriority">} */
  131. (key);
  132. this.options[orderKey] = Math.max(
  133. /** @type {number} */
  134. (this.options[orderKey]),
  135. /** @type {number} */
  136. (options[orderKey])
  137. );
  138. } else {
  139. throw new Error(
  140. `ChunkGroup.addOptions: No option merge strategy for ${key}`
  141. );
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Returns the configured name of the chunk group, if one was assigned.
  148. * @returns {ChunkGroupOptions["name"]} returns the ChunkGroup name
  149. */
  150. get name() {
  151. return this.options.name;
  152. }
  153. /**
  154. * Updates the configured name of the chunk group.
  155. * @param {string | undefined} value the new name for ChunkGroup
  156. * @returns {void}
  157. */
  158. set name(value) {
  159. this.options.name = value;
  160. }
  161. /* istanbul ignore next */
  162. /**
  163. * Returns a debug-only identifier derived from the group's member chunk
  164. * debug ids. This is primarily useful in diagnostics and assertions.
  165. * @returns {string} a unique concatenation of chunk debugId's
  166. */
  167. get debugId() {
  168. return Array.from(this.chunks, (x) => x.debugId).join("+");
  169. }
  170. /**
  171. * Returns an identifier derived from the ids of the chunks currently in
  172. * the group.
  173. * @returns {string} a unique concatenation of chunk ids
  174. */
  175. get id() {
  176. return Array.from(this.chunks, (x) => x.id).join("+");
  177. }
  178. /**
  179. * Moves a chunk to the front of the group or inserts it when it is not
  180. * already present.
  181. * @param {Chunk} chunk chunk being unshifted
  182. * @returns {boolean} returns true if attempted chunk shift is accepted
  183. */
  184. unshiftChunk(chunk) {
  185. const oldIdx = this.chunks.indexOf(chunk);
  186. if (oldIdx > 0) {
  187. this.chunks.splice(oldIdx, 1);
  188. this.chunks.unshift(chunk);
  189. } else if (oldIdx < 0) {
  190. this.chunks.unshift(chunk);
  191. return true;
  192. }
  193. return false;
  194. }
  195. /**
  196. * Inserts a chunk directly before another chunk that already belongs to the
  197. * group, preserving the rest of the ordering.
  198. * @param {Chunk} chunk Chunk being inserted
  199. * @param {Chunk} before Placeholder/target chunk marking new chunk insertion point
  200. * @returns {boolean} return true if insertion was successful
  201. */
  202. insertChunk(chunk, before) {
  203. const oldIdx = this.chunks.indexOf(chunk);
  204. const idx = this.chunks.indexOf(before);
  205. if (idx < 0) {
  206. throw new Error("before chunk not found");
  207. }
  208. if (oldIdx >= 0 && oldIdx > idx) {
  209. this.chunks.splice(oldIdx, 1);
  210. this.chunks.splice(idx, 0, chunk);
  211. } else if (oldIdx < 0) {
  212. this.chunks.splice(idx, 0, chunk);
  213. return true;
  214. }
  215. return false;
  216. }
  217. /**
  218. * Appends a chunk to the group when it is not already a member.
  219. * @param {Chunk} chunk chunk being pushed into ChunkGroupS
  220. * @returns {boolean} returns true if chunk addition was successful.
  221. */
  222. pushChunk(chunk) {
  223. const oldIdx = this.chunks.indexOf(chunk);
  224. if (oldIdx >= 0) {
  225. return false;
  226. }
  227. this.chunks.push(chunk);
  228. return true;
  229. }
  230. /**
  231. * Replaces one member chunk with another while preserving the group's
  232. * ordering and avoiding duplicates.
  233. * @param {Chunk} oldChunk chunk to be replaced
  234. * @param {Chunk} newChunk New chunk that will be replaced with
  235. * @returns {boolean | undefined} returns true if the replacement was successful
  236. */
  237. replaceChunk(oldChunk, newChunk) {
  238. const oldIdx = this.chunks.indexOf(oldChunk);
  239. if (oldIdx < 0) return false;
  240. const newIdx = this.chunks.indexOf(newChunk);
  241. if (newIdx < 0) {
  242. this.chunks[oldIdx] = newChunk;
  243. return true;
  244. }
  245. if (newIdx < oldIdx) {
  246. this.chunks.splice(oldIdx, 1);
  247. return true;
  248. } else if (newIdx !== oldIdx) {
  249. this.chunks[oldIdx] = newChunk;
  250. this.chunks.splice(newIdx, 1);
  251. return true;
  252. }
  253. }
  254. /**
  255. * Removes a chunk from this group.
  256. * @param {Chunk} chunk chunk to remove
  257. * @returns {boolean} returns true if chunk was removed
  258. */
  259. removeChunk(chunk) {
  260. const idx = this.chunks.indexOf(chunk);
  261. if (idx >= 0) {
  262. this.chunks.splice(idx, 1);
  263. return true;
  264. }
  265. return false;
  266. }
  267. /**
  268. * Indicates whether this chunk group is loaded as part of the initial page
  269. * load instead of being created lazily.
  270. * @returns {boolean} true, when this chunk group will be loaded on initial page load
  271. */
  272. isInitial() {
  273. return false;
  274. }
  275. /**
  276. * Adds a child chunk group to the current group.
  277. * @param {ChunkGroup} group chunk group to add
  278. * @returns {boolean} returns true if chunk group was added
  279. */
  280. addChild(group) {
  281. const size = this._children.size;
  282. this._children.add(group);
  283. return size !== this._children.size;
  284. }
  285. /**
  286. * Returns the child chunk groups reachable from this group.
  287. * @returns {ChunkGroup[]} returns the children of this group
  288. */
  289. getChildren() {
  290. return this._children.getFromCache(getArray);
  291. }
  292. getNumberOfChildren() {
  293. return this._children.size;
  294. }
  295. get childrenIterable() {
  296. return this._children;
  297. }
  298. /**
  299. * Removes a child chunk group and clears the corresponding parent link on
  300. * the removed child.
  301. * @param {ChunkGroup} group the chunk group to remove
  302. * @returns {boolean} returns true if the chunk group was removed
  303. */
  304. removeChild(group) {
  305. if (!this._children.has(group)) {
  306. return false;
  307. }
  308. this._children.delete(group);
  309. group.removeParent(this);
  310. return true;
  311. }
  312. /**
  313. * Records a parent chunk group relationship.
  314. * @param {ChunkGroup} parentChunk the parent group to be added into
  315. * @returns {boolean} returns true if this chunk group was added to the parent group
  316. */
  317. addParent(parentChunk) {
  318. if (!this._parents.has(parentChunk)) {
  319. this._parents.add(parentChunk);
  320. return true;
  321. }
  322. return false;
  323. }
  324. /**
  325. * Returns the parent chunk groups that can lead to this group.
  326. * @returns {ChunkGroup[]} returns the parents of this group
  327. */
  328. getParents() {
  329. return this._parents.getFromCache(getArray);
  330. }
  331. getNumberOfParents() {
  332. return this._parents.size;
  333. }
  334. /**
  335. * Checks whether the provided group is registered as a parent.
  336. * @param {ChunkGroup} parent the parent group
  337. * @returns {boolean} returns true if the parent group contains this group
  338. */
  339. hasParent(parent) {
  340. return this._parents.has(parent);
  341. }
  342. get parentsIterable() {
  343. return this._parents;
  344. }
  345. /**
  346. * Removes a parent chunk group and clears the reverse child relationship.
  347. * @param {ChunkGroup} chunkGroup the parent group
  348. * @returns {boolean} returns true if this group has been removed from the parent
  349. */
  350. removeParent(chunkGroup) {
  351. if (this._parents.delete(chunkGroup)) {
  352. chunkGroup.removeChild(this);
  353. return true;
  354. }
  355. return false;
  356. }
  357. /**
  358. * Registers an async entrypoint that is rooted in this chunk group.
  359. * @param {Entrypoint} entrypoint entrypoint to add
  360. * @returns {boolean} returns true if entrypoint was added
  361. */
  362. addAsyncEntrypoint(entrypoint) {
  363. const size = this._asyncEntrypoints.size;
  364. this._asyncEntrypoints.add(entrypoint);
  365. return size !== this._asyncEntrypoints.size;
  366. }
  367. get asyncEntrypointsIterable() {
  368. return this._asyncEntrypoints;
  369. }
  370. /**
  371. * Returns the async dependency blocks that create or reference this group.
  372. * @returns {AsyncDependenciesBlock[]} an array containing the blocks
  373. */
  374. getBlocks() {
  375. return this._blocks.getFromCache(getArray);
  376. }
  377. getNumberOfBlocks() {
  378. return this._blocks.size;
  379. }
  380. /**
  381. * Checks whether an async dependency block is associated with this group.
  382. * @param {AsyncDependenciesBlock} block block
  383. * @returns {boolean} true, if block exists
  384. */
  385. hasBlock(block) {
  386. return this._blocks.has(block);
  387. }
  388. /**
  389. * Exposes the group's async dependency blocks as an iterable.
  390. * @returns {Iterable<AsyncDependenciesBlock>} blocks
  391. */
  392. get blocksIterable() {
  393. return this._blocks;
  394. }
  395. /**
  396. * Associates an async dependency block with this chunk group.
  397. * @param {AsyncDependenciesBlock} block a block
  398. * @returns {boolean} false, if block was already added
  399. */
  400. addBlock(block) {
  401. if (!this._blocks.has(block)) {
  402. this._blocks.add(block);
  403. return true;
  404. }
  405. return false;
  406. }
  407. /**
  408. * Records where this chunk group originated from in user code.
  409. * The origin is used for diagnostics, ordering, and reporting.
  410. * @param {Module | null} module origin module
  411. * @param {DependencyLocation} loc location of the reference in the origin module
  412. * @param {string} request request name of the reference
  413. * @returns {void}
  414. */
  415. addOrigin(module, loc, request) {
  416. this.origins.push({
  417. module,
  418. loc,
  419. request
  420. });
  421. }
  422. /**
  423. * Collects the emitted files produced by every chunk in the group.
  424. * @returns {string[]} the files contained this chunk group
  425. */
  426. getFiles() {
  427. /** @type {Set<string>} */
  428. const files = new Set();
  429. for (const chunk of this.chunks) {
  430. for (const file of chunk.files) {
  431. files.add(file);
  432. }
  433. }
  434. return [...files];
  435. }
  436. /**
  437. * Disconnects this group from its parents, children, and chunks.
  438. * Child groups are reconnected to this group's parents so the surrounding
  439. * graph remains intact after removal.
  440. * @returns {void}
  441. */
  442. remove() {
  443. // cleanup parents
  444. for (const parentChunkGroup of this._parents) {
  445. // remove this chunk from its parents
  446. parentChunkGroup._children.delete(this);
  447. // cleanup "sub chunks"
  448. for (const chunkGroup of this._children) {
  449. /**
  450. * remove this chunk as "intermediary" and connect
  451. * it "sub chunks" and parents directly
  452. */
  453. // add parent to each "sub chunk"
  454. chunkGroup.addParent(parentChunkGroup);
  455. // add "sub chunk" to parent
  456. parentChunkGroup.addChild(chunkGroup);
  457. }
  458. }
  459. /**
  460. * we need to iterate again over the children
  461. * to remove this from the child's parents.
  462. * This can not be done in the above loop
  463. * as it is not guaranteed that `this._parents` contains anything.
  464. */
  465. for (const chunkGroup of this._children) {
  466. // remove this as parent of every "sub chunk"
  467. chunkGroup._parents.delete(this);
  468. }
  469. // remove chunks
  470. for (const chunk of this.chunks) {
  471. chunk.removeGroup(this);
  472. }
  473. }
  474. sortItems() {
  475. this.origins.sort(sortOrigin);
  476. }
  477. /**
  478. * Sorting predicate which allows current ChunkGroup to be compared against another.
  479. * Sorting values are based off of number of chunks in ChunkGroup.
  480. * @param {ChunkGraph} chunkGraph the chunk graph
  481. * @param {ChunkGroup} otherGroup the chunkGroup to compare this against
  482. * @returns {-1 | 0 | 1} sort position for comparison
  483. */
  484. compareTo(chunkGraph, otherGroup) {
  485. if (this.chunks.length > otherGroup.chunks.length) return -1;
  486. if (this.chunks.length < otherGroup.chunks.length) return 1;
  487. return compareIterables(compareChunks(chunkGraph))(
  488. this.chunks,
  489. otherGroup.chunks
  490. );
  491. }
  492. /**
  493. * Groups child chunk groups by their `*Order` options and sorts each group
  494. * by descending order and deterministic chunk-group comparison.
  495. * @param {ModuleGraph} moduleGraph the module graph
  496. * @param {ChunkGraph} chunkGraph the chunk graph
  497. * @returns {Record<string, ChunkGroup[]>} mapping from children type to ordered list of ChunkGroups
  498. */
  499. getChildrenByOrders(moduleGraph, chunkGraph) {
  500. /** @type {Map<string, { order: number, group: ChunkGroup }[]>} */
  501. const lists = new Map();
  502. for (const childGroup of this._children) {
  503. for (const key of Object.keys(childGroup.options)) {
  504. if (key.endsWith("Order")) {
  505. const name = key.slice(0, key.length - "Order".length);
  506. let list = lists.get(name);
  507. if (list === undefined) {
  508. lists.set(name, (list = []));
  509. }
  510. list.push({
  511. order:
  512. /** @type {number} */
  513. (
  514. childGroup.options[/** @type {keyof ChunkGroupOptions} */ (key)]
  515. ),
  516. group: childGroup
  517. });
  518. }
  519. }
  520. }
  521. /** @type {Record<string, ChunkGroup[]>} */
  522. const result = Object.create(null);
  523. for (const [name, list] of lists) {
  524. list.sort((a, b) => {
  525. const cmp = b.order - a.order;
  526. if (cmp !== 0) return cmp;
  527. return a.group.compareTo(chunkGraph, b.group);
  528. });
  529. result[name] = list.map((i) => i.group);
  530. }
  531. return result;
  532. }
  533. /**
  534. * Stores the module's top-down traversal index within this group.
  535. * @param {Module} module module for which the index should be set
  536. * @param {number} index the index of the module
  537. * @returns {void}
  538. */
  539. setModulePreOrderIndex(module, index) {
  540. this._modulePreOrderIndices.set(module, index);
  541. }
  542. /**
  543. * Returns the module's top-down traversal index within this group.
  544. * @param {Module} module the module
  545. * @returns {number | undefined} index
  546. */
  547. getModulePreOrderIndex(module) {
  548. return this._modulePreOrderIndices.get(module);
  549. }
  550. /**
  551. * Stores the module's bottom-up traversal index within this group.
  552. * @param {Module} module module for which the index should be set
  553. * @param {number} index the index of the module
  554. * @returns {void}
  555. */
  556. setModulePostOrderIndex(module, index) {
  557. this._modulePostOrderIndices.set(module, index);
  558. }
  559. /**
  560. * Returns the module's bottom-up traversal index within this group.
  561. * @param {Module} module the module
  562. * @returns {number | undefined} index
  563. */
  564. getModulePostOrderIndex(module) {
  565. return this._modulePostOrderIndices.get(module);
  566. }
  567. /* istanbul ignore next */
  568. checkConstraints() {
  569. const chunk = this;
  570. for (const child of chunk._children) {
  571. if (!child._parents.has(chunk)) {
  572. throw new Error(
  573. `checkConstraints: child missing parent ${chunk.debugId} -> ${child.debugId}`
  574. );
  575. }
  576. }
  577. for (const parentChunk of chunk._parents) {
  578. if (!parentChunk._children.has(chunk)) {
  579. throw new Error(
  580. `checkConstraints: parent missing child ${parentChunk.debugId} <- ${chunk.debugId}`
  581. );
  582. }
  583. }
  584. }
  585. }
  586. ChunkGroup.prototype.getModuleIndex = util.deprecate(
  587. ChunkGroup.prototype.getModulePreOrderIndex,
  588. "ChunkGroup.getModuleIndex was renamed to getModulePreOrderIndex",
  589. "DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX"
  590. );
  591. ChunkGroup.prototype.getModuleIndex2 = util.deprecate(
  592. ChunkGroup.prototype.getModulePostOrderIndex,
  593. "ChunkGroup.getModuleIndex2 was renamed to getModulePostOrderIndex",
  594. "DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX_2"
  595. );
  596. module.exports = ChunkGroup;