SplitChunksPlugin.js 59 KB

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