ChunkGroup.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. compareLocations,
  10. compareChunks,
  11. compareIterables
  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 {{id: number}} HasId */
  21. /** @typedef {{module: Module, loc: DependencyLocation, request: string}} OriginRecord */
  22. /**
  23. * @typedef {Object} RawChunkGroupOptions
  24. * @property {number=} preloadOrder
  25. * @property {number=} prefetchOrder
  26. * @property {("low" | "high" | "auto")=} fetchPriority
  27. */
  28. /** @typedef {RawChunkGroupOptions & { name?: string }} ChunkGroupOptions */
  29. let debugId = 5000;
  30. /**
  31. * @template T
  32. * @param {SortableSet<T>} set set to convert to array.
  33. * @returns {T[]} the array format of existing set
  34. */
  35. const getArray = set => Array.from(set);
  36. /**
  37. * A convenience method used to sort chunks based on their id's
  38. * @param {ChunkGroup} a first sorting comparator
  39. * @param {ChunkGroup} b second sorting comparator
  40. * @returns {1|0|-1} a sorting index to determine order
  41. */
  42. const sortById = (a, b) => {
  43. if (a.id < b.id) return -1;
  44. if (b.id < a.id) return 1;
  45. return 0;
  46. };
  47. /**
  48. * @param {OriginRecord} a the first comparator in sort
  49. * @param {OriginRecord} b the second comparator in sort
  50. * @returns {1|-1|0} returns sorting order as index
  51. */
  52. const sortOrigin = (a, b) => {
  53. const aIdent = a.module ? a.module.identifier() : "";
  54. const bIdent = b.module ? b.module.identifier() : "";
  55. if (aIdent < bIdent) return -1;
  56. if (aIdent > bIdent) return 1;
  57. return compareLocations(a.loc, b.loc);
  58. };
  59. class ChunkGroup {
  60. /**
  61. * Creates an instance of ChunkGroup.
  62. * @param {string|ChunkGroupOptions=} options chunk group options passed to chunkGroup
  63. */
  64. constructor(options) {
  65. if (typeof options === "string") {
  66. options = { name: options };
  67. } else if (!options) {
  68. options = { name: undefined };
  69. }
  70. /** @type {number} */
  71. this.groupDebugId = debugId++;
  72. this.options = options;
  73. /** @type {SortableSet<ChunkGroup>} */
  74. this._children = new SortableSet(undefined, sortById);
  75. /** @type {SortableSet<ChunkGroup>} */
  76. this._parents = new SortableSet(undefined, sortById);
  77. /** @type {SortableSet<ChunkGroup>} */
  78. this._asyncEntrypoints = new SortableSet(undefined, sortById);
  79. this._blocks = new SortableSet();
  80. /** @type {Chunk[]} */
  81. this.chunks = [];
  82. /** @type {OriginRecord[]} */
  83. this.origins = [];
  84. /** Indices in top-down order */
  85. /** @private @type {Map<Module, number>} */
  86. this._modulePreOrderIndices = new Map();
  87. /** Indices in bottom-up order */
  88. /** @private @type {Map<Module, number>} */
  89. this._modulePostOrderIndices = new Map();
  90. /** @type {number | undefined} */
  91. this.index = undefined;
  92. }
  93. /**
  94. * when a new chunk is added to a chunkGroup, addingOptions will occur.
  95. * @param {ChunkGroupOptions} options the chunkGroup options passed to addOptions
  96. * @returns {void}
  97. */
  98. addOptions(options) {
  99. for (const key of Object.keys(options)) {
  100. if (
  101. this.options[/** @type {keyof ChunkGroupOptions} */ (key)] === undefined
  102. ) {
  103. this.options[key] =
  104. options[/** @type {keyof ChunkGroupOptions} */ (key)];
  105. } else if (
  106. this.options[/** @type {keyof ChunkGroupOptions} */ (key)] !==
  107. options[/** @type {keyof ChunkGroupOptions} */ (key)]
  108. ) {
  109. if (key.endsWith("Order")) {
  110. this.options[key] = Math.max(this.options[key], options[key]);
  111. } else {
  112. throw new Error(
  113. `ChunkGroup.addOptions: No option merge strategy for ${key}`
  114. );
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * returns the name of current ChunkGroup
  121. * @returns {string | undefined} returns the ChunkGroup name
  122. */
  123. get name() {
  124. return this.options.name;
  125. }
  126. /**
  127. * sets a new name for current ChunkGroup
  128. * @param {string | undefined} value the new name for ChunkGroup
  129. * @returns {void}
  130. */
  131. set name(value) {
  132. this.options.name = value;
  133. }
  134. /* istanbul ignore next */
  135. /**
  136. * get a uniqueId for ChunkGroup, made up of its member Chunk debugId's
  137. * @returns {string} a unique concatenation of chunk debugId's
  138. */
  139. get debugId() {
  140. return Array.from(this.chunks, x => x.debugId).join("+");
  141. }
  142. /**
  143. * get a unique id for ChunkGroup, made up of its member Chunk id's
  144. * @returns {string} a unique concatenation of chunk ids
  145. */
  146. get id() {
  147. return Array.from(this.chunks, x => x.id).join("+");
  148. }
  149. /**
  150. * Performs an unshift of a specific chunk
  151. * @param {Chunk} chunk chunk being unshifted
  152. * @returns {boolean} returns true if attempted chunk shift is accepted
  153. */
  154. unshiftChunk(chunk) {
  155. const oldIdx = this.chunks.indexOf(chunk);
  156. if (oldIdx > 0) {
  157. this.chunks.splice(oldIdx, 1);
  158. this.chunks.unshift(chunk);
  159. } else if (oldIdx < 0) {
  160. this.chunks.unshift(chunk);
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * inserts a chunk before another existing chunk in group
  167. * @param {Chunk} chunk Chunk being inserted
  168. * @param {Chunk} before Placeholder/target chunk marking new chunk insertion point
  169. * @returns {boolean} return true if insertion was successful
  170. */
  171. insertChunk(chunk, before) {
  172. const oldIdx = this.chunks.indexOf(chunk);
  173. const idx = this.chunks.indexOf(before);
  174. if (idx < 0) {
  175. throw new Error("before chunk not found");
  176. }
  177. if (oldIdx >= 0 && oldIdx > idx) {
  178. this.chunks.splice(oldIdx, 1);
  179. this.chunks.splice(idx, 0, chunk);
  180. } else if (oldIdx < 0) {
  181. this.chunks.splice(idx, 0, chunk);
  182. return true;
  183. }
  184. return false;
  185. }
  186. /**
  187. * add a chunk into ChunkGroup. Is pushed on or prepended
  188. * @param {Chunk} chunk chunk being pushed into ChunkGroupS
  189. * @returns {boolean} returns true if chunk addition was successful.
  190. */
  191. pushChunk(chunk) {
  192. const oldIdx = this.chunks.indexOf(chunk);
  193. if (oldIdx >= 0) {
  194. return false;
  195. }
  196. this.chunks.push(chunk);
  197. return true;
  198. }
  199. /**
  200. * @param {Chunk} oldChunk chunk to be replaced
  201. * @param {Chunk} newChunk New chunk that will be replaced with
  202. * @returns {boolean} returns true if the replacement was successful
  203. */
  204. replaceChunk(oldChunk, newChunk) {
  205. const oldIdx = this.chunks.indexOf(oldChunk);
  206. if (oldIdx < 0) return false;
  207. const newIdx = this.chunks.indexOf(newChunk);
  208. if (newIdx < 0) {
  209. this.chunks[oldIdx] = newChunk;
  210. return true;
  211. }
  212. if (newIdx < oldIdx) {
  213. this.chunks.splice(oldIdx, 1);
  214. return true;
  215. } else if (newIdx !== oldIdx) {
  216. this.chunks[oldIdx] = newChunk;
  217. this.chunks.splice(newIdx, 1);
  218. return true;
  219. }
  220. }
  221. /**
  222. * @param {Chunk} chunk chunk to remove
  223. * @returns {boolean} returns true if chunk was removed
  224. */
  225. removeChunk(chunk) {
  226. const idx = this.chunks.indexOf(chunk);
  227. if (idx >= 0) {
  228. this.chunks.splice(idx, 1);
  229. return true;
  230. }
  231. return false;
  232. }
  233. /**
  234. * @returns {boolean} true, when this chunk group will be loaded on initial page load
  235. */
  236. isInitial() {
  237. return false;
  238. }
  239. /**
  240. * @param {ChunkGroup} group chunk group to add
  241. * @returns {boolean} returns true if chunk group was added
  242. */
  243. addChild(group) {
  244. const size = this._children.size;
  245. this._children.add(group);
  246. return size !== this._children.size;
  247. }
  248. /**
  249. * @returns {ChunkGroup[]} returns the children of this group
  250. */
  251. getChildren() {
  252. return this._children.getFromCache(getArray);
  253. }
  254. getNumberOfChildren() {
  255. return this._children.size;
  256. }
  257. get childrenIterable() {
  258. return this._children;
  259. }
  260. /**
  261. * @param {ChunkGroup} group the chunk group to remove
  262. * @returns {boolean} returns true if the chunk group was removed
  263. */
  264. removeChild(group) {
  265. if (!this._children.has(group)) {
  266. return false;
  267. }
  268. this._children.delete(group);
  269. group.removeParent(this);
  270. return true;
  271. }
  272. /**
  273. * @param {ChunkGroup} parentChunk the parent group to be added into
  274. * @returns {boolean} returns true if this chunk group was added to the parent group
  275. */
  276. addParent(parentChunk) {
  277. if (!this._parents.has(parentChunk)) {
  278. this._parents.add(parentChunk);
  279. return true;
  280. }
  281. return false;
  282. }
  283. /**
  284. * @returns {ChunkGroup[]} returns the parents of this group
  285. */
  286. getParents() {
  287. return this._parents.getFromCache(getArray);
  288. }
  289. getNumberOfParents() {
  290. return this._parents.size;
  291. }
  292. /**
  293. * @param {ChunkGroup} parent the parent group
  294. * @returns {boolean} returns true if the parent group contains this group
  295. */
  296. hasParent(parent) {
  297. return this._parents.has(parent);
  298. }
  299. get parentsIterable() {
  300. return this._parents;
  301. }
  302. /**
  303. * @param {ChunkGroup} chunkGroup the parent group
  304. * @returns {boolean} returns true if this group has been removed from the parent
  305. */
  306. removeParent(chunkGroup) {
  307. if (this._parents.delete(chunkGroup)) {
  308. chunkGroup.removeChild(this);
  309. return true;
  310. }
  311. return false;
  312. }
  313. /**
  314. * @param {Entrypoint} entrypoint entrypoint to add
  315. * @returns {boolean} returns true if entrypoint was added
  316. */
  317. addAsyncEntrypoint(entrypoint) {
  318. const size = this._asyncEntrypoints.size;
  319. this._asyncEntrypoints.add(entrypoint);
  320. return size !== this._asyncEntrypoints.size;
  321. }
  322. get asyncEntrypointsIterable() {
  323. return this._asyncEntrypoints;
  324. }
  325. /**
  326. * @returns {Array<AsyncDependenciesBlock>} an array containing the blocks
  327. */
  328. getBlocks() {
  329. return this._blocks.getFromCache(getArray);
  330. }
  331. getNumberOfBlocks() {
  332. return this._blocks.size;
  333. }
  334. /**
  335. * @param {AsyncDependenciesBlock} block block
  336. * @returns {boolean} true, if block exists
  337. */
  338. hasBlock(block) {
  339. return this._blocks.has(block);
  340. }
  341. /**
  342. * @returns {Iterable<AsyncDependenciesBlock>} blocks
  343. */
  344. get blocksIterable() {
  345. return this._blocks;
  346. }
  347. /**
  348. * @param {AsyncDependenciesBlock} block a block
  349. * @returns {boolean} false, if block was already added
  350. */
  351. addBlock(block) {
  352. if (!this._blocks.has(block)) {
  353. this._blocks.add(block);
  354. return true;
  355. }
  356. return false;
  357. }
  358. /**
  359. * @param {Module} module origin module
  360. * @param {DependencyLocation} loc location of the reference in the origin module
  361. * @param {string} request request name of the reference
  362. * @returns {void}
  363. */
  364. addOrigin(module, loc, request) {
  365. this.origins.push({
  366. module,
  367. loc,
  368. request
  369. });
  370. }
  371. /**
  372. * @returns {string[]} the files contained this chunk group
  373. */
  374. getFiles() {
  375. const files = new Set();
  376. for (const chunk of this.chunks) {
  377. for (const file of chunk.files) {
  378. files.add(file);
  379. }
  380. }
  381. return Array.from(files);
  382. }
  383. /**
  384. * @returns {void}
  385. */
  386. remove() {
  387. // cleanup parents
  388. for (const parentChunkGroup of this._parents) {
  389. // remove this chunk from its parents
  390. parentChunkGroup._children.delete(this);
  391. // cleanup "sub chunks"
  392. for (const chunkGroup of this._children) {
  393. /**
  394. * remove this chunk as "intermediary" and connect
  395. * it "sub chunks" and parents directly
  396. */
  397. // add parent to each "sub chunk"
  398. chunkGroup.addParent(parentChunkGroup);
  399. // add "sub chunk" to parent
  400. parentChunkGroup.addChild(chunkGroup);
  401. }
  402. }
  403. /**
  404. * we need to iterate again over the children
  405. * to remove this from the child's parents.
  406. * This can not be done in the above loop
  407. * as it is not guaranteed that `this._parents` contains anything.
  408. */
  409. for (const chunkGroup of this._children) {
  410. // remove this as parent of every "sub chunk"
  411. chunkGroup._parents.delete(this);
  412. }
  413. // remove chunks
  414. for (const chunk of this.chunks) {
  415. chunk.removeGroup(this);
  416. }
  417. }
  418. sortItems() {
  419. this.origins.sort(sortOrigin);
  420. }
  421. /**
  422. * Sorting predicate which allows current ChunkGroup to be compared against another.
  423. * Sorting values are based off of number of chunks in ChunkGroup.
  424. *
  425. * @param {ChunkGraph} chunkGraph the chunk graph
  426. * @param {ChunkGroup} otherGroup the chunkGroup to compare this against
  427. * @returns {-1|0|1} sort position for comparison
  428. */
  429. compareTo(chunkGraph, otherGroup) {
  430. if (this.chunks.length > otherGroup.chunks.length) return -1;
  431. if (this.chunks.length < otherGroup.chunks.length) return 1;
  432. return compareIterables(compareChunks(chunkGraph))(
  433. this.chunks,
  434. otherGroup.chunks
  435. );
  436. }
  437. /**
  438. * @param {ModuleGraph} moduleGraph the module graph
  439. * @param {ChunkGraph} chunkGraph the chunk graph
  440. * @returns {Record<string, ChunkGroup[]>} mapping from children type to ordered list of ChunkGroups
  441. */
  442. getChildrenByOrders(moduleGraph, chunkGraph) {
  443. /** @type {Map<string, {order: number, group: ChunkGroup}[]>} */
  444. const lists = new Map();
  445. for (const childGroup of this._children) {
  446. for (const key of Object.keys(childGroup.options)) {
  447. if (key.endsWith("Order")) {
  448. const name = key.slice(0, key.length - "Order".length);
  449. let list = lists.get(name);
  450. if (list === undefined) {
  451. lists.set(name, (list = []));
  452. }
  453. list.push({
  454. order:
  455. /** @type {number} */
  456. (
  457. childGroup.options[/** @type {keyof ChunkGroupOptions} */ (key)]
  458. ),
  459. group: childGroup
  460. });
  461. }
  462. }
  463. }
  464. /** @type {Record<string, ChunkGroup[]>} */
  465. const result = Object.create(null);
  466. for (const [name, list] of lists) {
  467. list.sort((a, b) => {
  468. const cmp = b.order - a.order;
  469. if (cmp !== 0) return cmp;
  470. return a.group.compareTo(chunkGraph, b.group);
  471. });
  472. result[name] = list.map(i => i.group);
  473. }
  474. return result;
  475. }
  476. /**
  477. * Sets the top-down index of a module in this ChunkGroup
  478. * @param {Module} module module for which the index should be set
  479. * @param {number} index the index of the module
  480. * @returns {void}
  481. */
  482. setModulePreOrderIndex(module, index) {
  483. this._modulePreOrderIndices.set(module, index);
  484. }
  485. /**
  486. * Gets the top-down index of a module in this ChunkGroup
  487. * @param {Module} module the module
  488. * @returns {number | undefined} index
  489. */
  490. getModulePreOrderIndex(module) {
  491. return this._modulePreOrderIndices.get(module);
  492. }
  493. /**
  494. * Sets the bottom-up index of a module in this ChunkGroup
  495. * @param {Module} module module for which the index should be set
  496. * @param {number} index the index of the module
  497. * @returns {void}
  498. */
  499. setModulePostOrderIndex(module, index) {
  500. this._modulePostOrderIndices.set(module, index);
  501. }
  502. /**
  503. * Gets the bottom-up index of a module in this ChunkGroup
  504. * @param {Module} module the module
  505. * @returns {number | undefined} index
  506. */
  507. getModulePostOrderIndex(module) {
  508. return this._modulePostOrderIndices.get(module);
  509. }
  510. /* istanbul ignore next */
  511. checkConstraints() {
  512. const chunk = this;
  513. for (const child of chunk._children) {
  514. if (!child._parents.has(chunk)) {
  515. throw new Error(
  516. `checkConstraints: child missing parent ${chunk.debugId} -> ${child.debugId}`
  517. );
  518. }
  519. }
  520. for (const parentChunk of chunk._parents) {
  521. if (!parentChunk._children.has(chunk)) {
  522. throw new Error(
  523. `checkConstraints: parent missing child ${parentChunk.debugId} <- ${chunk.debugId}`
  524. );
  525. }
  526. }
  527. }
  528. }
  529. ChunkGroup.prototype.getModuleIndex = util.deprecate(
  530. ChunkGroup.prototype.getModulePreOrderIndex,
  531. "ChunkGroup.getModuleIndex was renamed to getModulePreOrderIndex",
  532. "DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX"
  533. );
  534. ChunkGroup.prototype.getModuleIndex2 = util.deprecate(
  535. ChunkGroup.prototype.getModulePostOrderIndex,
  536. "ChunkGroup.getModuleIndex2 was renamed to getModulePostOrderIndex",
  537. "DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX_2"
  538. );
  539. module.exports = ChunkGroup;