SplitChunksPlugin.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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. };