SplitChunksPlugin.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const SortableSet = require("../util/SortableSet");
  7. const GraphHelpers = require("../GraphHelpers");
  8. const { isSubset } = require("../util/SetHelpers");
  9. const deterministicGrouping = require("../util/deterministicGrouping");
  10. const MinMaxSizeWarning = require("./MinMaxSizeWarning");
  11. const contextify = require("../util/identifier").contextify;
  12. const createHash = require("../util/createHash");
  13. /** @typedef {import("../Compiler")} Compiler */
  14. /** @typedef {import("../Chunk")} Chunk */
  15. /** @typedef {import("../Module")} Module */
  16. /** @typedef {import("../util/deterministicGrouping").Options<Module>} DeterministicGroupingOptionsForModule */
  17. /** @typedef {import("../util/deterministicGrouping").GroupedItems<Module>} DeterministicGroupingGroupedItemsForModule */
  18. const deterministicGroupingForModules = /** @type {function(DeterministicGroupingOptionsForModule): DeterministicGroupingGroupedItemsForModule[]} */ (deterministicGrouping);
  19. const hashFilename = name => {
  20. return createHash("md4")
  21. .update(name)
  22. .digest("hex")
  23. .slice(0, 8);
  24. };
  25. const sortByIdentifier = (a, b) => {
  26. if (a.identifier() > b.identifier()) return 1;
  27. if (a.identifier() < b.identifier()) return -1;
  28. return 0;
  29. };
  30. const getRequests = chunk => {
  31. let requests = 0;
  32. for (const chunkGroup of chunk.groupsIterable) {
  33. requests = Math.max(requests, chunkGroup.chunks.length);
  34. }
  35. return requests;
  36. };
  37. const getModulesSize = modules => {
  38. let sum = 0;
  39. for (const m of modules) {
  40. sum += m.size();
  41. }
  42. return sum;
  43. };
  44. /**
  45. * @template T
  46. * @param {Set<T>} a set
  47. * @param {Set<T>} b other set
  48. * @returns {boolean} true if at least one item of a is in b
  49. */
  50. const isOverlap = (a, b) => {
  51. for (const item of a) {
  52. if (b.has(item)) return true;
  53. }
  54. return false;
  55. };
  56. const compareEntries = (a, b) => {
  57. // 1. by priority
  58. const diffPriority = a.cacheGroup.priority - b.cacheGroup.priority;
  59. if (diffPriority) return diffPriority;
  60. // 2. by number of chunks
  61. const diffCount = a.chunks.size - b.chunks.size;
  62. if (diffCount) return diffCount;
  63. // 3. by size reduction
  64. const aSizeReduce = a.size * (a.chunks.size - 1);
  65. const bSizeReduce = b.size * (b.chunks.size - 1);
  66. const diffSizeReduce = aSizeReduce - bSizeReduce;
  67. if (diffSizeReduce) return diffSizeReduce;
  68. // 4. by cache group index
  69. const indexDiff = b.cacheGroupIndex - a.cacheGroupIndex;
  70. if (indexDiff) return indexDiff;
  71. // 5. by number of modules (to be able to compare by identifier)
  72. const modulesA = a.modules;
  73. const modulesB = b.modules;
  74. const diff = modulesA.size - modulesB.size;
  75. if (diff) return diff;
  76. // 6. by module identifiers
  77. modulesA.sort();
  78. modulesB.sort();
  79. const aI = modulesA[Symbol.iterator]();
  80. const bI = modulesB[Symbol.iterator]();
  81. // eslint-disable-next-line no-constant-condition
  82. while (true) {
  83. const aItem = aI.next();
  84. const bItem = bI.next();
  85. if (aItem.done) return 0;
  86. const aModuleIdentifier = aItem.value.identifier();
  87. const bModuleIdentifier = bItem.value.identifier();
  88. if (aModuleIdentifier > bModuleIdentifier) return -1;
  89. if (aModuleIdentifier < bModuleIdentifier) return 1;
  90. }
  91. };
  92. const compareNumbers = (a, b) => a - b;
  93. const INITIAL_CHUNK_FILTER = chunk => chunk.canBeInitial();
  94. const ASYNC_CHUNK_FILTER = chunk => !chunk.canBeInitial();
  95. const ALL_CHUNK_FILTER = chunk => true;
  96. module.exports = class SplitChunksPlugin {
  97. constructor(options) {
  98. this.options = SplitChunksPlugin.normalizeOptions(options);
  99. }
  100. static normalizeOptions(options = {}) {
  101. return {
  102. chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
  103. options.chunks || "all"
  104. ),
  105. minSize: options.minSize || 0,
  106. enforceSizeThreshold: options.enforceSizeThreshold || 0,
  107. maxSize: options.maxSize || 0,
  108. minChunks: options.minChunks || 1,
  109. maxAsyncRequests: options.maxAsyncRequests || 1,
  110. maxInitialRequests: options.maxInitialRequests || 1,
  111. hidePathInfo: options.hidePathInfo || false,
  112. filename: options.filename || undefined,
  113. getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({
  114. cacheGroups: options.cacheGroups,
  115. name: options.name,
  116. automaticNameDelimiter: options.automaticNameDelimiter,
  117. automaticNameMaxLength: options.automaticNameMaxLength
  118. }),
  119. automaticNameDelimiter: options.automaticNameDelimiter,
  120. automaticNameMaxLength: options.automaticNameMaxLength || 109,
  121. fallbackCacheGroup: SplitChunksPlugin.normalizeFallbackCacheGroup(
  122. options.fallbackCacheGroup || {},
  123. options
  124. )
  125. };
  126. }
  127. static normalizeName({
  128. name,
  129. automaticNameDelimiter,
  130. automaticNamePrefix,
  131. automaticNameMaxLength
  132. }) {
  133. if (name === true) {
  134. /** @type {WeakMap<Chunk[], Record<string, string>>} */
  135. const cache = new WeakMap();
  136. const fn = (module, chunks, cacheGroup) => {
  137. let cacheEntry = cache.get(chunks);
  138. if (cacheEntry === undefined) {
  139. cacheEntry = {};
  140. cache.set(chunks, cacheEntry);
  141. } else if (cacheGroup in cacheEntry) {
  142. return cacheEntry[cacheGroup];
  143. }
  144. const names = chunks.map(c => c.name);
  145. if (!names.every(Boolean)) {
  146. cacheEntry[cacheGroup] = undefined;
  147. return;
  148. }
  149. names.sort();
  150. const prefix =
  151. typeof automaticNamePrefix === "string"
  152. ? automaticNamePrefix
  153. : cacheGroup;
  154. const namePrefix = prefix ? prefix + automaticNameDelimiter : "";
  155. let name = namePrefix + names.join(automaticNameDelimiter);
  156. // Filenames and paths can't be too long otherwise an
  157. // ENAMETOOLONG error is raised. If the generated name if too
  158. // long, it is truncated and a hash is appended. The limit has
  159. // been set to 109 to prevent `[name].[chunkhash].[ext]` from
  160. // generating a 256+ character string.
  161. if (name.length > automaticNameMaxLength) {
  162. const hashedFilename = hashFilename(name);
  163. const sliceLength =
  164. automaticNameMaxLength -
  165. (automaticNameDelimiter.length + hashedFilename.length);
  166. name =
  167. name.slice(0, sliceLength) +
  168. automaticNameDelimiter +
  169. hashedFilename;
  170. }
  171. cacheEntry[cacheGroup] = name;
  172. return name;
  173. };
  174. return fn;
  175. }
  176. if (typeof name === "string") {
  177. const fn = () => {
  178. return name;
  179. };
  180. return fn;
  181. }
  182. if (typeof name === "function") return name;
  183. }
  184. static normalizeChunksFilter(chunks) {
  185. if (chunks === "initial") {
  186. return INITIAL_CHUNK_FILTER;
  187. }
  188. if (chunks === "async") {
  189. return ASYNC_CHUNK_FILTER;
  190. }
  191. if (chunks === "all") {
  192. return ALL_CHUNK_FILTER;
  193. }
  194. if (typeof chunks === "function") return chunks;
  195. }
  196. static normalizeFallbackCacheGroup(
  197. {
  198. minSize = undefined,
  199. maxSize = undefined,
  200. automaticNameDelimiter = undefined
  201. },
  202. {
  203. minSize: defaultMinSize = undefined,
  204. maxSize: defaultMaxSize = undefined,
  205. automaticNameDelimiter: defaultAutomaticNameDelimiter = undefined
  206. }
  207. ) {
  208. return {
  209. minSize: typeof minSize === "number" ? minSize : defaultMinSize || 0,
  210. maxSize: typeof maxSize === "number" ? maxSize : defaultMaxSize || 0,
  211. automaticNameDelimiter:
  212. automaticNameDelimiter || defaultAutomaticNameDelimiter || "~"
  213. };
  214. }
  215. static normalizeCacheGroups({
  216. cacheGroups,
  217. name,
  218. automaticNameDelimiter,
  219. automaticNameMaxLength
  220. }) {
  221. if (typeof cacheGroups === "function") {
  222. // TODO webpack 5 remove this
  223. if (cacheGroups.length !== 1) {
  224. return module => cacheGroups(module, module.getChunks());
  225. }
  226. return cacheGroups;
  227. }
  228. if (cacheGroups && typeof cacheGroups === "object") {
  229. const fn = module => {
  230. let results;
  231. for (const key of Object.keys(cacheGroups)) {
  232. let option = cacheGroups[key];
  233. if (option === false) continue;
  234. if (option instanceof RegExp || typeof option === "string") {
  235. option = {
  236. test: option
  237. };
  238. }
  239. if (typeof option === "function") {
  240. let result = option(module);
  241. if (result) {
  242. if (results === undefined) results = [];
  243. for (const r of Array.isArray(result) ? result : [result]) {
  244. const result = Object.assign({ key }, r);
  245. if (result.name) result.getName = () => result.name;
  246. if (result.chunks) {
  247. result.chunksFilter = SplitChunksPlugin.normalizeChunksFilter(
  248. result.chunks
  249. );
  250. }
  251. results.push(result);
  252. }
  253. }
  254. } else if (SplitChunksPlugin.checkTest(option.test, module)) {
  255. if (results === undefined) results = [];
  256. results.push({
  257. key: key,
  258. priority: option.priority,
  259. getName:
  260. SplitChunksPlugin.normalizeName({
  261. name: option.name || name,
  262. automaticNameDelimiter:
  263. typeof option.automaticNameDelimiter === "string"
  264. ? option.automaticNameDelimiter
  265. : automaticNameDelimiter,
  266. automaticNamePrefix: option.automaticNamePrefix,
  267. automaticNameMaxLength:
  268. option.automaticNameMaxLength || automaticNameMaxLength
  269. }) || (() => {}),
  270. chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
  271. option.chunks
  272. ),
  273. enforce: option.enforce,
  274. minSize: option.minSize,
  275. enforceSizeThreshold: option.enforceSizeThreshold,
  276. maxSize: option.maxSize,
  277. minChunks: option.minChunks,
  278. maxAsyncRequests: option.maxAsyncRequests,
  279. maxInitialRequests: option.maxInitialRequests,
  280. filename: option.filename,
  281. reuseExistingChunk: option.reuseExistingChunk
  282. });
  283. }
  284. }
  285. return results;
  286. };
  287. return fn;
  288. }
  289. const fn = () => {};
  290. return fn;
  291. }
  292. static checkTest(test, module) {
  293. if (test === undefined) return true;
  294. if (typeof test === "function") {
  295. if (test.length !== 1) {
  296. return test(module, module.getChunks());
  297. }
  298. return test(module);
  299. }
  300. if (typeof test === "boolean") return test;
  301. if (typeof test === "string") {
  302. if (
  303. module.nameForCondition &&
  304. module.nameForCondition().startsWith(test)
  305. ) {
  306. return true;
  307. }
  308. for (const chunk of module.chunksIterable) {
  309. if (chunk.name && chunk.name.startsWith(test)) {
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. if (test instanceof RegExp) {
  316. if (module.nameForCondition && test.test(module.nameForCondition())) {
  317. return true;
  318. }
  319. for (const chunk of module.chunksIterable) {
  320. if (chunk.name && test.test(chunk.name)) {
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. return false;
  327. }
  328. /**
  329. * @param {Compiler} compiler webpack compiler
  330. * @returns {void}
  331. */
  332. apply(compiler) {
  333. compiler.hooks.thisCompilation.tap("SplitChunksPlugin", compilation => {
  334. let alreadyOptimized = false;
  335. compilation.hooks.unseal.tap("SplitChunksPlugin", () => {
  336. alreadyOptimized = false;
  337. });
  338. compilation.hooks.optimizeChunksAdvanced.tap(
  339. "SplitChunksPlugin",
  340. chunks => {
  341. if (alreadyOptimized) return;
  342. alreadyOptimized = true;
  343. // Give each selected chunk an index (to create strings from chunks)
  344. const indexMap = new Map();
  345. let index = 1;
  346. for (const chunk of chunks) {
  347. indexMap.set(chunk, index++);
  348. }
  349. const getKey = chunks => {
  350. return Array.from(chunks, c => indexMap.get(c))
  351. .sort(compareNumbers)
  352. .join();
  353. };
  354. /** @type {Map<string, Set<Chunk>>} */
  355. const chunkSetsInGraph = new Map();
  356. for (const module of compilation.modules) {
  357. const chunksKey = getKey(module.chunksIterable);
  358. if (!chunkSetsInGraph.has(chunksKey)) {
  359. chunkSetsInGraph.set(chunksKey, new Set(module.chunksIterable));
  360. }
  361. }
  362. // group these set of chunks by count
  363. // to allow to check less sets via isSubset
  364. // (only smaller sets can be subset)
  365. /** @type {Map<number, Array<Set<Chunk>>>} */
  366. const chunkSetsByCount = new Map();
  367. for (const chunksSet of chunkSetsInGraph.values()) {
  368. const count = chunksSet.size;
  369. let array = chunkSetsByCount.get(count);
  370. if (array === undefined) {
  371. array = [];
  372. chunkSetsByCount.set(count, array);
  373. }
  374. array.push(chunksSet);
  375. }
  376. // Create a list of possible combinations
  377. const combinationsCache = new Map(); // Map<string, Set<Chunk>[]>
  378. const getCombinations = key => {
  379. const chunksSet = chunkSetsInGraph.get(key);
  380. var array = [chunksSet];
  381. if (chunksSet.size > 1) {
  382. for (const [count, setArray] of chunkSetsByCount) {
  383. // "equal" is not needed because they would have been merge in the first step
  384. if (count < chunksSet.size) {
  385. for (const set of setArray) {
  386. if (isSubset(chunksSet, set)) {
  387. array.push(set);
  388. }
  389. }
  390. }
  391. }
  392. }
  393. return array;
  394. };
  395. /**
  396. * @typedef {Object} SelectedChunksResult
  397. * @property {Chunk[]} chunks the list of chunks
  398. * @property {string} key a key of the list
  399. */
  400. /**
  401. * @typedef {function(Chunk): boolean} ChunkFilterFunction
  402. */
  403. /** @type {WeakMap<Set<Chunk>, WeakMap<ChunkFilterFunction, SelectedChunksResult>>} */
  404. const selectedChunksCacheByChunksSet = new WeakMap();
  405. /**
  406. * get list and key by applying the filter function to the list
  407. * It is cached for performance reasons
  408. * @param {Set<Chunk>} chunks list of chunks
  409. * @param {ChunkFilterFunction} chunkFilter filter function for chunks
  410. * @returns {SelectedChunksResult} list and key
  411. */
  412. const getSelectedChunks = (chunks, chunkFilter) => {
  413. let entry = selectedChunksCacheByChunksSet.get(chunks);
  414. if (entry === undefined) {
  415. entry = new WeakMap();
  416. selectedChunksCacheByChunksSet.set(chunks, entry);
  417. }
  418. /** @type {SelectedChunksResult} */
  419. let entry2 = entry.get(chunkFilter);
  420. if (entry2 === undefined) {
  421. /** @type {Chunk[]} */
  422. const selectedChunks = [];
  423. for (const chunk of chunks) {
  424. if (chunkFilter(chunk)) selectedChunks.push(chunk);
  425. }
  426. entry2 = {
  427. chunks: selectedChunks,
  428. key: getKey(selectedChunks)
  429. };
  430. entry.set(chunkFilter, entry2);
  431. }
  432. return entry2;
  433. };
  434. /**
  435. * @typedef {Object} ChunksInfoItem
  436. * @property {SortableSet} modules
  437. * @property {TODO} cacheGroup
  438. * @property {number} cacheGroupIndex
  439. * @property {string} name
  440. * @property {number} size
  441. * @property {Set<Chunk>} chunks
  442. * @property {Set<Chunk>} reuseableChunks
  443. * @property {Set<string>} chunksKeys
  444. */
  445. // Map a list of chunks to a list of modules
  446. // For the key the chunk "index" is used, the value is a SortableSet of modules
  447. /** @type {Map<string, ChunksInfoItem>} */
  448. const chunksInfoMap = new Map();
  449. /**
  450. * @param {TODO} cacheGroup the current cache group
  451. * @param {number} cacheGroupIndex the index of the cache group of ordering
  452. * @param {Chunk[]} selectedChunks chunks selected for this module
  453. * @param {string} selectedChunksKey a key of selectedChunks
  454. * @param {Module} module the current module
  455. * @returns {void}
  456. */
  457. const addModuleToChunksInfoMap = (
  458. cacheGroup,
  459. cacheGroupIndex,
  460. selectedChunks,
  461. selectedChunksKey,
  462. module
  463. ) => {
  464. // Break if minimum number of chunks is not reached
  465. if (selectedChunks.length < cacheGroup.minChunks) return;
  466. // Determine name for split chunk
  467. const name = cacheGroup.getName(
  468. module,
  469. selectedChunks,
  470. cacheGroup.key
  471. );
  472. // Create key for maps
  473. // When it has a name we use the name as key
  474. // Elsewise we create the key from chunks and cache group key
  475. // This automatically merges equal names
  476. const key =
  477. cacheGroup.key +
  478. (name ? ` name:${name}` : ` chunks:${selectedChunksKey}`);
  479. // Add module to maps
  480. let info = chunksInfoMap.get(key);
  481. if (info === undefined) {
  482. chunksInfoMap.set(
  483. key,
  484. (info = {
  485. modules: new SortableSet(undefined, sortByIdentifier),
  486. cacheGroup,
  487. cacheGroupIndex,
  488. name,
  489. size: 0,
  490. chunks: new Set(),
  491. reuseableChunks: new Set(),
  492. chunksKeys: new Set()
  493. })
  494. );
  495. }
  496. const oldSize = info.modules.size;
  497. info.modules.add(module);
  498. if (info.modules.size !== oldSize) {
  499. info.size += module.size();
  500. }
  501. const oldChunksKeysSize = info.chunksKeys.size;
  502. info.chunksKeys.add(selectedChunksKey);
  503. if (oldChunksKeysSize !== info.chunksKeys.size) {
  504. for (const chunk of selectedChunks) {
  505. info.chunks.add(chunk);
  506. }
  507. }
  508. };
  509. // Walk through all modules
  510. for (const module of compilation.modules) {
  511. // Get cache group
  512. let cacheGroups = this.options.getCacheGroups(module);
  513. if (!Array.isArray(cacheGroups) || cacheGroups.length === 0) {
  514. continue;
  515. }
  516. // Prepare some values
  517. const chunksKey = getKey(module.chunksIterable);
  518. let combs = combinationsCache.get(chunksKey);
  519. if (combs === undefined) {
  520. combs = getCombinations(chunksKey);
  521. combinationsCache.set(chunksKey, combs);
  522. }
  523. let cacheGroupIndex = 0;
  524. for (const cacheGroupSource of cacheGroups) {
  525. const minSize =
  526. cacheGroupSource.minSize !== undefined
  527. ? cacheGroupSource.minSize
  528. : cacheGroupSource.enforce
  529. ? 0
  530. : this.options.minSize;
  531. const enforceSizeThreshold =
  532. cacheGroupSource.enforceSizeThreshold !== undefined
  533. ? cacheGroupSource.enforceSizeThreshold
  534. : cacheGroupSource.enforce
  535. ? 0
  536. : this.options.enforceSizeThreshold;
  537. const cacheGroup = {
  538. key: cacheGroupSource.key,
  539. priority: cacheGroupSource.priority || 0,
  540. chunksFilter:
  541. cacheGroupSource.chunksFilter || this.options.chunksFilter,
  542. minSize,
  543. minSizeForMaxSize:
  544. cacheGroupSource.minSize !== undefined
  545. ? cacheGroupSource.minSize
  546. : this.options.minSize,
  547. enforceSizeThreshold,
  548. maxSize:
  549. cacheGroupSource.maxSize !== undefined
  550. ? cacheGroupSource.maxSize
  551. : cacheGroupSource.enforce
  552. ? 0
  553. : this.options.maxSize,
  554. minChunks:
  555. cacheGroupSource.minChunks !== undefined
  556. ? cacheGroupSource.minChunks
  557. : cacheGroupSource.enforce
  558. ? 1
  559. : this.options.minChunks,
  560. maxAsyncRequests:
  561. cacheGroupSource.maxAsyncRequests !== undefined
  562. ? cacheGroupSource.maxAsyncRequests
  563. : cacheGroupSource.enforce
  564. ? Infinity
  565. : this.options.maxAsyncRequests,
  566. maxInitialRequests:
  567. cacheGroupSource.maxInitialRequests !== undefined
  568. ? cacheGroupSource.maxInitialRequests
  569. : cacheGroupSource.enforce
  570. ? Infinity
  571. : this.options.maxInitialRequests,
  572. getName:
  573. cacheGroupSource.getName !== undefined
  574. ? cacheGroupSource.getName
  575. : this.options.getName,
  576. filename:
  577. cacheGroupSource.filename !== undefined
  578. ? cacheGroupSource.filename
  579. : this.options.filename,
  580. automaticNameDelimiter:
  581. cacheGroupSource.automaticNameDelimiter !== undefined
  582. ? cacheGroupSource.automaticNameDelimiter
  583. : this.options.automaticNameDelimiter,
  584. reuseExistingChunk: cacheGroupSource.reuseExistingChunk,
  585. _validateSize: minSize > 0,
  586. _conditionalEnforce: enforceSizeThreshold > 0
  587. };
  588. // For all combination of chunk selection
  589. for (const chunkCombination of combs) {
  590. // Break if minimum number of chunks is not reached
  591. if (chunkCombination.size < cacheGroup.minChunks) continue;
  592. // Select chunks by configuration
  593. const {
  594. chunks: selectedChunks,
  595. key: selectedChunksKey
  596. } = getSelectedChunks(
  597. chunkCombination,
  598. cacheGroup.chunksFilter
  599. );
  600. addModuleToChunksInfoMap(
  601. cacheGroup,
  602. cacheGroupIndex,
  603. selectedChunks,
  604. selectedChunksKey,
  605. module
  606. );
  607. }
  608. cacheGroupIndex++;
  609. }
  610. }
  611. // Filter items were size < minSize
  612. for (const pair of chunksInfoMap) {
  613. const info = pair[1];
  614. if (
  615. info.cacheGroup._validateSize &&
  616. info.size < info.cacheGroup.minSize
  617. ) {
  618. chunksInfoMap.delete(pair[0]);
  619. }
  620. }
  621. /** @type {Map<Chunk, {minSize: number, maxSize: number, automaticNameDelimiter: string, keys: string[]}>} */
  622. const maxSizeQueueMap = new Map();
  623. while (chunksInfoMap.size > 0) {
  624. // Find best matching entry
  625. let bestEntryKey;
  626. let bestEntry;
  627. for (const pair of chunksInfoMap) {
  628. const key = pair[0];
  629. const info = pair[1];
  630. if (bestEntry === undefined) {
  631. bestEntry = info;
  632. bestEntryKey = key;
  633. } else if (compareEntries(bestEntry, info) < 0) {
  634. bestEntry = info;
  635. bestEntryKey = key;
  636. }
  637. }
  638. const item = bestEntry;
  639. chunksInfoMap.delete(bestEntryKey);
  640. let chunkName = item.name;
  641. // Variable for the new chunk (lazy created)
  642. /** @type {Chunk} */
  643. let newChunk;
  644. // When no chunk name, check if we can reuse a chunk instead of creating a new one
  645. let isReused = false;
  646. if (item.cacheGroup.reuseExistingChunk) {
  647. outer: for (const chunk of item.chunks) {
  648. if (chunk.getNumberOfModules() !== item.modules.size) continue;
  649. if (chunk.hasEntryModule()) continue;
  650. for (const module of item.modules) {
  651. if (!chunk.containsModule(module)) continue outer;
  652. }
  653. if (!newChunk || !newChunk.name) {
  654. newChunk = chunk;
  655. } else if (
  656. chunk.name &&
  657. chunk.name.length < newChunk.name.length
  658. ) {
  659. newChunk = chunk;
  660. } else if (
  661. chunk.name &&
  662. chunk.name.length === newChunk.name.length &&
  663. chunk.name < newChunk.name
  664. ) {
  665. newChunk = chunk;
  666. }
  667. chunkName = undefined;
  668. isReused = true;
  669. }
  670. }
  671. // Check if maxRequests condition can be fulfilled
  672. const selectedChunks = Array.from(item.chunks).filter(chunk => {
  673. // skip if we address ourself
  674. return (
  675. (!chunkName || chunk.name !== chunkName) && chunk !== newChunk
  676. );
  677. });
  678. const enforced =
  679. item.cacheGroup._conditionalEnforce &&
  680. item.size >= item.cacheGroup.enforceSizeThreshold;
  681. // Skip when no chunk selected
  682. if (selectedChunks.length === 0) continue;
  683. const usedChunks = new Set(selectedChunks);
  684. // Check if maxRequests condition can be fulfilled
  685. if (
  686. !enforced &&
  687. (Number.isFinite(item.cacheGroup.maxInitialRequests) ||
  688. Number.isFinite(item.cacheGroup.maxAsyncRequests))
  689. ) {
  690. for (const chunk of usedChunks) {
  691. // respect max requests
  692. const maxRequests = chunk.isOnlyInitial()
  693. ? item.cacheGroup.maxInitialRequests
  694. : chunk.canBeInitial()
  695. ? Math.min(
  696. item.cacheGroup.maxInitialRequests,
  697. item.cacheGroup.maxAsyncRequests
  698. )
  699. : item.cacheGroup.maxAsyncRequests;
  700. if (
  701. isFinite(maxRequests) &&
  702. getRequests(chunk) >= maxRequests
  703. ) {
  704. usedChunks.delete(chunk);
  705. }
  706. }
  707. }
  708. outer: for (const chunk of usedChunks) {
  709. for (const module of item.modules) {
  710. if (chunk.containsModule(module)) continue outer;
  711. }
  712. usedChunks.delete(chunk);
  713. }
  714. // Were some (invalid) chunks removed from usedChunks?
  715. // => readd all modules to the queue, as things could have been changed
  716. if (usedChunks.size < selectedChunks.length) {
  717. if (usedChunks.size >= item.cacheGroup.minChunks) {
  718. const chunksArr = Array.from(usedChunks);
  719. for (const module of item.modules) {
  720. addModuleToChunksInfoMap(
  721. item.cacheGroup,
  722. item.cacheGroupIndex,
  723. chunksArr,
  724. getKey(usedChunks),
  725. module
  726. );
  727. }
  728. }
  729. continue;
  730. }
  731. // Create the new chunk if not reusing one
  732. if (!isReused) {
  733. newChunk = compilation.addChunk(chunkName);
  734. }
  735. // Walk through all chunks
  736. for (const chunk of usedChunks) {
  737. // Add graph connections for splitted chunk
  738. chunk.split(newChunk);
  739. }
  740. // Add a note to the chunk
  741. newChunk.chunkReason = isReused
  742. ? "reused as split chunk"
  743. : "split chunk";
  744. if (item.cacheGroup.key) {
  745. newChunk.chunkReason += ` (cache group: ${item.cacheGroup.key})`;
  746. }
  747. if (chunkName) {
  748. newChunk.chunkReason += ` (name: ${chunkName})`;
  749. // If the chosen name is already an entry point we remove the entry point
  750. const entrypoint = compilation.entrypoints.get(chunkName);
  751. if (entrypoint) {
  752. compilation.entrypoints.delete(chunkName);
  753. entrypoint.remove();
  754. newChunk.entryModule = undefined;
  755. }
  756. }
  757. if (item.cacheGroup.filename) {
  758. if (!newChunk.isOnlyInitial()) {
  759. throw new Error(
  760. "SplitChunksPlugin: You are trying to set a filename for a chunk which is (also) loaded on demand. " +
  761. "The runtime can only handle loading of chunks which match the chunkFilename schema. " +
  762. "Using a custom filename would fail at runtime. " +
  763. `(cache group: ${item.cacheGroup.key})`
  764. );
  765. }
  766. newChunk.filenameTemplate = item.cacheGroup.filename;
  767. }
  768. if (!isReused) {
  769. // Add all modules to the new chunk
  770. for (const module of item.modules) {
  771. if (typeof module.chunkCondition === "function") {
  772. if (!module.chunkCondition(newChunk)) continue;
  773. }
  774. // Add module to new chunk
  775. GraphHelpers.connectChunkAndModule(newChunk, module);
  776. // Remove module from used chunks
  777. for (const chunk of usedChunks) {
  778. chunk.removeModule(module);
  779. module.rewriteChunkInReasons(chunk, [newChunk]);
  780. }
  781. }
  782. } else {
  783. // Remove all modules from used chunks
  784. for (const module of item.modules) {
  785. for (const chunk of usedChunks) {
  786. chunk.removeModule(module);
  787. module.rewriteChunkInReasons(chunk, [newChunk]);
  788. }
  789. }
  790. }
  791. if (item.cacheGroup.maxSize > 0) {
  792. const oldMaxSizeSettings = maxSizeQueueMap.get(newChunk);
  793. maxSizeQueueMap.set(newChunk, {
  794. minSize: Math.max(
  795. oldMaxSizeSettings ? oldMaxSizeSettings.minSize : 0,
  796. item.cacheGroup.minSizeForMaxSize
  797. ),
  798. maxSize: Math.min(
  799. oldMaxSizeSettings ? oldMaxSizeSettings.maxSize : Infinity,
  800. item.cacheGroup.maxSize
  801. ),
  802. automaticNameDelimiter: item.cacheGroup.automaticNameDelimiter,
  803. keys: oldMaxSizeSettings
  804. ? oldMaxSizeSettings.keys.concat(item.cacheGroup.key)
  805. : [item.cacheGroup.key]
  806. });
  807. }
  808. // remove all modules from other entries and update size
  809. for (const [key, info] of chunksInfoMap) {
  810. if (isOverlap(info.chunks, usedChunks)) {
  811. // update modules and total size
  812. // may remove it from the map when < minSize
  813. const oldSize = info.modules.size;
  814. for (const module of item.modules) {
  815. info.modules.delete(module);
  816. }
  817. if (info.modules.size !== oldSize) {
  818. if (info.modules.size === 0) {
  819. chunksInfoMap.delete(key);
  820. continue;
  821. }
  822. info.size = getModulesSize(info.modules);
  823. if (
  824. info.cacheGroup._validateSize &&
  825. info.size < info.cacheGroup.minSize
  826. ) {
  827. chunksInfoMap.delete(key);
  828. }
  829. if (info.modules.size === 0) {
  830. chunksInfoMap.delete(key);
  831. }
  832. }
  833. }
  834. }
  835. }
  836. const incorrectMinMaxSizeSet = new Set();
  837. // Make sure that maxSize is fulfilled
  838. for (const chunk of compilation.chunks.slice()) {
  839. const { minSize, maxSize, automaticNameDelimiter, keys } =
  840. maxSizeQueueMap.get(chunk) || this.options.fallbackCacheGroup;
  841. if (!maxSize) continue;
  842. if (minSize > maxSize) {
  843. const warningKey = `${keys && keys.join()} ${minSize} ${maxSize}`;
  844. if (!incorrectMinMaxSizeSet.has(warningKey)) {
  845. incorrectMinMaxSizeSet.add(warningKey);
  846. compilation.warnings.push(
  847. new MinMaxSizeWarning(keys, minSize, maxSize)
  848. );
  849. }
  850. }
  851. const results = deterministicGroupingForModules({
  852. maxSize: Math.max(minSize, maxSize),
  853. minSize,
  854. items: chunk.modulesIterable,
  855. getKey(module) {
  856. const ident = contextify(
  857. compilation.options.context,
  858. module.identifier()
  859. );
  860. const name = module.nameForCondition
  861. ? contextify(
  862. compilation.options.context,
  863. module.nameForCondition()
  864. )
  865. : ident.replace(/^.*!|\?[^?!]*$/g, "");
  866. const fullKey =
  867. name + automaticNameDelimiter + hashFilename(ident);
  868. return fullKey.replace(/[\\/?]/g, "_");
  869. },
  870. getSize(module) {
  871. return module.size();
  872. }
  873. });
  874. results.sort((a, b) => {
  875. if (a.key < b.key) return -1;
  876. if (a.key > b.key) return 1;
  877. return 0;
  878. });
  879. for (let i = 0; i < results.length; i++) {
  880. const group = results[i];
  881. const key = this.options.hidePathInfo
  882. ? hashFilename(group.key)
  883. : group.key;
  884. let name = chunk.name
  885. ? chunk.name + automaticNameDelimiter + key
  886. : null;
  887. if (name && name.length > 100) {
  888. name =
  889. name.slice(0, 100) +
  890. automaticNameDelimiter +
  891. hashFilename(name);
  892. }
  893. let newPart;
  894. if (i !== results.length - 1) {
  895. newPart = compilation.addChunk(name);
  896. chunk.split(newPart);
  897. newPart.chunkReason = chunk.chunkReason;
  898. // Add all modules to the new chunk
  899. for (const module of group.items) {
  900. if (typeof module.chunkCondition === "function") {
  901. if (!module.chunkCondition(newPart)) continue;
  902. }
  903. // Add module to new chunk
  904. GraphHelpers.connectChunkAndModule(newPart, module);
  905. // Remove module from used chunks
  906. chunk.removeModule(module);
  907. module.rewriteChunkInReasons(chunk, [newPart]);
  908. }
  909. } else {
  910. // change the chunk to be a part
  911. newPart = chunk;
  912. chunk.name = name;
  913. }
  914. }
  915. }
  916. }
  917. );
  918. });
  919. }
  920. };