SplitChunksPlugin.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Chunk = require("../Chunk");
  7. const { STAGE_ADVANCED } = require("../OptimizationStages");
  8. const WebpackError = require("../WebpackError");
  9. const { requestToId } = require("../ids/IdHelpers");
  10. const { isSubset } = require("../util/SetHelpers");
  11. const SortableSet = require("../util/SortableSet");
  12. const {
  13. compareIterables,
  14. compareModulesByIdentifier
  15. } = require("../util/comparators");
  16. const createHash = require("../util/createHash");
  17. const deterministicGrouping = require("../util/deterministicGrouping");
  18. const { makePathsRelative } = require("../util/identifier");
  19. const memoize = require("../util/memoize");
  20. const MinMaxSizeWarning = require("./MinMaxSizeWarning");
  21. /** @typedef {import("../../declarations/WebpackOptions").OptimizationSplitChunksCacheGroup} OptimizationSplitChunksCacheGroup */
  22. /** @typedef {import("../../declarations/WebpackOptions").OptimizationSplitChunksOptions} OptimizationSplitChunksOptions */
  23. /** @typedef {import("../../declarations/WebpackOptions").OptimizationSplitChunksSizes} OptimizationSplitChunksSizes */
  24. /** @typedef {import("../config/defaults").OutputNormalizedWithDefaults} OutputOptions */
  25. /** @typedef {import("../Chunk").ChunkName} ChunkName */
  26. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  27. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  28. /** @typedef {import("../Compiler")} Compiler */
  29. /** @typedef {import("../Module")} Module */
  30. /** @typedef {import("../Module").SourceType} SourceType */
  31. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  32. /** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
  33. /** @typedef {import("../util/deterministicGrouping").GroupedItems<Module>} DeterministicGroupingGroupedItemsForModule */
  34. /** @typedef {import("../util/deterministicGrouping").Options<Module>} DeterministicGroupingOptionsForModule */
  35. /** @typedef {import("../util/deterministicGrouping").Sizes} Sizes */
  36. /**
  37. * @callback ChunkFilterFn
  38. * @param {Chunk} chunk
  39. * @returns {boolean | undefined}
  40. */
  41. /** @typedef {number} Priority */
  42. /** @typedef {number} Size */
  43. /** @typedef {number} CountOfChunk */
  44. /** @typedef {number} CountOfRequest */
  45. /**
  46. * @callback CombineSizeFunction
  47. * @param {Size} a
  48. * @param {Size} b
  49. * @returns {Size}
  50. */
  51. /** @typedef {SourceType[]} SourceTypes */
  52. /** @typedef {SourceType[]} DefaultSizeTypes */
  53. /** @typedef {Record<SourceType, Size>} SplitChunksSizes */
  54. /**
  55. * @typedef {object} CacheGroupSource
  56. * @property {string} key
  57. * @property {Priority=} priority
  58. * @property {GetNameFn=} getName
  59. * @property {ChunkFilterFn=} chunksFilter
  60. * @property {boolean=} enforce
  61. * @property {SplitChunksSizes} minSize
  62. * @property {SplitChunksSizes} minSizeReduction
  63. * @property {SplitChunksSizes} minRemainingSize
  64. * @property {SplitChunksSizes} enforceSizeThreshold
  65. * @property {SplitChunksSizes} maxAsyncSize
  66. * @property {SplitChunksSizes} maxInitialSize
  67. * @property {CountOfChunk=} minChunks
  68. * @property {CountOfRequest=} maxAsyncRequests
  69. * @property {CountOfRequest=} maxInitialRequests
  70. * @property {TemplatePath=} filename
  71. * @property {string=} idHint
  72. * @property {string=} automaticNameDelimiter
  73. * @property {boolean=} reuseExistingChunk
  74. * @property {boolean=} usedExports
  75. */
  76. /**
  77. * @typedef {object} CacheGroup
  78. * @property {string} key
  79. * @property {Priority} priority
  80. * @property {GetNameFn=} getName
  81. * @property {ChunkFilterFn} chunksFilter
  82. * @property {SplitChunksSizes} minSize
  83. * @property {SplitChunksSizes} minSizeReduction
  84. * @property {SplitChunksSizes} minRemainingSize
  85. * @property {SplitChunksSizes} enforceSizeThreshold
  86. * @property {SplitChunksSizes} maxAsyncSize
  87. * @property {SplitChunksSizes} maxInitialSize
  88. * @property {CountOfChunk} minChunks
  89. * @property {CountOfRequest} maxAsyncRequests
  90. * @property {CountOfRequest} maxInitialRequests
  91. * @property {TemplatePath=} filename
  92. * @property {string} idHint
  93. * @property {string} automaticNameDelimiter
  94. * @property {boolean} reuseExistingChunk
  95. * @property {boolean} usedExports
  96. * @property {boolean} _validateSize
  97. * @property {boolean} _validateRemainingSize
  98. * @property {SplitChunksSizes} _minSizeForMaxSize
  99. * @property {boolean} _conditionalEnforce
  100. */
  101. /**
  102. * @typedef {object} FallbackCacheGroup
  103. * @property {ChunkFilterFn} chunksFilter
  104. * @property {SplitChunksSizes} minSize
  105. * @property {SplitChunksSizes} maxAsyncSize
  106. * @property {SplitChunksSizes} maxInitialSize
  107. * @property {string} automaticNameDelimiter
  108. */
  109. /**
  110. * @typedef {object} CacheGroupsContext
  111. * @property {ModuleGraph} moduleGraph
  112. * @property {ChunkGraph} chunkGraph
  113. */
  114. /** @typedef {(module: Module) => OptimizationSplitChunksCacheGroup | OptimizationSplitChunksCacheGroup[] | void} RawGetCacheGroups */
  115. /**
  116. * @callback GetCacheGroups
  117. * @param {Module} module
  118. * @param {CacheGroupsContext} context
  119. * @returns {CacheGroupSource[] | null}
  120. */
  121. /**
  122. * @callback GetNameFn
  123. * @param {Module} module
  124. * @param {Chunk[]} chunks
  125. * @param {string} key
  126. * @returns {string | undefined}
  127. */
  128. /**
  129. * @typedef {object} SplitChunksOptions
  130. * @property {ChunkFilterFn} chunksFilter
  131. * @property {DefaultSizeTypes} defaultSizeTypes
  132. * @property {SplitChunksSizes} minSize
  133. * @property {SplitChunksSizes} minSizeReduction
  134. * @property {SplitChunksSizes} minRemainingSize
  135. * @property {SplitChunksSizes} enforceSizeThreshold
  136. * @property {SplitChunksSizes} maxInitialSize
  137. * @property {SplitChunksSizes} maxAsyncSize
  138. * @property {CountOfChunk} minChunks
  139. * @property {CountOfRequest} maxAsyncRequests
  140. * @property {CountOfRequest} maxInitialRequests
  141. * @property {boolean} hidePathInfo
  142. * @property {TemplatePath=} filename
  143. * @property {string} automaticNameDelimiter
  144. * @property {GetCacheGroups} getCacheGroups
  145. * @property {GetNameFn} getName
  146. * @property {boolean} usedExports
  147. * @property {FallbackCacheGroup} fallbackCacheGroup
  148. */
  149. /** @typedef {Set<Chunk>} ChunkSet */
  150. /**
  151. * @typedef {object} ChunksInfoItem
  152. * @property {SortableSet<Module>} modules
  153. * @property {CacheGroup} cacheGroup
  154. * @property {number} cacheGroupIndex
  155. * @property {string=} name
  156. * @property {SplitChunksSizes} sizes
  157. * @property {ChunkSet} chunks
  158. * @property {ChunkSet} reusableChunks
  159. * @property {Set<bigint | Chunk>} chunksKeys
  160. */
  161. /** @type {GetNameFn} */
  162. const defaultGetName = () => undefined;
  163. const deterministicGroupingForModules =
  164. /** @type {(options: DeterministicGroupingOptionsForModule) => DeterministicGroupingGroupedItemsForModule[]} */
  165. (deterministicGrouping);
  166. /** @type {WeakMap<Module, string>} */
  167. const getKeyCache = new WeakMap();
  168. /**
  169. * @param {string} name a filename to hash
  170. * @param {OutputOptions} outputOptions hash function used
  171. * @returns {string} hashed filename
  172. */
  173. const hashFilename = (name, outputOptions) => {
  174. const digest =
  175. /** @type {string} */
  176. (
  177. createHash(outputOptions.hashFunction)
  178. .update(name)
  179. .digest(outputOptions.hashDigest)
  180. );
  181. return digest.slice(0, 8);
  182. };
  183. /**
  184. * @param {Chunk} chunk the chunk
  185. * @returns {CountOfRequest} the number of requests
  186. */
  187. const getRequests = (chunk) => {
  188. let requests = 0;
  189. for (const chunkGroup of chunk.groupsIterable) {
  190. requests = Math.max(requests, chunkGroup.chunks.length);
  191. }
  192. return requests;
  193. };
  194. /**
  195. * @template {object} T
  196. * @template {object} R
  197. * @param {T} obj obj an object
  198. * @param {(obj: T[keyof T], key: keyof T) => T[keyof T]} fn fn
  199. * @returns {T} result
  200. */
  201. const mapObject = (obj, fn) => {
  202. /** @type {T} */
  203. const newObj = Object.create(null);
  204. for (const key of Object.keys(obj)) {
  205. newObj[/** @type {keyof T} */ (key)] = fn(
  206. obj[/** @type {keyof T} */ (key)],
  207. /** @type {keyof T} */
  208. (key)
  209. );
  210. }
  211. return newObj;
  212. };
  213. /**
  214. * @template T
  215. * @param {Set<T>} a set
  216. * @param {Set<T>} b other set
  217. * @returns {boolean} true if at least one item of a is in b
  218. */
  219. const isOverlap = (a, b) => {
  220. for (const item of a) {
  221. if (b.has(item)) return true;
  222. }
  223. return false;
  224. };
  225. const compareModuleIterables = compareIterables(compareModulesByIdentifier);
  226. /**
  227. * @param {ChunksInfoItem} a item
  228. * @param {ChunksInfoItem} b item
  229. * @returns {number} compare result
  230. */
  231. const compareEntries = (a, b) => {
  232. // 1. by priority
  233. const diffPriority = a.cacheGroup.priority - b.cacheGroup.priority;
  234. if (diffPriority) return diffPriority;
  235. // 2. by number of chunks
  236. const diffCount = a.chunks.size - b.chunks.size;
  237. if (diffCount) return diffCount;
  238. // 3. by size reduction
  239. const aSizeReduce = totalSize(a.sizes) * (a.chunks.size - 1);
  240. const bSizeReduce = totalSize(b.sizes) * (b.chunks.size - 1);
  241. const diffSizeReduce = aSizeReduce - bSizeReduce;
  242. if (diffSizeReduce) return diffSizeReduce;
  243. // 4. by cache group index
  244. const indexDiff = b.cacheGroupIndex - a.cacheGroupIndex;
  245. if (indexDiff) return indexDiff;
  246. // 5. by number of modules (to be able to compare by identifier)
  247. const modulesA = a.modules;
  248. const modulesB = b.modules;
  249. const diff = modulesA.size - modulesB.size;
  250. if (diff) return diff;
  251. // 6. by module identifiers
  252. modulesA.sort();
  253. modulesB.sort();
  254. return compareModuleIterables(modulesA, modulesB);
  255. };
  256. /**
  257. * @param {Chunk} chunk the chunk
  258. * @returns {boolean} true, if the chunk is an entry chunk
  259. */
  260. const INITIAL_CHUNK_FILTER = (chunk) => chunk.canBeInitial();
  261. /**
  262. * @param {Chunk} chunk the chunk
  263. * @returns {boolean} true, if the chunk is an async chunk
  264. */
  265. const ASYNC_CHUNK_FILTER = (chunk) => !chunk.canBeInitial();
  266. /**
  267. * @param {Chunk} _chunk the chunk
  268. * @returns {boolean} always true
  269. */
  270. const ALL_CHUNK_FILTER = (_chunk) => true;
  271. /**
  272. * @param {OptimizationSplitChunksSizes | undefined} value the sizes
  273. * @param {DefaultSizeTypes} defaultSizeTypes the default size types
  274. * @returns {SplitChunksSizes} normalized representation
  275. */
  276. const normalizeSizes = (value, defaultSizeTypes) => {
  277. if (typeof value === "number") {
  278. /** @type {SplitChunksSizes} */
  279. const o = {};
  280. for (const sizeType of defaultSizeTypes) o[sizeType] = value;
  281. return o;
  282. } else if (typeof value === "object" && value !== null) {
  283. return { ...value };
  284. }
  285. return {};
  286. };
  287. /**
  288. * @param {...(SplitChunksSizes | undefined)} sizes the sizes
  289. * @returns {SplitChunksSizes} the merged sizes
  290. */
  291. const mergeSizes = (...sizes) => {
  292. /** @type {SplitChunksSizes} */
  293. let merged = {};
  294. for (let i = sizes.length - 1; i >= 0; i--) {
  295. merged = Object.assign(merged, sizes[i]);
  296. }
  297. return merged;
  298. };
  299. /**
  300. * @param {SplitChunksSizes} sizes the sizes
  301. * @returns {boolean} true, if there are sizes > 0
  302. */
  303. const hasNonZeroSizes = (sizes) => {
  304. for (const key of /** @type {SourceType[]} */ (Object.keys(sizes))) {
  305. if (sizes[key] > 0) return true;
  306. }
  307. return false;
  308. };
  309. /**
  310. * @param {SplitChunksSizes} a first sizes
  311. * @param {SplitChunksSizes} b second sizes
  312. * @param {CombineSizeFunction} combine a function to combine sizes
  313. * @returns {SplitChunksSizes} the combine sizes
  314. */
  315. const combineSizes = (a, b, combine) => {
  316. const aKeys = /** @type {Set<SourceType>} */ (new Set(Object.keys(a)));
  317. const bKeys = /** @type {Set<SourceType>} */ (new Set(Object.keys(b)));
  318. /** @type {SplitChunksSizes} */
  319. const result = {};
  320. for (const key of aKeys) {
  321. result[key] = bKeys.has(key) ? combine(a[key], b[key]) : a[key];
  322. }
  323. for (const key of bKeys) {
  324. if (!aKeys.has(key)) {
  325. result[key] = b[key];
  326. }
  327. }
  328. return result;
  329. };
  330. /**
  331. * @param {SplitChunksSizes} sizes the sizes
  332. * @param {SplitChunksSizes} minSize the min sizes
  333. * @returns {boolean} true if there are sizes and all existing sizes are at least `minSize`
  334. */
  335. const checkMinSize = (sizes, minSize) => {
  336. for (const key of /** @type {SourceType[]} */ (Object.keys(minSize))) {
  337. const size = sizes[key];
  338. if (size === undefined || size === 0) continue;
  339. if (size < minSize[key]) return false;
  340. }
  341. return true;
  342. };
  343. /**
  344. * @param {SplitChunksSizes} sizes the sizes
  345. * @param {SplitChunksSizes} minSizeReduction the min sizes
  346. * @param {CountOfChunk} chunkCount number of chunks
  347. * @returns {boolean} true if there are sizes and all existing sizes are at least `minSizeReduction`
  348. */
  349. const checkMinSizeReduction = (sizes, minSizeReduction, chunkCount) => {
  350. for (const key of /** @type {SourceType[]} */ (
  351. Object.keys(minSizeReduction)
  352. )) {
  353. const size = sizes[key];
  354. if (size === undefined || size === 0) continue;
  355. if (size * chunkCount < minSizeReduction[key]) return false;
  356. }
  357. return true;
  358. };
  359. /**
  360. * @param {SplitChunksSizes} sizes the sizes
  361. * @param {SplitChunksSizes} minSize the min sizes
  362. * @returns {undefined | SourceTypes} list of size types that are below min size
  363. */
  364. const getViolatingMinSizes = (sizes, minSize) => {
  365. /** @type {SourceTypes | undefined} */
  366. let list;
  367. for (const key of /** @type {SourceType[]} */ (Object.keys(minSize))) {
  368. const size = sizes[key];
  369. if (size === undefined || size === 0) continue;
  370. if (size < minSize[key]) {
  371. if (list === undefined) list = [key];
  372. else list.push(key);
  373. }
  374. }
  375. return list;
  376. };
  377. /**
  378. * @param {SplitChunksSizes} sizes the sizes
  379. * @returns {Size} the total size
  380. */
  381. const totalSize = (sizes) => {
  382. let size = 0;
  383. for (const key of /** @type {SourceType[]} */ (Object.keys(sizes))) {
  384. size += sizes[key];
  385. }
  386. return size;
  387. };
  388. /**
  389. * @param {OptimizationSplitChunksCacheGroup["name"]} name the chunk name
  390. * @returns {GetNameFn | undefined} a function to get the name of the chunk
  391. */
  392. const normalizeName = (name) => {
  393. if (typeof name === "string") {
  394. return () => name;
  395. }
  396. if (typeof name === "function") {
  397. return /** @type {GetNameFn} */ (name);
  398. }
  399. };
  400. /**
  401. * @param {OptimizationSplitChunksCacheGroup["chunks"]} chunks the chunk filter option
  402. * @returns {ChunkFilterFn | undefined} the chunk filter function
  403. */
  404. const normalizeChunksFilter = (chunks) => {
  405. if (chunks === "initial") {
  406. return INITIAL_CHUNK_FILTER;
  407. }
  408. if (chunks === "async") {
  409. return ASYNC_CHUNK_FILTER;
  410. }
  411. if (chunks === "all") {
  412. return ALL_CHUNK_FILTER;
  413. }
  414. if (chunks instanceof RegExp) {
  415. return (chunk) => (chunk.name ? chunks.test(chunk.name) : false);
  416. }
  417. if (typeof chunks === "function") {
  418. return chunks;
  419. }
  420. };
  421. /**
  422. * @param {undefined | GetCacheGroups | Record<string, false | string | RegExp | RawGetCacheGroups | OptimizationSplitChunksCacheGroup>} cacheGroups the cache group options
  423. * @param {DefaultSizeTypes} defaultSizeTypes the default size types
  424. * @returns {GetCacheGroups} a function to get the cache groups
  425. */
  426. const normalizeCacheGroups = (cacheGroups, defaultSizeTypes) => {
  427. if (typeof cacheGroups === "function") {
  428. return cacheGroups;
  429. }
  430. if (typeof cacheGroups === "object" && cacheGroups !== null) {
  431. /** @type {((module: Module, context: CacheGroupsContext, results: CacheGroupSource[]) => void)[]} */
  432. const handlers = [];
  433. for (const key of Object.keys(cacheGroups)) {
  434. const option = cacheGroups[key];
  435. if (option === false) {
  436. continue;
  437. }
  438. if (typeof option === "string" || option instanceof RegExp) {
  439. const source = createCacheGroupSource({}, key, defaultSizeTypes);
  440. handlers.push((module, context, results) => {
  441. if (checkTest(option, module, context)) {
  442. results.push(source);
  443. }
  444. });
  445. } else if (typeof option === "function") {
  446. /** @type {WeakMap<OptimizationSplitChunksCacheGroup, CacheGroupSource>} */
  447. const cache = new WeakMap();
  448. handlers.push((module, context, results) => {
  449. const result = option(module);
  450. if (result) {
  451. const groups = Array.isArray(result) ? result : [result];
  452. for (const group of groups) {
  453. const cachedSource = cache.get(group);
  454. if (cachedSource !== undefined) {
  455. results.push(cachedSource);
  456. } else {
  457. const source = createCacheGroupSource(
  458. group,
  459. key,
  460. defaultSizeTypes
  461. );
  462. cache.set(group, source);
  463. results.push(source);
  464. }
  465. }
  466. }
  467. });
  468. } else {
  469. const source = createCacheGroupSource(option, key, defaultSizeTypes);
  470. handlers.push((module, context, results) => {
  471. if (
  472. checkTest(option.test, module, context) &&
  473. checkModuleType(option.type, module) &&
  474. checkModuleLayer(option.layer, module)
  475. ) {
  476. results.push(source);
  477. }
  478. });
  479. }
  480. }
  481. /**
  482. * @param {Module} module the current module
  483. * @param {CacheGroupsContext} context the current context
  484. * @returns {CacheGroupSource[]} the matching cache groups
  485. */
  486. const fn = (module, context) => {
  487. /** @type {CacheGroupSource[]} */
  488. const results = [];
  489. for (const fn of handlers) {
  490. fn(module, context, results);
  491. }
  492. return results;
  493. };
  494. return fn;
  495. }
  496. return () => null;
  497. };
  498. /** @typedef {(module: Module, context: CacheGroupsContext) => boolean} CheckTestFn */
  499. /**
  500. * @param {OptimizationSplitChunksCacheGroup["test"]} test test option
  501. * @param {Module} module the module
  502. * @param {CacheGroupsContext} context context object
  503. * @returns {boolean} true, if the module should be selected
  504. */
  505. const checkTest = (test, module, context) => {
  506. if (test === undefined) return true;
  507. if (typeof test === "function") {
  508. return test(module, context);
  509. }
  510. if (typeof test === "boolean") return test;
  511. if (typeof test === "string") {
  512. const name = module.nameForCondition();
  513. return name ? name.startsWith(test) : false;
  514. }
  515. if (test instanceof RegExp) {
  516. const name = module.nameForCondition();
  517. return name ? test.test(name) : false;
  518. }
  519. return false;
  520. };
  521. /** @typedef {(type: string) => boolean} CheckModuleTypeFn */
  522. /**
  523. * @param {OptimizationSplitChunksCacheGroup["type"]} test type option
  524. * @param {Module} module the module
  525. * @returns {boolean} true, if the module should be selected
  526. */
  527. const checkModuleType = (test, module) => {
  528. if (test === undefined) return true;
  529. if (typeof test === "function") {
  530. return test(module.type);
  531. }
  532. if (typeof test === "string") {
  533. const type = module.type;
  534. return test === type;
  535. }
  536. if (test instanceof RegExp) {
  537. const type = module.type;
  538. return test.test(type);
  539. }
  540. return false;
  541. };
  542. /** @typedef {(layer: string | null) => boolean} CheckModuleLayerFn */
  543. /**
  544. * @param {OptimizationSplitChunksCacheGroup["layer"]} test type option
  545. * @param {Module} module the module
  546. * @returns {boolean} true, if the module should be selected
  547. */
  548. const checkModuleLayer = (test, module) => {
  549. if (test === undefined) return true;
  550. if (typeof test === "function") {
  551. return test(module.layer);
  552. }
  553. if (typeof test === "string") {
  554. const layer = module.layer;
  555. return test === "" ? !layer : layer ? layer.startsWith(test) : false;
  556. }
  557. if (test instanceof RegExp) {
  558. const layer = module.layer;
  559. return layer ? test.test(layer) : false;
  560. }
  561. return false;
  562. };
  563. /**
  564. * @param {OptimizationSplitChunksCacheGroup} options the group options
  565. * @param {string} key key of cache group
  566. * @param {DefaultSizeTypes} defaultSizeTypes the default size types
  567. * @returns {CacheGroupSource} the normalized cached group
  568. */
  569. const createCacheGroupSource = (options, key, defaultSizeTypes) => {
  570. const minSize = normalizeSizes(options.minSize, defaultSizeTypes);
  571. const minSizeReduction = normalizeSizes(
  572. options.minSizeReduction,
  573. defaultSizeTypes
  574. );
  575. const maxSize = normalizeSizes(options.maxSize, defaultSizeTypes);
  576. return {
  577. key,
  578. priority: options.priority,
  579. getName: normalizeName(options.name),
  580. chunksFilter: normalizeChunksFilter(options.chunks),
  581. enforce: options.enforce,
  582. minSize,
  583. minSizeReduction,
  584. minRemainingSize: mergeSizes(
  585. normalizeSizes(options.minRemainingSize, defaultSizeTypes),
  586. minSize
  587. ),
  588. enforceSizeThreshold: normalizeSizes(
  589. options.enforceSizeThreshold,
  590. defaultSizeTypes
  591. ),
  592. maxAsyncSize: mergeSizes(
  593. normalizeSizes(options.maxAsyncSize, defaultSizeTypes),
  594. maxSize
  595. ),
  596. maxInitialSize: mergeSizes(
  597. normalizeSizes(options.maxInitialSize, defaultSizeTypes),
  598. maxSize
  599. ),
  600. minChunks: options.minChunks,
  601. maxAsyncRequests: options.maxAsyncRequests,
  602. maxInitialRequests: options.maxInitialRequests,
  603. filename: options.filename,
  604. idHint: options.idHint,
  605. automaticNameDelimiter: options.automaticNameDelimiter,
  606. reuseExistingChunk: options.reuseExistingChunk,
  607. usedExports: options.usedExports
  608. };
  609. };
  610. const PLUGIN_NAME = "SplitChunksPlugin";
  611. module.exports = class SplitChunksPlugin {
  612. /**
  613. * @param {OptimizationSplitChunksOptions=} options plugin options
  614. */
  615. constructor(options = {}) {
  616. const defaultSizeTypes = options.defaultSizeTypes || [
  617. "javascript",
  618. "unknown"
  619. ];
  620. const fallbackCacheGroup = options.fallbackCacheGroup || {};
  621. const minSize = normalizeSizes(options.minSize, defaultSizeTypes);
  622. const minSizeReduction = normalizeSizes(
  623. options.minSizeReduction,
  624. defaultSizeTypes
  625. );
  626. const maxSize = normalizeSizes(options.maxSize, defaultSizeTypes);
  627. /** @type {SplitChunksOptions} */
  628. this.options = {
  629. chunksFilter:
  630. /** @type {ChunkFilterFn} */
  631. (normalizeChunksFilter(options.chunks || "all")),
  632. defaultSizeTypes,
  633. minSize,
  634. minSizeReduction,
  635. minRemainingSize: mergeSizes(
  636. normalizeSizes(options.minRemainingSize, defaultSizeTypes),
  637. minSize
  638. ),
  639. enforceSizeThreshold: normalizeSizes(
  640. options.enforceSizeThreshold,
  641. defaultSizeTypes
  642. ),
  643. maxAsyncSize: mergeSizes(
  644. normalizeSizes(options.maxAsyncSize, defaultSizeTypes),
  645. maxSize
  646. ),
  647. maxInitialSize: mergeSizes(
  648. normalizeSizes(options.maxInitialSize, defaultSizeTypes),
  649. maxSize
  650. ),
  651. minChunks: options.minChunks || 1,
  652. maxAsyncRequests: options.maxAsyncRequests || 1,
  653. maxInitialRequests: options.maxInitialRequests || 1,
  654. hidePathInfo: options.hidePathInfo || false,
  655. filename: options.filename || undefined,
  656. getCacheGroups: normalizeCacheGroups(
  657. options.cacheGroups,
  658. defaultSizeTypes
  659. ),
  660. getName: options.name
  661. ? /** @type {GetNameFn} */ (normalizeName(options.name))
  662. : defaultGetName,
  663. automaticNameDelimiter: options.automaticNameDelimiter || "-",
  664. usedExports: options.usedExports || false,
  665. fallbackCacheGroup: {
  666. chunksFilter:
  667. /** @type {ChunkFilterFn} */
  668. (
  669. normalizeChunksFilter(
  670. fallbackCacheGroup.chunks || options.chunks || "all"
  671. )
  672. ),
  673. minSize: mergeSizes(
  674. normalizeSizes(fallbackCacheGroup.minSize, defaultSizeTypes),
  675. minSize
  676. ),
  677. maxAsyncSize: mergeSizes(
  678. normalizeSizes(fallbackCacheGroup.maxAsyncSize, defaultSizeTypes),
  679. normalizeSizes(fallbackCacheGroup.maxSize, defaultSizeTypes),
  680. normalizeSizes(options.maxAsyncSize, defaultSizeTypes),
  681. normalizeSizes(options.maxSize, defaultSizeTypes)
  682. ),
  683. maxInitialSize: mergeSizes(
  684. normalizeSizes(fallbackCacheGroup.maxInitialSize, defaultSizeTypes),
  685. normalizeSizes(fallbackCacheGroup.maxSize, defaultSizeTypes),
  686. normalizeSizes(options.maxInitialSize, defaultSizeTypes),
  687. normalizeSizes(options.maxSize, defaultSizeTypes)
  688. ),
  689. automaticNameDelimiter:
  690. fallbackCacheGroup.automaticNameDelimiter ||
  691. options.automaticNameDelimiter ||
  692. "~"
  693. }
  694. };
  695. /** @type {WeakMap<CacheGroupSource, CacheGroup>} */
  696. this._cacheGroupCache = new WeakMap();
  697. }
  698. /**
  699. * @param {CacheGroupSource} cacheGroupSource source
  700. * @returns {CacheGroup} the cache group (cached)
  701. */
  702. _getCacheGroup(cacheGroupSource) {
  703. const cacheEntry = this._cacheGroupCache.get(cacheGroupSource);
  704. if (cacheEntry !== undefined) return cacheEntry;
  705. const minSize = mergeSizes(
  706. cacheGroupSource.minSize,
  707. cacheGroupSource.enforce ? undefined : this.options.minSize
  708. );
  709. const minSizeReduction = mergeSizes(
  710. cacheGroupSource.minSizeReduction,
  711. cacheGroupSource.enforce ? undefined : this.options.minSizeReduction
  712. );
  713. const minRemainingSize = mergeSizes(
  714. cacheGroupSource.minRemainingSize,
  715. cacheGroupSource.enforce ? undefined : this.options.minRemainingSize
  716. );
  717. const enforceSizeThreshold = mergeSizes(
  718. cacheGroupSource.enforceSizeThreshold,
  719. cacheGroupSource.enforce ? undefined : this.options.enforceSizeThreshold
  720. );
  721. /** @type {CacheGroup} */
  722. const cacheGroup = {
  723. key: cacheGroupSource.key,
  724. priority: cacheGroupSource.priority || 0,
  725. chunksFilter: cacheGroupSource.chunksFilter || this.options.chunksFilter,
  726. minSize,
  727. minSizeReduction,
  728. minRemainingSize,
  729. enforceSizeThreshold,
  730. maxAsyncSize: mergeSizes(
  731. cacheGroupSource.maxAsyncSize,
  732. cacheGroupSource.enforce ? undefined : this.options.maxAsyncSize
  733. ),
  734. maxInitialSize: mergeSizes(
  735. cacheGroupSource.maxInitialSize,
  736. cacheGroupSource.enforce ? undefined : this.options.maxInitialSize
  737. ),
  738. minChunks:
  739. cacheGroupSource.minChunks !== undefined
  740. ? cacheGroupSource.minChunks
  741. : cacheGroupSource.enforce
  742. ? 1
  743. : this.options.minChunks,
  744. maxAsyncRequests:
  745. cacheGroupSource.maxAsyncRequests !== undefined
  746. ? cacheGroupSource.maxAsyncRequests
  747. : cacheGroupSource.enforce
  748. ? Infinity
  749. : this.options.maxAsyncRequests,
  750. maxInitialRequests:
  751. cacheGroupSource.maxInitialRequests !== undefined
  752. ? cacheGroupSource.maxInitialRequests
  753. : cacheGroupSource.enforce
  754. ? Infinity
  755. : this.options.maxInitialRequests,
  756. getName:
  757. cacheGroupSource.getName !== undefined
  758. ? cacheGroupSource.getName
  759. : this.options.getName,
  760. usedExports:
  761. cacheGroupSource.usedExports !== undefined
  762. ? cacheGroupSource.usedExports
  763. : this.options.usedExports,
  764. filename:
  765. cacheGroupSource.filename !== undefined
  766. ? cacheGroupSource.filename
  767. : this.options.filename,
  768. automaticNameDelimiter:
  769. cacheGroupSource.automaticNameDelimiter !== undefined
  770. ? cacheGroupSource.automaticNameDelimiter
  771. : this.options.automaticNameDelimiter,
  772. idHint:
  773. cacheGroupSource.idHint !== undefined
  774. ? cacheGroupSource.idHint
  775. : cacheGroupSource.key,
  776. reuseExistingChunk: cacheGroupSource.reuseExistingChunk || false,
  777. _validateSize: hasNonZeroSizes(minSize),
  778. _validateRemainingSize: hasNonZeroSizes(minRemainingSize),
  779. _minSizeForMaxSize: mergeSizes(
  780. cacheGroupSource.minSize,
  781. this.options.minSize
  782. ),
  783. _conditionalEnforce: hasNonZeroSizes(enforceSizeThreshold)
  784. };
  785. this._cacheGroupCache.set(cacheGroupSource, cacheGroup);
  786. return cacheGroup;
  787. }
  788. /**
  789. * Apply the plugin
  790. * @param {Compiler} compiler the compiler instance
  791. * @returns {void}
  792. */
  793. apply(compiler) {
  794. const cachedMakePathsRelative = makePathsRelative.bindContextCache(
  795. compiler.context,
  796. compiler.root
  797. );
  798. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  799. const logger = compilation.getLogger(`webpack.${PLUGIN_NAME}`);
  800. let alreadyOptimized = false;
  801. compilation.hooks.unseal.tap(PLUGIN_NAME, () => {
  802. alreadyOptimized = false;
  803. });
  804. compilation.hooks.optimizeChunks.tap(
  805. {
  806. name: PLUGIN_NAME,
  807. stage: STAGE_ADVANCED
  808. },
  809. (chunks) => {
  810. if (alreadyOptimized) return;
  811. alreadyOptimized = true;
  812. logger.time("prepare");
  813. const chunkGraph = compilation.chunkGraph;
  814. const moduleGraph = compilation.moduleGraph;
  815. // Give each selected chunk an index (to create strings from chunks)
  816. /** @type {Map<Chunk, bigint>} */
  817. const chunkIndexMap = new Map();
  818. const ZERO = BigInt("0");
  819. const ONE = BigInt("1");
  820. const START = ONE << BigInt("31");
  821. let index = START;
  822. for (const chunk of chunks) {
  823. chunkIndexMap.set(
  824. chunk,
  825. index | BigInt((Math.random() * 0x7fffffff) | 0)
  826. );
  827. index <<= ONE;
  828. }
  829. /**
  830. * @param {Iterable<Chunk, undefined, undefined>} chunks list of chunks
  831. * @returns {bigint | Chunk} key of the chunks
  832. */
  833. const getKey = (chunks) => {
  834. const iterator = chunks[Symbol.iterator]();
  835. let result = iterator.next();
  836. if (result.done) return ZERO;
  837. const first = result.value;
  838. result = iterator.next();
  839. if (result.done) return first;
  840. let key =
  841. /** @type {bigint} */ (chunkIndexMap.get(first)) |
  842. /** @type {bigint} */ (chunkIndexMap.get(result.value));
  843. while (!(result = iterator.next()).done) {
  844. const raw = chunkIndexMap.get(result.value);
  845. key ^= /** @type {bigint} */ (raw);
  846. }
  847. return key;
  848. };
  849. /**
  850. * @param {bigint | Chunk} key key of the chunks
  851. * @returns {string} stringified key
  852. */
  853. const keyToString = (key) => {
  854. if (typeof key === "bigint") return key.toString(16);
  855. return /** @type {bigint} */ (chunkIndexMap.get(key)).toString(16);
  856. };
  857. const getChunkSetsInGraph = memoize(() => {
  858. /** @type {Map<bigint, ChunkSet>} */
  859. const chunkSetsInGraph = new Map();
  860. /** @type {ChunkSet} */
  861. const singleChunkSets = new Set();
  862. for (const module of compilation.modules) {
  863. const chunks = chunkGraph.getModuleChunksIterable(module);
  864. const chunksKey = getKey(chunks);
  865. if (typeof chunksKey === "bigint") {
  866. if (!chunkSetsInGraph.has(chunksKey)) {
  867. chunkSetsInGraph.set(chunksKey, new Set(chunks));
  868. }
  869. } else {
  870. singleChunkSets.add(chunksKey);
  871. }
  872. }
  873. return { chunkSetsInGraph, singleChunkSets };
  874. });
  875. /**
  876. * @param {Module} module the module
  877. * @returns {Iterable<Chunk[]>} groups of chunks with equal exports
  878. */
  879. const groupChunksByExports = (module) => {
  880. const exportsInfo = moduleGraph.getExportsInfo(module);
  881. /** @type {Map<string, Chunk[]>} */
  882. const groupedByUsedExports = new Map();
  883. for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
  884. const key = exportsInfo.getUsageKey(chunk.runtime);
  885. const list = groupedByUsedExports.get(key);
  886. if (list !== undefined) {
  887. list.push(chunk);
  888. } else {
  889. groupedByUsedExports.set(key, [chunk]);
  890. }
  891. }
  892. return groupedByUsedExports.values();
  893. };
  894. /** @type {Map<Module, Iterable<Chunk[]>>} */
  895. const groupedByExportsMap = new Map();
  896. /** @typedef {Map<bigint | Chunk, ChunkSet>} ChunkSetsInGraph */
  897. const getExportsChunkSetsInGraph = memoize(() => {
  898. /** @type {ChunkSetsInGraph} */
  899. const chunkSetsInGraph = new Map();
  900. /** @type {ChunkSet} */
  901. const singleChunkSets = new Set();
  902. for (const module of compilation.modules) {
  903. const groupedChunks = [...groupChunksByExports(module)];
  904. groupedByExportsMap.set(module, groupedChunks);
  905. for (const chunks of groupedChunks) {
  906. if (chunks.length === 1) {
  907. singleChunkSets.add(chunks[0]);
  908. } else {
  909. const chunksKey = getKey(chunks);
  910. if (!chunkSetsInGraph.has(chunksKey)) {
  911. chunkSetsInGraph.set(chunksKey, new Set(chunks));
  912. }
  913. }
  914. }
  915. }
  916. return { chunkSetsInGraph, singleChunkSets };
  917. });
  918. /** @typedef {Map<CountOfChunk, ChunkSet[]>} ChunkSetsByCount */
  919. // group these set of chunks by count
  920. // to allow to check less sets via isSubset
  921. // (only smaller sets can be subset)
  922. /**
  923. * @param {IterableIterator<ChunkSet>} chunkSets set of sets of chunks
  924. * @returns {ChunkSetsByCount} map of sets of chunks by count
  925. */
  926. const groupChunkSetsByCount = (chunkSets) => {
  927. /** @type {ChunkSetsByCount} */
  928. const chunkSetsByCount = new Map();
  929. for (const chunksSet of chunkSets) {
  930. const count = chunksSet.size;
  931. let array = chunkSetsByCount.get(count);
  932. if (array === undefined) {
  933. array = [];
  934. chunkSetsByCount.set(count, array);
  935. }
  936. array.push(chunksSet);
  937. }
  938. return chunkSetsByCount;
  939. };
  940. const getChunkSetsByCount = memoize(() =>
  941. groupChunkSetsByCount(
  942. getChunkSetsInGraph().chunkSetsInGraph.values()
  943. )
  944. );
  945. const getExportsChunkSetsByCount = memoize(() =>
  946. groupChunkSetsByCount(
  947. getExportsChunkSetsInGraph().chunkSetsInGraph.values()
  948. )
  949. );
  950. /** @typedef {(ChunkSet | Chunk)[]} Combinations */
  951. // Create a list of possible combinations
  952. /**
  953. * @param {ChunkSetsInGraph} chunkSets chunk sets
  954. * @param {ChunkSet} singleChunkSets single chunks sets
  955. * @param {ChunkSetsByCount} chunkSetsByCount chunk sets by count
  956. * @returns {(key: bigint | Chunk) => Combinations} combinations
  957. */
  958. const createGetCombinations = (
  959. chunkSets,
  960. singleChunkSets,
  961. chunkSetsByCount
  962. ) => {
  963. /** @type {Map<bigint | Chunk, Combinations>} */
  964. const combinationsCache = new Map();
  965. return (key) => {
  966. const cacheEntry = combinationsCache.get(key);
  967. if (cacheEntry !== undefined) return cacheEntry;
  968. if (key instanceof Chunk) {
  969. const result = [key];
  970. combinationsCache.set(key, result);
  971. return result;
  972. }
  973. const chunksSet =
  974. /** @type {ChunkSet} */
  975. (chunkSets.get(key));
  976. /** @type {Combinations} */
  977. const array = [chunksSet];
  978. for (const [count, setArray] of chunkSetsByCount) {
  979. // "equal" is not needed because they would have been merge in the first step
  980. if (count < chunksSet.size) {
  981. for (const set of setArray) {
  982. if (isSubset(chunksSet, set)) {
  983. array.push(set);
  984. }
  985. }
  986. }
  987. }
  988. for (const chunk of singleChunkSets) {
  989. if (chunksSet.has(chunk)) {
  990. array.push(chunk);
  991. }
  992. }
  993. combinationsCache.set(key, array);
  994. return array;
  995. };
  996. };
  997. const getCombinationsFactory = memoize(() => {
  998. const { chunkSetsInGraph, singleChunkSets } = getChunkSetsInGraph();
  999. return createGetCombinations(
  1000. chunkSetsInGraph,
  1001. singleChunkSets,
  1002. getChunkSetsByCount()
  1003. );
  1004. });
  1005. /**
  1006. * @param {bigint | Chunk} key key
  1007. * @returns {Combinations} combinations by key
  1008. */
  1009. const getCombinations = (key) => getCombinationsFactory()(key);
  1010. const getExportsCombinationsFactory = memoize(() => {
  1011. const { chunkSetsInGraph, singleChunkSets } =
  1012. getExportsChunkSetsInGraph();
  1013. return createGetCombinations(
  1014. chunkSetsInGraph,
  1015. singleChunkSets,
  1016. getExportsChunkSetsByCount()
  1017. );
  1018. });
  1019. /**
  1020. * @param {bigint | Chunk} key key
  1021. * @returns {Combinations} exports combinations by key
  1022. */
  1023. const getExportsCombinations = (key) =>
  1024. getExportsCombinationsFactory()(key);
  1025. /**
  1026. * @typedef {object} SelectedChunksResult
  1027. * @property {Chunk[]} chunks the list of chunks
  1028. * @property {bigint | Chunk} key a key of the list
  1029. */
  1030. /** @typedef {WeakMap<ChunkFilterFn, SelectedChunksResult>} ChunkMap */
  1031. /** @type {WeakMap<ChunkSet | Chunk, ChunkMap>} */
  1032. const selectedChunksCacheByChunksSet = new WeakMap();
  1033. /**
  1034. * get list and key by applying the filter function to the list
  1035. * It is cached for performance reasons
  1036. * @param {ChunkSet | Chunk} chunks list of chunks
  1037. * @param {ChunkFilterFn} chunkFilter filter function for chunks
  1038. * @returns {SelectedChunksResult} list and key
  1039. */
  1040. const getSelectedChunks = (chunks, chunkFilter) => {
  1041. let entry = selectedChunksCacheByChunksSet.get(chunks);
  1042. if (entry === undefined) {
  1043. /** @type {ChunkMap} */
  1044. entry = new WeakMap();
  1045. selectedChunksCacheByChunksSet.set(chunks, entry);
  1046. }
  1047. let entry2 =
  1048. /** @type {SelectedChunksResult} */
  1049. (entry.get(chunkFilter));
  1050. if (entry2 === undefined) {
  1051. /** @type {Chunk[]} */
  1052. const selectedChunks = [];
  1053. if (chunks instanceof Chunk) {
  1054. if (chunkFilter(chunks)) selectedChunks.push(chunks);
  1055. } else {
  1056. for (const chunk of chunks) {
  1057. if (chunkFilter(chunk)) selectedChunks.push(chunk);
  1058. }
  1059. }
  1060. entry2 = {
  1061. chunks: selectedChunks,
  1062. key: getKey(selectedChunks)
  1063. };
  1064. entry.set(chunkFilter, entry2);
  1065. }
  1066. return entry2;
  1067. };
  1068. /** @type {Map<string, boolean>} */
  1069. const alreadyValidatedParents = new Map();
  1070. /** @type {Set<string>} */
  1071. const alreadyReportedErrors = new Set();
  1072. // Map a list of chunks to a list of modules
  1073. // For the key the chunk "index" is used, the value is a SortableSet of modules
  1074. /** @type {Map<string, ChunksInfoItem>} */
  1075. const chunksInfoMap = new Map();
  1076. /**
  1077. * @param {CacheGroup} cacheGroup the current cache group
  1078. * @param {number} cacheGroupIndex the index of the cache group of ordering
  1079. * @param {Chunk[]} selectedChunks chunks selected for this module
  1080. * @param {bigint | Chunk} selectedChunksKey a key of selectedChunks
  1081. * @param {Module} module the current module
  1082. * @returns {void}
  1083. */
  1084. const addModuleToChunksInfoMap = (
  1085. cacheGroup,
  1086. cacheGroupIndex,
  1087. selectedChunks,
  1088. selectedChunksKey,
  1089. module
  1090. ) => {
  1091. // Break if minimum number of chunks is not reached
  1092. if (selectedChunks.length < cacheGroup.minChunks) return;
  1093. // Determine name for split chunk
  1094. const name =
  1095. /** @type {GetNameFn} */
  1096. (cacheGroup.getName)(module, selectedChunks, cacheGroup.key);
  1097. // Check if the name is ok
  1098. const existingChunk = name && compilation.namedChunks.get(name);
  1099. if (existingChunk) {
  1100. const parentValidationKey = `${name}|${
  1101. typeof selectedChunksKey === "bigint"
  1102. ? selectedChunksKey
  1103. : selectedChunksKey.debugId
  1104. }`;
  1105. const valid = alreadyValidatedParents.get(parentValidationKey);
  1106. if (valid === false) return;
  1107. if (valid === undefined) {
  1108. // Module can only be moved into the existing chunk if the existing chunk
  1109. // is a parent of all selected chunks
  1110. let isInAllParents = true;
  1111. /** @type {Set<ChunkGroup>} */
  1112. const queue = new Set();
  1113. for (const chunk of selectedChunks) {
  1114. for (const group of chunk.groupsIterable) {
  1115. queue.add(group);
  1116. }
  1117. }
  1118. for (const group of queue) {
  1119. if (existingChunk.isInGroup(group)) continue;
  1120. let hasParent = false;
  1121. for (const parent of group.parentsIterable) {
  1122. hasParent = true;
  1123. queue.add(parent);
  1124. }
  1125. if (!hasParent) {
  1126. isInAllParents = false;
  1127. }
  1128. }
  1129. const valid = isInAllParents;
  1130. alreadyValidatedParents.set(parentValidationKey, valid);
  1131. if (!valid) {
  1132. if (!alreadyReportedErrors.has(name)) {
  1133. alreadyReportedErrors.add(name);
  1134. compilation.errors.push(
  1135. new WebpackError(
  1136. `${PLUGIN_NAME}\n` +
  1137. `Cache group "${cacheGroup.key}" conflicts with existing chunk.\n` +
  1138. `Both have the same name "${name}" and existing chunk is not a parent of the selected modules.\n` +
  1139. "Use a different name for the cache group or make sure that the existing chunk is a parent (e. g. via dependOn).\n" +
  1140. 'HINT: You can omit "name" to automatically create a name.\n' +
  1141. "BREAKING CHANGE: webpack < 5 used to allow to use an entrypoint as splitChunk. " +
  1142. "This is no longer allowed when the entrypoint is not a parent of the selected modules.\n" +
  1143. "Remove this entrypoint and add modules to cache group's 'test' instead. " +
  1144. "If you need modules to be evaluated on startup, add them to the existing entrypoints (make them arrays). " +
  1145. "See migration guide of more info."
  1146. )
  1147. );
  1148. }
  1149. return;
  1150. }
  1151. }
  1152. }
  1153. // Create key for maps
  1154. // When it has a name we use the name as key
  1155. // Otherwise we create the key from chunks and cache group key
  1156. // This automatically merges equal names
  1157. const key =
  1158. cacheGroup.key +
  1159. (name
  1160. ? ` name:${name}`
  1161. : ` chunks:${keyToString(selectedChunksKey)}`);
  1162. // Add module to maps
  1163. let info = chunksInfoMap.get(key);
  1164. if (info === undefined) {
  1165. chunksInfoMap.set(
  1166. key,
  1167. (info = {
  1168. modules: new SortableSet(
  1169. undefined,
  1170. compareModulesByIdentifier
  1171. ),
  1172. cacheGroup,
  1173. cacheGroupIndex,
  1174. name,
  1175. sizes: {},
  1176. chunks: new Set(),
  1177. reusableChunks: new Set(),
  1178. chunksKeys: new Set()
  1179. })
  1180. );
  1181. }
  1182. const oldSize = info.modules.size;
  1183. info.modules.add(module);
  1184. if (info.modules.size !== oldSize) {
  1185. for (const type of module.getSourceTypes()) {
  1186. info.sizes[type] = (info.sizes[type] || 0) + module.size(type);
  1187. }
  1188. }
  1189. const oldChunksKeysSize = info.chunksKeys.size;
  1190. info.chunksKeys.add(selectedChunksKey);
  1191. if (oldChunksKeysSize !== info.chunksKeys.size) {
  1192. for (const chunk of selectedChunks) {
  1193. info.chunks.add(chunk);
  1194. }
  1195. }
  1196. };
  1197. const context = {
  1198. moduleGraph,
  1199. chunkGraph
  1200. };
  1201. logger.timeEnd("prepare");
  1202. logger.time("modules");
  1203. // Walk through all modules
  1204. for (const module of compilation.modules) {
  1205. // Get cache group
  1206. const cacheGroups = this.options.getCacheGroups(module, context);
  1207. if (!Array.isArray(cacheGroups) || cacheGroups.length === 0) {
  1208. continue;
  1209. }
  1210. // Prepare some values (usedExports = false)
  1211. const getCombs = memoize(() => {
  1212. const chunks = chunkGraph.getModuleChunksIterable(module);
  1213. const chunksKey = getKey(chunks);
  1214. return getCombinations(chunksKey);
  1215. });
  1216. // Prepare some values (usedExports = true)
  1217. const getCombsByUsedExports = memoize(() => {
  1218. // fill the groupedByExportsMap
  1219. getExportsChunkSetsInGraph();
  1220. /** @type {Set<ChunkSet | Chunk>} */
  1221. const set = new Set();
  1222. const groupedByUsedExports =
  1223. /** @type {Iterable<Chunk[]>} */
  1224. (groupedByExportsMap.get(module));
  1225. for (const chunks of groupedByUsedExports) {
  1226. const chunksKey = getKey(chunks);
  1227. for (const comb of getExportsCombinations(chunksKey)) {
  1228. set.add(comb);
  1229. }
  1230. }
  1231. return set;
  1232. });
  1233. let cacheGroupIndex = 0;
  1234. for (const cacheGroupSource of cacheGroups) {
  1235. const cacheGroup = this._getCacheGroup(cacheGroupSource);
  1236. const combs = cacheGroup.usedExports
  1237. ? getCombsByUsedExports()
  1238. : getCombs();
  1239. // For all combination of chunk selection
  1240. for (const chunkCombination of combs) {
  1241. // Break if minimum number of chunks is not reached
  1242. const count =
  1243. chunkCombination instanceof Chunk ? 1 : chunkCombination.size;
  1244. if (count < cacheGroup.minChunks) continue;
  1245. // Select chunks by configuration
  1246. const { chunks: selectedChunks, key: selectedChunksKey } =
  1247. getSelectedChunks(
  1248. chunkCombination,
  1249. /** @type {ChunkFilterFn} */
  1250. (cacheGroup.chunksFilter)
  1251. );
  1252. addModuleToChunksInfoMap(
  1253. cacheGroup,
  1254. cacheGroupIndex,
  1255. selectedChunks,
  1256. selectedChunksKey,
  1257. module
  1258. );
  1259. }
  1260. cacheGroupIndex++;
  1261. }
  1262. }
  1263. logger.timeEnd("modules");
  1264. logger.time("queue");
  1265. /**
  1266. * @param {ChunksInfoItem} info entry
  1267. * @param {SourceTypes} sourceTypes source types to be removed
  1268. */
  1269. const removeModulesWithSourceType = (info, sourceTypes) => {
  1270. for (const module of info.modules) {
  1271. const types = module.getSourceTypes();
  1272. if (sourceTypes.some((type) => types.has(type))) {
  1273. info.modules.delete(module);
  1274. for (const type of types) {
  1275. info.sizes[type] -= module.size(type);
  1276. }
  1277. }
  1278. }
  1279. };
  1280. /**
  1281. * @param {ChunksInfoItem} info entry
  1282. * @returns {boolean} true, if entry become empty
  1283. */
  1284. const removeMinSizeViolatingModules = (info) => {
  1285. if (!info.cacheGroup._validateSize) return false;
  1286. const violatingSizes = getViolatingMinSizes(
  1287. info.sizes,
  1288. info.cacheGroup.minSize
  1289. );
  1290. if (violatingSizes === undefined) return false;
  1291. removeModulesWithSourceType(info, violatingSizes);
  1292. return info.modules.size === 0;
  1293. };
  1294. // Filter items were size < minSize
  1295. for (const [key, info] of chunksInfoMap) {
  1296. if (removeMinSizeViolatingModules(info)) {
  1297. chunksInfoMap.delete(key);
  1298. } else if (
  1299. !checkMinSizeReduction(
  1300. info.sizes,
  1301. info.cacheGroup.minSizeReduction,
  1302. info.chunks.size
  1303. )
  1304. ) {
  1305. chunksInfoMap.delete(key);
  1306. }
  1307. }
  1308. /**
  1309. * @typedef {object} MaxSizeQueueItem
  1310. * @property {SplitChunksSizes} minSize
  1311. * @property {SplitChunksSizes} maxAsyncSize
  1312. * @property {SplitChunksSizes} maxInitialSize
  1313. * @property {string} automaticNameDelimiter
  1314. * @property {string[]} keys
  1315. */
  1316. /** @type {Map<Chunk, MaxSizeQueueItem>} */
  1317. const maxSizeQueueMap = new Map();
  1318. while (chunksInfoMap.size > 0) {
  1319. // Find best matching entry
  1320. /** @type {undefined | string} */
  1321. let bestEntryKey;
  1322. /** @type {undefined | ChunksInfoItem} */
  1323. let bestEntry;
  1324. for (const pair of chunksInfoMap) {
  1325. const key = pair[0];
  1326. const info = pair[1];
  1327. if (
  1328. bestEntry === undefined ||
  1329. compareEntries(bestEntry, info) < 0
  1330. ) {
  1331. bestEntry = info;
  1332. bestEntryKey = key;
  1333. }
  1334. }
  1335. const item = /** @type {ChunksInfoItem} */ (bestEntry);
  1336. chunksInfoMap.delete(/** @type {string} */ (bestEntryKey));
  1337. /** @type {ChunkName | undefined} */
  1338. let chunkName = item.name;
  1339. // Variable for the new chunk (lazy created)
  1340. /** @type {Chunk | undefined} */
  1341. let newChunk;
  1342. // When no chunk name, check if we can reuse a chunk instead of creating a new one
  1343. let isExistingChunk = false;
  1344. let isReusedWithAllModules = false;
  1345. if (chunkName) {
  1346. const chunkByName = compilation.namedChunks.get(chunkName);
  1347. if (chunkByName !== undefined) {
  1348. newChunk = chunkByName;
  1349. const oldSize = item.chunks.size;
  1350. item.chunks.delete(newChunk);
  1351. isExistingChunk = item.chunks.size !== oldSize;
  1352. }
  1353. } else if (item.cacheGroup.reuseExistingChunk) {
  1354. outer: for (const chunk of item.chunks) {
  1355. if (
  1356. chunkGraph.getNumberOfChunkModules(chunk) !==
  1357. item.modules.size
  1358. ) {
  1359. continue;
  1360. }
  1361. if (
  1362. item.chunks.size > 1 &&
  1363. chunkGraph.getNumberOfEntryModules(chunk) > 0
  1364. ) {
  1365. continue;
  1366. }
  1367. for (const module of item.modules) {
  1368. if (!chunkGraph.isModuleInChunk(module, chunk)) {
  1369. continue outer;
  1370. }
  1371. }
  1372. if (!newChunk || !newChunk.name) {
  1373. newChunk = chunk;
  1374. } else if (
  1375. chunk.name &&
  1376. chunk.name.length < newChunk.name.length
  1377. ) {
  1378. newChunk = chunk;
  1379. } else if (
  1380. chunk.name &&
  1381. chunk.name.length === newChunk.name.length &&
  1382. chunk.name < newChunk.name
  1383. ) {
  1384. newChunk = chunk;
  1385. }
  1386. }
  1387. if (newChunk) {
  1388. item.chunks.delete(newChunk);
  1389. chunkName = undefined;
  1390. isExistingChunk = true;
  1391. isReusedWithAllModules = true;
  1392. }
  1393. }
  1394. const enforced =
  1395. item.cacheGroup._conditionalEnforce &&
  1396. checkMinSize(item.sizes, item.cacheGroup.enforceSizeThreshold);
  1397. /** @type {Set<Chunk>} */
  1398. const usedChunks = new Set(item.chunks);
  1399. // Check if maxRequests condition can be fulfilled
  1400. if (
  1401. !enforced &&
  1402. (Number.isFinite(item.cacheGroup.maxInitialRequests) ||
  1403. Number.isFinite(item.cacheGroup.maxAsyncRequests))
  1404. ) {
  1405. for (const chunk of usedChunks) {
  1406. // respect max requests
  1407. const maxRequests = chunk.isOnlyInitial()
  1408. ? item.cacheGroup.maxInitialRequests
  1409. : chunk.canBeInitial()
  1410. ? Math.min(
  1411. item.cacheGroup.maxInitialRequests,
  1412. item.cacheGroup.maxAsyncRequests
  1413. )
  1414. : item.cacheGroup.maxAsyncRequests;
  1415. if (
  1416. Number.isFinite(maxRequests) &&
  1417. getRequests(chunk) >= maxRequests
  1418. ) {
  1419. usedChunks.delete(chunk);
  1420. }
  1421. }
  1422. }
  1423. outer: for (const chunk of usedChunks) {
  1424. for (const module of item.modules) {
  1425. if (chunkGraph.isModuleInChunk(module, chunk)) continue outer;
  1426. }
  1427. usedChunks.delete(chunk);
  1428. }
  1429. // Were some (invalid) chunks removed from usedChunks?
  1430. // => readd all modules to the queue, as things could have been changed
  1431. if (usedChunks.size < item.chunks.size) {
  1432. if (isExistingChunk) {
  1433. usedChunks.add(/** @type {Chunk} */ (newChunk));
  1434. }
  1435. if (usedChunks.size >= item.cacheGroup.minChunks) {
  1436. const chunksArr = [...usedChunks];
  1437. for (const module of item.modules) {
  1438. addModuleToChunksInfoMap(
  1439. item.cacheGroup,
  1440. item.cacheGroupIndex,
  1441. chunksArr,
  1442. getKey(usedChunks),
  1443. module
  1444. );
  1445. }
  1446. }
  1447. continue;
  1448. }
  1449. // Validate minRemainingSize constraint when a single chunk is left over
  1450. if (
  1451. !enforced &&
  1452. item.cacheGroup._validateRemainingSize &&
  1453. usedChunks.size === 1
  1454. ) {
  1455. const [chunk] = usedChunks;
  1456. /** @type {SplitChunksSizes} */
  1457. const chunkSizes = Object.create(null);
  1458. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  1459. if (!item.modules.has(module)) {
  1460. for (const type of module.getSourceTypes()) {
  1461. chunkSizes[type] =
  1462. (chunkSizes[type] || 0) + module.size(type);
  1463. }
  1464. }
  1465. }
  1466. const violatingSizes = getViolatingMinSizes(
  1467. chunkSizes,
  1468. item.cacheGroup.minRemainingSize
  1469. );
  1470. if (violatingSizes !== undefined) {
  1471. const oldModulesSize = item.modules.size;
  1472. removeModulesWithSourceType(item, violatingSizes);
  1473. if (
  1474. item.modules.size > 0 &&
  1475. item.modules.size !== oldModulesSize
  1476. ) {
  1477. // queue this item again to be processed again
  1478. // without violating modules
  1479. chunksInfoMap.set(/** @type {string} */ (bestEntryKey), item);
  1480. }
  1481. continue;
  1482. }
  1483. }
  1484. // Create the new chunk if not reusing one
  1485. if (newChunk === undefined) {
  1486. newChunk = compilation.addChunk(chunkName);
  1487. }
  1488. // Walk through all chunks
  1489. for (const chunk of usedChunks) {
  1490. // Add graph connections for splitted chunk
  1491. chunk.split(newChunk);
  1492. }
  1493. // Add a note to the chunk
  1494. newChunk.chunkReason =
  1495. (newChunk.chunkReason ? `${newChunk.chunkReason}, ` : "") +
  1496. (isReusedWithAllModules
  1497. ? "reused as split chunk"
  1498. : "split chunk");
  1499. if (item.cacheGroup.key) {
  1500. newChunk.chunkReason += ` (cache group: ${item.cacheGroup.key})`;
  1501. }
  1502. if (chunkName) {
  1503. newChunk.chunkReason += ` (name: ${chunkName})`;
  1504. }
  1505. if (item.cacheGroup.filename) {
  1506. newChunk.filenameTemplate = item.cacheGroup.filename;
  1507. }
  1508. if (item.cacheGroup.idHint) {
  1509. newChunk.idNameHints.add(item.cacheGroup.idHint);
  1510. }
  1511. if (!isReusedWithAllModules) {
  1512. // Add all modules to the new chunk
  1513. for (const module of item.modules) {
  1514. if (!module.chunkCondition(newChunk, compilation)) continue;
  1515. // Add module to new chunk
  1516. chunkGraph.connectChunkAndModule(newChunk, module);
  1517. // Remove module from used chunks
  1518. for (const chunk of usedChunks) {
  1519. chunkGraph.disconnectChunkAndModule(chunk, module);
  1520. }
  1521. }
  1522. } else {
  1523. // Remove all modules from used chunks
  1524. for (const module of item.modules) {
  1525. for (const chunk of usedChunks) {
  1526. chunkGraph.disconnectChunkAndModule(chunk, module);
  1527. }
  1528. }
  1529. }
  1530. if (
  1531. Object.keys(item.cacheGroup.maxAsyncSize).length > 0 ||
  1532. Object.keys(item.cacheGroup.maxInitialSize).length > 0
  1533. ) {
  1534. const oldMaxSizeSettings = maxSizeQueueMap.get(newChunk);
  1535. maxSizeQueueMap.set(newChunk, {
  1536. minSize: oldMaxSizeSettings
  1537. ? combineSizes(
  1538. oldMaxSizeSettings.minSize,
  1539. item.cacheGroup._minSizeForMaxSize,
  1540. Math.max
  1541. )
  1542. : item.cacheGroup.minSize,
  1543. maxAsyncSize: oldMaxSizeSettings
  1544. ? combineSizes(
  1545. oldMaxSizeSettings.maxAsyncSize,
  1546. item.cacheGroup.maxAsyncSize,
  1547. Math.min
  1548. )
  1549. : item.cacheGroup.maxAsyncSize,
  1550. maxInitialSize: oldMaxSizeSettings
  1551. ? combineSizes(
  1552. oldMaxSizeSettings.maxInitialSize,
  1553. item.cacheGroup.maxInitialSize,
  1554. Math.min
  1555. )
  1556. : item.cacheGroup.maxInitialSize,
  1557. automaticNameDelimiter: item.cacheGroup.automaticNameDelimiter,
  1558. keys: oldMaxSizeSettings
  1559. ? [...oldMaxSizeSettings.keys, item.cacheGroup.key]
  1560. : [item.cacheGroup.key]
  1561. });
  1562. }
  1563. // remove all modules from other entries and update size
  1564. for (const [key, info] of chunksInfoMap) {
  1565. if (isOverlap(info.chunks, usedChunks)) {
  1566. // update modules and total size
  1567. // may remove it from the map when < minSize
  1568. let updated = false;
  1569. for (const module of item.modules) {
  1570. if (info.modules.has(module)) {
  1571. // remove module
  1572. info.modules.delete(module);
  1573. // update size
  1574. for (const key of module.getSourceTypes()) {
  1575. info.sizes[key] -= module.size(key);
  1576. }
  1577. updated = true;
  1578. }
  1579. }
  1580. if (updated) {
  1581. if (info.modules.size === 0) {
  1582. chunksInfoMap.delete(key);
  1583. continue;
  1584. }
  1585. if (
  1586. removeMinSizeViolatingModules(info) ||
  1587. !checkMinSizeReduction(
  1588. info.sizes,
  1589. info.cacheGroup.minSizeReduction,
  1590. info.chunks.size
  1591. )
  1592. ) {
  1593. chunksInfoMap.delete(key);
  1594. continue;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. logger.timeEnd("queue");
  1601. logger.time("maxSize");
  1602. /** @type {Set<string>} */
  1603. const incorrectMinMaxSizeSet = new Set();
  1604. const { outputOptions } = compilation;
  1605. // Make sure that maxSize is fulfilled
  1606. const { fallbackCacheGroup } = this.options;
  1607. for (const chunk of compilation.chunks) {
  1608. const chunkConfig = maxSizeQueueMap.get(chunk);
  1609. const {
  1610. minSize,
  1611. maxAsyncSize,
  1612. maxInitialSize,
  1613. automaticNameDelimiter
  1614. } = chunkConfig || fallbackCacheGroup;
  1615. if (!chunkConfig && !fallbackCacheGroup.chunksFilter(chunk)) {
  1616. continue;
  1617. }
  1618. /** @type {SplitChunksSizes} */
  1619. let maxSize;
  1620. if (chunk.isOnlyInitial()) {
  1621. maxSize = maxInitialSize;
  1622. } else if (chunk.canBeInitial()) {
  1623. maxSize = combineSizes(maxAsyncSize, maxInitialSize, Math.min);
  1624. } else {
  1625. maxSize = maxAsyncSize;
  1626. }
  1627. if (Object.keys(maxSize).length === 0) {
  1628. continue;
  1629. }
  1630. for (const key of /** @type {SourceType[]} */ (
  1631. Object.keys(maxSize)
  1632. )) {
  1633. const maxSizeValue = maxSize[key];
  1634. const minSizeValue = minSize[key];
  1635. if (
  1636. typeof minSizeValue === "number" &&
  1637. minSizeValue > maxSizeValue
  1638. ) {
  1639. const keys = chunkConfig && chunkConfig.keys;
  1640. const warningKey = `${
  1641. keys && keys.join()
  1642. } ${minSizeValue} ${maxSizeValue}`;
  1643. if (!incorrectMinMaxSizeSet.has(warningKey)) {
  1644. incorrectMinMaxSizeSet.add(warningKey);
  1645. compilation.warnings.push(
  1646. new MinMaxSizeWarning(keys, minSizeValue, maxSizeValue)
  1647. );
  1648. }
  1649. }
  1650. }
  1651. const results = deterministicGroupingForModules({
  1652. minSize,
  1653. maxSize: mapObject(maxSize, (value, key) => {
  1654. const minSizeValue = minSize[key];
  1655. return typeof minSizeValue === "number"
  1656. ? Math.max(value, minSizeValue)
  1657. : value;
  1658. }),
  1659. items: chunkGraph.getChunkModulesIterable(chunk),
  1660. getKey(module) {
  1661. const cache = getKeyCache.get(module);
  1662. if (cache !== undefined) return cache;
  1663. const ident = cachedMakePathsRelative(module.identifier());
  1664. const nameForCondition =
  1665. module.nameForCondition && module.nameForCondition();
  1666. const name = nameForCondition
  1667. ? cachedMakePathsRelative(nameForCondition)
  1668. : ident.replace(/^.*!|\?[^?!]*$/g, "");
  1669. const fullKey =
  1670. name +
  1671. automaticNameDelimiter +
  1672. hashFilename(ident, outputOptions);
  1673. const key = requestToId(fullKey);
  1674. getKeyCache.set(module, key);
  1675. return key;
  1676. },
  1677. getSize(module) {
  1678. /** @type {Sizes} */
  1679. const size = Object.create(null);
  1680. for (const key of module.getSourceTypes()) {
  1681. size[key] = module.size(key);
  1682. }
  1683. return size;
  1684. }
  1685. });
  1686. if (results.length <= 1) {
  1687. continue;
  1688. }
  1689. for (let i = 0; i < results.length; i++) {
  1690. const group = results[i];
  1691. const key = this.options.hidePathInfo
  1692. ? hashFilename(group.key, outputOptions)
  1693. : group.key;
  1694. let name = chunk.name
  1695. ? chunk.name + automaticNameDelimiter + key
  1696. : null;
  1697. if (name && name.length > 100) {
  1698. name =
  1699. name.slice(0, 100) +
  1700. automaticNameDelimiter +
  1701. hashFilename(name, outputOptions);
  1702. }
  1703. if (i !== results.length - 1) {
  1704. const newPart = compilation.addChunk(name);
  1705. chunk.split(newPart);
  1706. newPart.chunkReason = chunk.chunkReason;
  1707. if (chunk.filenameTemplate) {
  1708. newPart.filenameTemplate = chunk.filenameTemplate;
  1709. }
  1710. // Add all modules to the new chunk
  1711. for (const module of group.items) {
  1712. if (!module.chunkCondition(newPart, compilation)) {
  1713. continue;
  1714. }
  1715. // Add module to new chunk
  1716. chunkGraph.connectChunkAndModule(newPart, module);
  1717. // Remove module from used chunks
  1718. chunkGraph.disconnectChunkAndModule(chunk, module);
  1719. }
  1720. } else {
  1721. // change the chunk to be a part
  1722. chunk.name = name;
  1723. }
  1724. }
  1725. }
  1726. logger.timeEnd("maxSize");
  1727. }
  1728. );
  1729. });
  1730. }
  1731. };