SplitChunksPlugin.js 56 KB

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