SplitChunksPlugin.js 55 KB

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