DefaultStatsFactoryPlugin.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const { WEBPACK_MODULE_TYPE_RUNTIME } = require("../ModuleTypeConstants");
  8. const ModuleDependency = require("../dependencies/ModuleDependency");
  9. const formatLocation = require("../formatLocation");
  10. const { LogType } = require("../logging/Logger");
  11. const AggressiveSplittingPlugin = require("../optimize/AggressiveSplittingPlugin");
  12. const SizeLimitsPlugin = require("../performance/SizeLimitsPlugin");
  13. const { countIterable } = require("../util/IterableHelpers");
  14. const {
  15. compareLocations,
  16. compareChunksById,
  17. compareNumbers,
  18. compareIds,
  19. concatComparators,
  20. compareSelect,
  21. compareModulesByIdentifier
  22. } = require("../util/comparators");
  23. const { makePathsRelative, parseResource } = require("../util/identifier");
  24. /** @typedef {import("webpack-sources").Source} Source */
  25. /** @typedef {import("../Chunk")} Chunk */
  26. /** @typedef {import("../Chunk").ChunkId} ChunkId */
  27. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  28. /** @typedef {import("../ChunkGroup").OriginRecord} OriginRecord */
  29. /** @typedef {import("../Compilation")} Compilation */
  30. /** @typedef {import("../Compilation").Asset} Asset */
  31. /** @typedef {import("../Compilation").AssetInfo} AssetInfo */
  32. /** @typedef {import("../Compilation").NormalizedStatsOptions} NormalizedStatsOptions */
  33. /** @typedef {import("../Compiler")} Compiler */
  34. /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
  35. /** @typedef {import("../Dependency")} Dependency */
  36. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  37. /** @typedef {import("../Module")} Module */
  38. /** @typedef {import("../Module").BuildInfo} BuildInfo */
  39. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  40. /** @typedef {import("../ModuleProfile")} ModuleProfile */
  41. /** @typedef {import("../RequestShortener")} RequestShortener */
  42. /** @typedef {import("../WebpackError")} WebpackError */
  43. /** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
  44. /**
  45. * @template T
  46. * @typedef {import("../util/comparators").Comparator<T>} Comparator<T>
  47. */
  48. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  49. /**
  50. * @template T, R
  51. * @typedef {import("../util/smartGrouping").GroupConfig<T, R>} GroupConfig
  52. */
  53. /** @typedef {import("./StatsFactory")} StatsFactory */
  54. /** @typedef {import("./StatsFactory").StatsFactoryContext} StatsFactoryContext */
  55. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsCompilation} StatsCompilation */
  56. /**
  57. * @typedef {object} KnownStatsCompilation
  58. * @property {any=} env
  59. * @property {string=} name
  60. * @property {string=} hash
  61. * @property {string=} version
  62. * @property {number=} time
  63. * @property {number=} builtAt
  64. * @property {boolean=} needAdditionalPass
  65. * @property {string=} publicPath
  66. * @property {string=} outputPath
  67. * @property {Record<string, string[]>=} assetsByChunkName
  68. * @property {StatsAsset[]=} assets
  69. * @property {number=} filteredAssets
  70. * @property {StatsChunk[]=} chunks
  71. * @property {StatsModule[]=} modules
  72. * @property {number=} filteredModules
  73. * @property {Record<string, StatsChunkGroup>=} entrypoints
  74. * @property {Record<string, StatsChunkGroup>=} namedChunkGroups
  75. * @property {StatsError[]=} errors
  76. * @property {number=} errorsCount
  77. * @property {StatsError[]=} warnings
  78. * @property {number=} warningsCount
  79. * @property {StatsCompilation[]=} children
  80. * @property {Record<string, StatsLogging>=} logging
  81. */
  82. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsLogging} StatsLogging */
  83. /**
  84. * @typedef {object} KnownStatsLogging
  85. * @property {StatsLoggingEntry[]} entries
  86. * @property {number} filteredEntries
  87. * @property {boolean} debug
  88. */
  89. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsLoggingEntry} StatsLoggingEntry */
  90. /**
  91. * @typedef {object} KnownStatsLoggingEntry
  92. * @property {string} type
  93. * @property {string=} message
  94. * @property {string[]=} trace
  95. * @property {StatsLoggingEntry[]=} children
  96. * @property {any[]=} args
  97. * @property {number=} time
  98. */
  99. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsAsset} StatsAsset */
  100. /**
  101. * @typedef {object} KnownStatsAsset
  102. * @property {string} type
  103. * @property {string} name
  104. * @property {AssetInfo} info
  105. * @property {number} size
  106. * @property {boolean} emitted
  107. * @property {boolean} comparedForEmit
  108. * @property {boolean} cached
  109. * @property {StatsAsset[]=} related
  110. * @property {(string|number)[]=} chunkNames
  111. * @property {(string|number)[]=} chunkIdHints
  112. * @property {(string|number)[]=} chunks
  113. * @property {(string|number)[]=} auxiliaryChunkNames
  114. * @property {(string|number)[]=} auxiliaryChunks
  115. * @property {(string|number)[]=} auxiliaryChunkIdHints
  116. * @property {number=} filteredRelated
  117. * @property {boolean=} isOverSizeLimit
  118. */
  119. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsChunkGroup} StatsChunkGroup */
  120. /**
  121. * @typedef {object} KnownStatsChunkGroup
  122. * @property {(string | null)=} name
  123. * @property {(string | number)[]=} chunks
  124. * @property {({ name: string, size?: number })[]=} assets
  125. * @property {number=} filteredAssets
  126. * @property {number=} assetsSize
  127. * @property {({ name: string, size?: number })[]=} auxiliaryAssets
  128. * @property {number=} filteredAuxiliaryAssets
  129. * @property {number=} auxiliaryAssetsSize
  130. * @property {{ [x: string]: StatsChunkGroup[] }=} children
  131. * @property {{ [x: string]: string[] }=} childAssets
  132. * @property {boolean=} isOverSizeLimit
  133. */
  134. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsModule} StatsModule */
  135. /**
  136. * @typedef {object} KnownStatsModule
  137. * @property {string=} type
  138. * @property {string=} moduleType
  139. * @property {(string | null)=} layer
  140. * @property {string=} identifier
  141. * @property {string=} name
  142. * @property {(string | null)=} nameForCondition
  143. * @property {number=} index
  144. * @property {number=} preOrderIndex
  145. * @property {number=} index2
  146. * @property {number=} postOrderIndex
  147. * @property {number=} size
  148. * @property {{ [x: string]: number }=} sizes
  149. * @property {boolean=} cacheable
  150. * @property {boolean=} built
  151. * @property {boolean=} codeGenerated
  152. * @property {boolean=} buildTimeExecuted
  153. * @property {boolean=} cached
  154. * @property {boolean=} optional
  155. * @property {boolean=} orphan
  156. * @property {string | number=} id
  157. * @property {string | number | null=} issuerId
  158. * @property {(string | number)[]=} chunks
  159. * @property {(string | number)[]=} assets
  160. * @property {boolean=} dependent
  161. * @property {(string | null)=} issuer
  162. * @property {(string | null)=} issuerName
  163. * @property {StatsModuleIssuer[]=} issuerPath
  164. * @property {boolean=} failed
  165. * @property {number=} errors
  166. * @property {number=} warnings
  167. * @property {StatsProfile=} profile
  168. * @property {StatsModuleReason[]=} reasons
  169. * @property {(boolean | null | string[])=} usedExports
  170. * @property {(string[] | null)=} providedExports
  171. * @property {string[]=} optimizationBailout
  172. * @property {(number | null)=} depth
  173. * @property {StatsModule[]=} modules
  174. * @property {number=} filteredModules
  175. * @property {ReturnType<Source["source"]>=} source
  176. */
  177. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsProfile} StatsProfile */
  178. /**
  179. * @typedef {object} KnownStatsProfile
  180. * @property {number} total
  181. * @property {number} resolving
  182. * @property {number} restoring
  183. * @property {number} building
  184. * @property {number} integration
  185. * @property {number} storing
  186. * @property {number} additionalResolving
  187. * @property {number} additionalIntegration
  188. * @property {number} factory
  189. * @property {number} dependencies
  190. */
  191. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsModuleIssuer} StatsModuleIssuer */
  192. /**
  193. * @typedef {object} KnownStatsModuleIssuer
  194. * @property {string} identifier
  195. * @property {string} name
  196. * @property {(string|number)=} id
  197. * @property {StatsProfile} profile
  198. */
  199. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsModuleReason} StatsModuleReason */
  200. /**
  201. * @typedef {object} KnownStatsModuleReason
  202. * @property {string | null} moduleIdentifier
  203. * @property {string | null} module
  204. * @property {string | null} moduleName
  205. * @property {string | null} resolvedModuleIdentifier
  206. * @property {string | null} resolvedModule
  207. * @property {string | null} type
  208. * @property {boolean} active
  209. * @property {string | null} explanation
  210. * @property {string | null} userRequest
  211. * @property {(string | null)=} loc
  212. * @property {(string | number | null)=} moduleId
  213. * @property {(string | number | null)=} resolvedModuleId
  214. */
  215. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsChunk} StatsChunk */
  216. /**
  217. * @typedef {object} KnownStatsChunk
  218. * @property {boolean} rendered
  219. * @property {boolean} initial
  220. * @property {boolean} entry
  221. * @property {boolean} recorded
  222. * @property {string=} reason
  223. * @property {number} size
  224. * @property {Record<string, number>} sizes
  225. * @property {string[]} names
  226. * @property {string[]} idHints
  227. * @property {string[]=} runtime
  228. * @property {string[]} files
  229. * @property {string[]} auxiliaryFiles
  230. * @property {string} hash
  231. * @property {Record<string, (string|number)[]>} childrenByOrder
  232. * @property {(string|number)=} id
  233. * @property {(string|number)[]=} siblings
  234. * @property {(string|number)[]=} parents
  235. * @property {(string|number)[]=} children
  236. * @property {StatsModule[]=} modules
  237. * @property {number=} filteredModules
  238. * @property {StatsChunkOrigin[]=} origins
  239. */
  240. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsChunkOrigin} StatsChunkOrigin */
  241. /**
  242. * @typedef {object} KnownStatsChunkOrigin
  243. * @property {string} module
  244. * @property {string} moduleIdentifier
  245. * @property {string} moduleName
  246. * @property {string} loc
  247. * @property {string} request
  248. * @property {(string | number)=} moduleId
  249. */
  250. /** @typedef { Record<string, EXPECTED_ANY> & KnownStatsModuleTraceItem} StatsModuleTraceItem */
  251. /**
  252. * @typedef {object} KnownStatsModuleTraceItem
  253. * @property {string=} originIdentifier
  254. * @property {string=} originName
  255. * @property {string=} moduleIdentifier
  256. * @property {string=} moduleName
  257. * @property {StatsModuleTraceDependency[]=} dependencies
  258. * @property {(string|number)=} originId
  259. * @property {(string|number)=} moduleId
  260. */
  261. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsModuleTraceDependency} StatsModuleTraceDependency */
  262. /**
  263. * @typedef {object} KnownStatsModuleTraceDependency
  264. * @property {string=} loc
  265. */
  266. /** @typedef {Record<string, EXPECTED_ANY> & KnownStatsError} StatsError */
  267. /**
  268. * @typedef {object} KnownStatsError
  269. * @property {string} message
  270. * @property {string=} chunkName
  271. * @property {boolean=} chunkEntry
  272. * @property {boolean=} chunkInitial
  273. * @property {string=} file
  274. * @property {string=} moduleIdentifier
  275. * @property {string=} moduleName
  276. * @property {string=} loc
  277. * @property {ChunkId=} chunkId
  278. * @property {string|number=} moduleId
  279. * @property {StatsModuleTraceItem[]=} moduleTrace
  280. * @property {any=} details
  281. * @property {string=} stack
  282. */
  283. /** @typedef {Asset & { type: string, related: PreprocessedAsset[] | undefined }} PreprocessedAsset */
  284. /**
  285. * @template T
  286. * @template O
  287. * @typedef {Record<string, (object: O, data: T, context: StatsFactoryContext, options: NormalizedStatsOptions, factory: StatsFactory) => void>} ExtractorsByOption
  288. */
  289. /**
  290. * @typedef {object} SimpleExtractors
  291. * @property {ExtractorsByOption<Compilation, StatsCompilation>} compilation
  292. * @property {ExtractorsByOption<PreprocessedAsset, StatsAsset>} asset
  293. * @property {ExtractorsByOption<PreprocessedAsset, StatsAsset>} asset$visible
  294. * @property {ExtractorsByOption<{ name: string, chunkGroup: ChunkGroup }, StatsChunkGroup>} chunkGroup
  295. * @property {ExtractorsByOption<Module, StatsModule>} module
  296. * @property {ExtractorsByOption<Module, StatsModule>} module$visible
  297. * @property {ExtractorsByOption<Module, StatsModuleIssuer>} moduleIssuer
  298. * @property {ExtractorsByOption<ModuleProfile, StatsProfile>} profile
  299. * @property {ExtractorsByOption<ModuleGraphConnection, StatsModuleReason>} moduleReason
  300. * @property {ExtractorsByOption<Chunk, StatsChunk>} chunk
  301. * @property {ExtractorsByOption<OriginRecord, StatsChunkOrigin>} chunkOrigin
  302. * @property {ExtractorsByOption<WebpackError, StatsError>} error
  303. * @property {ExtractorsByOption<WebpackError, StatsError>} warning
  304. * @property {ExtractorsByOption<{ origin: Module, module: Module }, StatsModuleTraceItem>} moduleTraceItem
  305. * @property {ExtractorsByOption<Dependency, StatsModuleTraceDependency>} moduleTraceDependency
  306. */
  307. /**
  308. * @template T
  309. * @template I
  310. * @param {Iterable<T>} items items to select from
  311. * @param {function(T): Iterable<I>} selector selector function to select values from item
  312. * @returns {I[]} array of values
  313. */
  314. const uniqueArray = (items, selector) => {
  315. /** @type {Set<I>} */
  316. const set = new Set();
  317. for (const item of items) {
  318. for (const i of selector(item)) {
  319. set.add(i);
  320. }
  321. }
  322. return Array.from(set);
  323. };
  324. /**
  325. * @template T
  326. * @template I
  327. * @param {Iterable<T>} items items to select from
  328. * @param {function(T): Iterable<I>} selector selector function to select values from item
  329. * @param {Comparator<I>} comparator comparator function
  330. * @returns {I[]} array of values
  331. */
  332. const uniqueOrderedArray = (items, selector, comparator) =>
  333. uniqueArray(items, selector).sort(comparator);
  334. /** @template T @template R @typedef {{ [P in keyof T]: R }} MappedValues<T, R> */
  335. /**
  336. * @template {object} T
  337. * @template {object} R
  338. * @param {T} obj object to be mapped
  339. * @param {function(T[keyof T], keyof T): R} fn mapping function
  340. * @returns {MappedValues<T, R>} mapped object
  341. */
  342. const mapObject = (obj, fn) => {
  343. const newObj = Object.create(null);
  344. for (const key of Object.keys(obj)) {
  345. newObj[key] = fn(
  346. obj[/** @type {keyof T} */ (key)],
  347. /** @type {keyof T} */ (key)
  348. );
  349. }
  350. return newObj;
  351. };
  352. /**
  353. * @param {Compilation} compilation the compilation
  354. * @param {function(Compilation, string): any[]} getItems get items
  355. * @returns {number} total number
  356. */
  357. const countWithChildren = (compilation, getItems) => {
  358. let count = getItems(compilation, "").length;
  359. for (const child of compilation.children) {
  360. count += countWithChildren(child, (c, type) =>
  361. getItems(c, `.children[].compilation${type}`)
  362. );
  363. }
  364. return count;
  365. };
  366. /** @type {ExtractorsByOption<WebpackError | string, StatsError>} */
  367. const EXTRACT_ERROR = {
  368. _: (object, error, context, { requestShortener }) => {
  369. // TODO webpack 6 disallow strings in the errors/warnings list
  370. if (typeof error === "string") {
  371. object.message = error;
  372. } else {
  373. if (error.chunk) {
  374. object.chunkName = error.chunk.name;
  375. object.chunkEntry = error.chunk.hasRuntime();
  376. object.chunkInitial = error.chunk.canBeInitial();
  377. }
  378. if (error.file) {
  379. object.file = error.file;
  380. }
  381. if (error.module) {
  382. object.moduleIdentifier = error.module.identifier();
  383. object.moduleName = error.module.readableIdentifier(requestShortener);
  384. }
  385. if (error.loc) {
  386. object.loc = formatLocation(error.loc);
  387. }
  388. object.message = error.message;
  389. }
  390. },
  391. ids: (object, error, { compilation: { chunkGraph } }) => {
  392. if (typeof error !== "string") {
  393. if (error.chunk) {
  394. object.chunkId = /** @type {ChunkId} */ (error.chunk.id);
  395. }
  396. if (error.module) {
  397. object.moduleId =
  398. /** @type {ModuleId} */
  399. (chunkGraph.getModuleId(error.module));
  400. }
  401. }
  402. },
  403. moduleTrace: (object, error, context, options, factory) => {
  404. if (typeof error !== "string" && error.module) {
  405. const {
  406. type,
  407. compilation: { moduleGraph }
  408. } = context;
  409. /** @type {Set<Module>} */
  410. const visitedModules = new Set();
  411. const moduleTrace = [];
  412. let current = error.module;
  413. while (current) {
  414. if (visitedModules.has(current)) break; // circular (technically impossible, but how knows)
  415. visitedModules.add(current);
  416. const origin = moduleGraph.getIssuer(current);
  417. if (!origin) break;
  418. moduleTrace.push({ origin, module: current });
  419. current = origin;
  420. }
  421. object.moduleTrace = factory.create(
  422. `${type}.moduleTrace`,
  423. moduleTrace,
  424. context
  425. );
  426. }
  427. },
  428. errorDetails: (
  429. object,
  430. error,
  431. { type, compilation, cachedGetErrors, cachedGetWarnings },
  432. { errorDetails }
  433. ) => {
  434. if (
  435. typeof error !== "string" &&
  436. (errorDetails === true ||
  437. (type.endsWith(".error") && cachedGetErrors(compilation).length < 3))
  438. ) {
  439. object.details = error.details;
  440. }
  441. },
  442. errorStack: (object, error) => {
  443. if (typeof error !== "string") {
  444. object.stack = error.stack;
  445. }
  446. }
  447. };
  448. /** @type {SimpleExtractors} */
  449. const SIMPLE_EXTRACTORS = {
  450. compilation: {
  451. _: (object, compilation, context, options) => {
  452. if (!context.makePathsRelative) {
  453. context.makePathsRelative = makePathsRelative.bindContextCache(
  454. compilation.compiler.context,
  455. compilation.compiler.root
  456. );
  457. }
  458. if (!context.cachedGetErrors) {
  459. const map = new WeakMap();
  460. context.cachedGetErrors = compilation =>
  461. map.get(compilation) ||
  462. // eslint-disable-next-line no-sequences
  463. (errors => (map.set(compilation, errors), errors))(
  464. compilation.getErrors()
  465. );
  466. }
  467. if (!context.cachedGetWarnings) {
  468. const map = new WeakMap();
  469. context.cachedGetWarnings = compilation =>
  470. map.get(compilation) ||
  471. // eslint-disable-next-line no-sequences
  472. (warnings => (map.set(compilation, warnings), warnings))(
  473. compilation.getWarnings()
  474. );
  475. }
  476. if (compilation.name) {
  477. object.name = compilation.name;
  478. }
  479. if (compilation.needAdditionalPass) {
  480. object.needAdditionalPass = true;
  481. }
  482. const { logging, loggingDebug, loggingTrace } = options;
  483. if (logging || (loggingDebug && loggingDebug.length > 0)) {
  484. const util = require("util");
  485. object.logging = {};
  486. let acceptedTypes;
  487. let collapsedGroups = false;
  488. switch (logging) {
  489. case "error":
  490. acceptedTypes = new Set([LogType.error]);
  491. break;
  492. case "warn":
  493. acceptedTypes = new Set([LogType.error, LogType.warn]);
  494. break;
  495. case "info":
  496. acceptedTypes = new Set([
  497. LogType.error,
  498. LogType.warn,
  499. LogType.info
  500. ]);
  501. break;
  502. case "log":
  503. acceptedTypes = new Set([
  504. LogType.error,
  505. LogType.warn,
  506. LogType.info,
  507. LogType.log,
  508. LogType.group,
  509. LogType.groupEnd,
  510. LogType.groupCollapsed,
  511. LogType.clear
  512. ]);
  513. break;
  514. case "verbose":
  515. acceptedTypes = new Set([
  516. LogType.error,
  517. LogType.warn,
  518. LogType.info,
  519. LogType.log,
  520. LogType.group,
  521. LogType.groupEnd,
  522. LogType.groupCollapsed,
  523. LogType.profile,
  524. LogType.profileEnd,
  525. LogType.time,
  526. LogType.status,
  527. LogType.clear
  528. ]);
  529. collapsedGroups = true;
  530. break;
  531. default:
  532. acceptedTypes = new Set();
  533. break;
  534. }
  535. const cachedMakePathsRelative = makePathsRelative.bindContextCache(
  536. options.context,
  537. compilation.compiler.root
  538. );
  539. let depthInCollapsedGroup = 0;
  540. for (const [origin, logEntries] of compilation.logging) {
  541. const debugMode = loggingDebug.some(fn => fn(origin));
  542. if (logging === false && !debugMode) continue;
  543. /** @type {KnownStatsLoggingEntry[]} */
  544. const groupStack = [];
  545. /** @type {KnownStatsLoggingEntry[]} */
  546. const rootList = [];
  547. let currentList = rootList;
  548. let processedLogEntries = 0;
  549. for (const entry of logEntries) {
  550. let type = entry.type;
  551. if (!debugMode && !acceptedTypes.has(type)) continue;
  552. // Expand groups in verbose and debug modes
  553. if (
  554. type === LogType.groupCollapsed &&
  555. (debugMode || collapsedGroups)
  556. )
  557. type = LogType.group;
  558. if (depthInCollapsedGroup === 0) {
  559. processedLogEntries++;
  560. }
  561. if (type === LogType.groupEnd) {
  562. groupStack.pop();
  563. currentList =
  564. groupStack.length > 0
  565. ? /** @type {KnownStatsLoggingEntry[]} */ (
  566. groupStack[groupStack.length - 1].children
  567. )
  568. : rootList;
  569. if (depthInCollapsedGroup > 0) depthInCollapsedGroup--;
  570. continue;
  571. }
  572. let message;
  573. if (entry.type === LogType.time) {
  574. const [label, first, second] =
  575. /** @type {[string, number, number]} */
  576. (entry.args);
  577. message = `${label}: ${first * 1000 + second / 1000000} ms`;
  578. } else if (entry.args && entry.args.length > 0) {
  579. message = util.format(entry.args[0], ...entry.args.slice(1));
  580. }
  581. /** @type {KnownStatsLoggingEntry} */
  582. const newEntry = {
  583. ...entry,
  584. type,
  585. message,
  586. trace: loggingTrace ? entry.trace : undefined,
  587. children:
  588. type === LogType.group || type === LogType.groupCollapsed
  589. ? []
  590. : undefined
  591. };
  592. currentList.push(newEntry);
  593. if (newEntry.children) {
  594. groupStack.push(newEntry);
  595. currentList = newEntry.children;
  596. if (depthInCollapsedGroup > 0) {
  597. depthInCollapsedGroup++;
  598. } else if (type === LogType.groupCollapsed) {
  599. depthInCollapsedGroup = 1;
  600. }
  601. }
  602. }
  603. let name = cachedMakePathsRelative(origin).replace(/\|/g, " ");
  604. if (name in object.logging) {
  605. let i = 1;
  606. while (`${name}#${i}` in object.logging) {
  607. i++;
  608. }
  609. name = `${name}#${i}`;
  610. }
  611. object.logging[name] = {
  612. entries: rootList,
  613. filteredEntries: logEntries.length - processedLogEntries,
  614. debug: debugMode
  615. };
  616. }
  617. }
  618. },
  619. hash: (object, compilation) => {
  620. object.hash = /** @type {string} */ (compilation.hash);
  621. },
  622. version: object => {
  623. object.version = require("../../package.json").version;
  624. },
  625. env: (object, compilation, context, { _env }) => {
  626. object.env = _env;
  627. },
  628. timings: (object, compilation) => {
  629. object.time =
  630. /** @type {number} */ (compilation.endTime) -
  631. /** @type {number} */ (compilation.startTime);
  632. },
  633. builtAt: (object, compilation) => {
  634. object.builtAt = /** @type {number} */ (compilation.endTime);
  635. },
  636. publicPath: (object, compilation) => {
  637. object.publicPath = compilation.getPath(
  638. /** @type {TemplatePath} */
  639. (compilation.outputOptions.publicPath)
  640. );
  641. },
  642. outputPath: (object, compilation) => {
  643. object.outputPath = /** @type {string} */ (
  644. compilation.outputOptions.path
  645. );
  646. },
  647. assets: (object, compilation, context, options, factory) => {
  648. const { type } = context;
  649. /** @type {Map<string, Chunk[]>} */
  650. const compilationFileToChunks = new Map();
  651. /** @type {Map<string, Chunk[]>} */
  652. const compilationAuxiliaryFileToChunks = new Map();
  653. for (const chunk of compilation.chunks) {
  654. for (const file of chunk.files) {
  655. let array = compilationFileToChunks.get(file);
  656. if (array === undefined) {
  657. array = [];
  658. compilationFileToChunks.set(file, array);
  659. }
  660. array.push(chunk);
  661. }
  662. for (const file of chunk.auxiliaryFiles) {
  663. let array = compilationAuxiliaryFileToChunks.get(file);
  664. if (array === undefined) {
  665. array = [];
  666. compilationAuxiliaryFileToChunks.set(file, array);
  667. }
  668. array.push(chunk);
  669. }
  670. }
  671. /** @type {Map<string, PreprocessedAsset>} */
  672. const assetMap = new Map();
  673. /** @type {Set<PreprocessedAsset>} */
  674. const assets = new Set();
  675. for (const asset of compilation.getAssets()) {
  676. /** @type {PreprocessedAsset} */
  677. const item = {
  678. ...asset,
  679. type: "asset",
  680. related: undefined
  681. };
  682. assets.add(item);
  683. assetMap.set(asset.name, item);
  684. }
  685. for (const item of assetMap.values()) {
  686. const related = item.info.related;
  687. if (!related) continue;
  688. for (const type of Object.keys(related)) {
  689. const relatedEntry = related[type];
  690. const deps = Array.isArray(relatedEntry)
  691. ? relatedEntry
  692. : [relatedEntry];
  693. for (const dep of deps) {
  694. const depItem = assetMap.get(dep);
  695. if (!depItem) continue;
  696. assets.delete(depItem);
  697. depItem.type = type;
  698. item.related = item.related || [];
  699. item.related.push(depItem);
  700. }
  701. }
  702. }
  703. object.assetsByChunkName = {};
  704. for (const [file, chunks] of compilationFileToChunks) {
  705. for (const chunk of chunks) {
  706. const name = chunk.name;
  707. if (!name) continue;
  708. if (
  709. !Object.prototype.hasOwnProperty.call(
  710. object.assetsByChunkName,
  711. name
  712. )
  713. ) {
  714. object.assetsByChunkName[name] = [];
  715. }
  716. object.assetsByChunkName[name].push(file);
  717. }
  718. }
  719. const groupedAssets = factory.create(
  720. `${type}.assets`,
  721. Array.from(assets),
  722. {
  723. ...context,
  724. compilationFileToChunks,
  725. compilationAuxiliaryFileToChunks
  726. }
  727. );
  728. const limited = spaceLimited(
  729. groupedAssets,
  730. /** @type {number} */ (options.assetsSpace)
  731. );
  732. object.assets = limited.children;
  733. object.filteredAssets = limited.filteredChildren;
  734. },
  735. chunks: (object, compilation, context, options, factory) => {
  736. const { type } = context;
  737. object.chunks = factory.create(
  738. `${type}.chunks`,
  739. Array.from(compilation.chunks),
  740. context
  741. );
  742. },
  743. modules: (object, compilation, context, options, factory) => {
  744. const { type } = context;
  745. const array = Array.from(compilation.modules);
  746. const groupedModules = factory.create(`${type}.modules`, array, context);
  747. const limited = spaceLimited(groupedModules, options.modulesSpace);
  748. object.modules = limited.children;
  749. object.filteredModules = limited.filteredChildren;
  750. },
  751. entrypoints: (
  752. object,
  753. compilation,
  754. context,
  755. { entrypoints, chunkGroups, chunkGroupAuxiliary, chunkGroupChildren },
  756. factory
  757. ) => {
  758. const { type } = context;
  759. const array = Array.from(compilation.entrypoints, ([key, value]) => ({
  760. name: key,
  761. chunkGroup: value
  762. }));
  763. if (entrypoints === "auto" && !chunkGroups) {
  764. if (array.length > 5) return;
  765. if (
  766. !chunkGroupChildren &&
  767. array.every(({ chunkGroup }) => {
  768. if (chunkGroup.chunks.length !== 1) return false;
  769. const chunk = chunkGroup.chunks[0];
  770. return (
  771. chunk.files.size === 1 &&
  772. (!chunkGroupAuxiliary || chunk.auxiliaryFiles.size === 0)
  773. );
  774. })
  775. ) {
  776. return;
  777. }
  778. }
  779. object.entrypoints = factory.create(
  780. `${type}.entrypoints`,
  781. array,
  782. context
  783. );
  784. },
  785. chunkGroups: (object, compilation, context, options, factory) => {
  786. const { type } = context;
  787. const array = Array.from(
  788. compilation.namedChunkGroups,
  789. ([key, value]) => ({
  790. name: key,
  791. chunkGroup: value
  792. })
  793. );
  794. object.namedChunkGroups = factory.create(
  795. `${type}.namedChunkGroups`,
  796. array,
  797. context
  798. );
  799. },
  800. errors: (object, compilation, context, options, factory) => {
  801. const { type, cachedGetErrors } = context;
  802. const rawErrors = cachedGetErrors(compilation);
  803. const factorizedErrors = factory.create(
  804. `${type}.errors`,
  805. cachedGetErrors(compilation),
  806. context
  807. );
  808. let filtered = 0;
  809. if (options.errorDetails === "auto" && rawErrors.length >= 3) {
  810. filtered = rawErrors
  811. .map(e => typeof e !== "string" && e.details)
  812. .filter(Boolean).length;
  813. }
  814. if (
  815. options.errorDetails === true ||
  816. !Number.isFinite(options.errorsSpace)
  817. ) {
  818. object.errors = factorizedErrors;
  819. if (filtered) object.filteredErrorDetailsCount = filtered;
  820. return;
  821. }
  822. const [errors, filteredBySpace] = errorsSpaceLimit(
  823. factorizedErrors,
  824. /** @type {number} */
  825. (options.errorsSpace)
  826. );
  827. object.filteredErrorDetailsCount = filtered + filteredBySpace;
  828. object.errors = errors;
  829. },
  830. errorsCount: (object, compilation, { cachedGetErrors }) => {
  831. object.errorsCount = countWithChildren(compilation, c =>
  832. cachedGetErrors(c)
  833. );
  834. },
  835. warnings: (object, compilation, context, options, factory) => {
  836. const { type, cachedGetWarnings } = context;
  837. const rawWarnings = factory.create(
  838. `${type}.warnings`,
  839. cachedGetWarnings(compilation),
  840. context
  841. );
  842. let filtered = 0;
  843. if (options.errorDetails === "auto") {
  844. filtered = cachedGetWarnings(compilation)
  845. .map(e => typeof e !== "string" && e.details)
  846. .filter(Boolean).length;
  847. }
  848. if (
  849. options.errorDetails === true ||
  850. !Number.isFinite(options.warningsSpace)
  851. ) {
  852. object.warnings = rawWarnings;
  853. if (filtered) object.filteredWarningDetailsCount = filtered;
  854. return;
  855. }
  856. const [warnings, filteredBySpace] = errorsSpaceLimit(
  857. rawWarnings,
  858. /** @type {number} */
  859. (options.warningsSpace)
  860. );
  861. object.filteredWarningDetailsCount = filtered + filteredBySpace;
  862. object.warnings = warnings;
  863. },
  864. warningsCount: (
  865. object,
  866. compilation,
  867. context,
  868. { warningsFilter },
  869. factory
  870. ) => {
  871. const { type, cachedGetWarnings } = context;
  872. object.warningsCount = countWithChildren(compilation, (c, childType) => {
  873. if (
  874. !warningsFilter &&
  875. /** @type {((warning: StatsError, textValue: string) => boolean)[]} */
  876. (warningsFilter).length === 0
  877. )
  878. return cachedGetWarnings(c);
  879. return factory
  880. .create(`${type}${childType}.warnings`, cachedGetWarnings(c), context)
  881. .filter(
  882. /**
  883. * @param {TODO} warning warning
  884. * @returns {boolean} result
  885. */
  886. warning => {
  887. const warningString = Object.keys(warning)
  888. .map(
  889. key =>
  890. `${warning[/** @type {keyof KnownStatsError} */ (key)]}`
  891. )
  892. .join("\n");
  893. return !warningsFilter.some(filter =>
  894. filter(warning, warningString)
  895. );
  896. }
  897. );
  898. });
  899. },
  900. children: (object, compilation, context, options, factory) => {
  901. const { type } = context;
  902. object.children = factory.create(
  903. `${type}.children`,
  904. compilation.children,
  905. context
  906. );
  907. }
  908. },
  909. asset: {
  910. _: (object, asset, context, options, factory) => {
  911. const { compilation } = context;
  912. object.type = asset.type;
  913. object.name = asset.name;
  914. object.size = asset.source.size();
  915. object.emitted = compilation.emittedAssets.has(asset.name);
  916. object.comparedForEmit = compilation.comparedForEmitAssets.has(
  917. asset.name
  918. );
  919. const cached = !object.emitted && !object.comparedForEmit;
  920. object.cached = cached;
  921. object.info = asset.info;
  922. if (!cached || options.cachedAssets) {
  923. Object.assign(
  924. object,
  925. factory.create(`${context.type}$visible`, asset, context)
  926. );
  927. }
  928. }
  929. },
  930. asset$visible: {
  931. _: (
  932. object,
  933. asset,
  934. { compilation, compilationFileToChunks, compilationAuxiliaryFileToChunks }
  935. ) => {
  936. const chunks = compilationFileToChunks.get(asset.name) || [];
  937. const auxiliaryChunks =
  938. compilationAuxiliaryFileToChunks.get(asset.name) || [];
  939. object.chunkNames = uniqueOrderedArray(
  940. chunks,
  941. c => (c.name ? [c.name] : []),
  942. compareIds
  943. );
  944. object.chunkIdHints = uniqueOrderedArray(
  945. chunks,
  946. c => Array.from(c.idNameHints),
  947. compareIds
  948. );
  949. object.auxiliaryChunkNames = uniqueOrderedArray(
  950. auxiliaryChunks,
  951. c => (c.name ? [c.name] : []),
  952. compareIds
  953. );
  954. object.auxiliaryChunkIdHints = uniqueOrderedArray(
  955. auxiliaryChunks,
  956. c => Array.from(c.idNameHints),
  957. compareIds
  958. );
  959. object.filteredRelated = asset.related ? asset.related.length : undefined;
  960. },
  961. relatedAssets: (object, asset, context, options, factory) => {
  962. const { type } = context;
  963. object.related = factory.create(
  964. `${type.slice(0, -8)}.related`,
  965. asset.related || [],
  966. context
  967. );
  968. object.filteredRelated = asset.related
  969. ? asset.related.length -
  970. /** @type {StatsAsset[]} */ (object.related).length
  971. : undefined;
  972. },
  973. ids: (
  974. object,
  975. asset,
  976. { compilationFileToChunks, compilationAuxiliaryFileToChunks }
  977. ) => {
  978. const chunks = compilationFileToChunks.get(asset.name) || [];
  979. const auxiliaryChunks =
  980. compilationAuxiliaryFileToChunks.get(asset.name) || [];
  981. object.chunks = uniqueOrderedArray(
  982. chunks,
  983. c => /** @type {ChunkId[]} */ (c.ids),
  984. compareIds
  985. );
  986. object.auxiliaryChunks = uniqueOrderedArray(
  987. auxiliaryChunks,
  988. c => /** @type {ChunkId[]} */ (c.ids),
  989. compareIds
  990. );
  991. },
  992. performance: (object, asset) => {
  993. object.isOverSizeLimit = SizeLimitsPlugin.isOverSizeLimit(asset.source);
  994. }
  995. },
  996. chunkGroup: {
  997. _: (
  998. object,
  999. { name, chunkGroup },
  1000. { compilation, compilation: { moduleGraph, chunkGraph } },
  1001. { ids, chunkGroupAuxiliary, chunkGroupChildren, chunkGroupMaxAssets }
  1002. ) => {
  1003. const children =
  1004. chunkGroupChildren &&
  1005. chunkGroup.getChildrenByOrders(moduleGraph, chunkGraph);
  1006. /**
  1007. * @param {string} name Name
  1008. * @returns {{ name: string, size: number }} Asset object
  1009. */
  1010. const toAsset = name => {
  1011. const asset = compilation.getAsset(name);
  1012. return {
  1013. name,
  1014. size: /** @type {number} */ (asset ? asset.info.size : -1)
  1015. };
  1016. };
  1017. /** @type {(total: number, asset: { size: number }) => number} */
  1018. const sizeReducer = (total, { size }) => total + size;
  1019. const assets = uniqueArray(chunkGroup.chunks, c => c.files).map(toAsset);
  1020. const auxiliaryAssets = uniqueOrderedArray(
  1021. chunkGroup.chunks,
  1022. c => c.auxiliaryFiles,
  1023. compareIds
  1024. ).map(toAsset);
  1025. const assetsSize = assets.reduce(sizeReducer, 0);
  1026. const auxiliaryAssetsSize = auxiliaryAssets.reduce(sizeReducer, 0);
  1027. /** @type {KnownStatsChunkGroup} */
  1028. const statsChunkGroup = {
  1029. name,
  1030. chunks: ids
  1031. ? /** @type {ChunkId[]} */ (chunkGroup.chunks.map(c => c.id))
  1032. : undefined,
  1033. assets: assets.length <= chunkGroupMaxAssets ? assets : undefined,
  1034. filteredAssets:
  1035. assets.length <= chunkGroupMaxAssets ? 0 : assets.length,
  1036. assetsSize,
  1037. auxiliaryAssets:
  1038. chunkGroupAuxiliary && auxiliaryAssets.length <= chunkGroupMaxAssets
  1039. ? auxiliaryAssets
  1040. : undefined,
  1041. filteredAuxiliaryAssets:
  1042. chunkGroupAuxiliary && auxiliaryAssets.length <= chunkGroupMaxAssets
  1043. ? 0
  1044. : auxiliaryAssets.length,
  1045. auxiliaryAssetsSize,
  1046. children: children
  1047. ? mapObject(children, groups =>
  1048. groups.map(group => {
  1049. const assets = uniqueArray(group.chunks, c => c.files).map(
  1050. toAsset
  1051. );
  1052. const auxiliaryAssets = uniqueOrderedArray(
  1053. group.chunks,
  1054. c => c.auxiliaryFiles,
  1055. compareIds
  1056. ).map(toAsset);
  1057. /** @type {KnownStatsChunkGroup} */
  1058. const childStatsChunkGroup = {
  1059. name: group.name,
  1060. chunks: ids
  1061. ? /** @type {ChunkId[]} */
  1062. (group.chunks.map(c => c.id))
  1063. : undefined,
  1064. assets:
  1065. assets.length <= chunkGroupMaxAssets ? assets : undefined,
  1066. filteredAssets:
  1067. assets.length <= chunkGroupMaxAssets ? 0 : assets.length,
  1068. auxiliaryAssets:
  1069. chunkGroupAuxiliary &&
  1070. auxiliaryAssets.length <= chunkGroupMaxAssets
  1071. ? auxiliaryAssets
  1072. : undefined,
  1073. filteredAuxiliaryAssets:
  1074. chunkGroupAuxiliary &&
  1075. auxiliaryAssets.length <= chunkGroupMaxAssets
  1076. ? 0
  1077. : auxiliaryAssets.length
  1078. };
  1079. return childStatsChunkGroup;
  1080. })
  1081. )
  1082. : undefined,
  1083. childAssets: children
  1084. ? mapObject(children, groups => {
  1085. /** @type {Set<string>} */
  1086. const set = new Set();
  1087. for (const group of groups) {
  1088. for (const chunk of group.chunks) {
  1089. for (const asset of chunk.files) {
  1090. set.add(asset);
  1091. }
  1092. }
  1093. }
  1094. return Array.from(set);
  1095. })
  1096. : undefined
  1097. };
  1098. Object.assign(object, statsChunkGroup);
  1099. },
  1100. performance: (object, { chunkGroup }) => {
  1101. object.isOverSizeLimit = SizeLimitsPlugin.isOverSizeLimit(chunkGroup);
  1102. }
  1103. },
  1104. module: {
  1105. _: (object, module, context, options, factory) => {
  1106. const { type } = context;
  1107. const compilation = /** @type {Compilation} */ (context.compilation);
  1108. const built = compilation.builtModules.has(module);
  1109. const codeGenerated = compilation.codeGeneratedModules.has(module);
  1110. const buildTimeExecuted =
  1111. compilation.buildTimeExecutedModules.has(module);
  1112. /** @type {{[x: string]: number}} */
  1113. const sizes = {};
  1114. for (const sourceType of module.getSourceTypes()) {
  1115. sizes[sourceType] = module.size(sourceType);
  1116. }
  1117. /** @type {KnownStatsModule} */
  1118. const statsModule = {
  1119. type: "module",
  1120. moduleType: module.type,
  1121. layer: module.layer,
  1122. size: module.size(),
  1123. sizes,
  1124. built,
  1125. codeGenerated,
  1126. buildTimeExecuted,
  1127. cached: !built && !codeGenerated
  1128. };
  1129. Object.assign(object, statsModule);
  1130. if (built || codeGenerated || options.cachedModules) {
  1131. Object.assign(
  1132. object,
  1133. factory.create(`${type}$visible`, module, context)
  1134. );
  1135. }
  1136. }
  1137. },
  1138. module$visible: {
  1139. _: (object, module, context, { requestShortener }, factory) => {
  1140. const { type, rootModules } = context;
  1141. const compilation = /** @type {Compilation} */ (context.compilation);
  1142. const { moduleGraph } = compilation;
  1143. /** @type {Module[]} */
  1144. const path = [];
  1145. const issuer = moduleGraph.getIssuer(module);
  1146. let current = issuer;
  1147. while (current) {
  1148. path.push(current);
  1149. current = moduleGraph.getIssuer(current);
  1150. }
  1151. path.reverse();
  1152. const profile = moduleGraph.getProfile(module);
  1153. const errors = module.getErrors();
  1154. const errorsCount = errors !== undefined ? countIterable(errors) : 0;
  1155. const warnings = module.getWarnings();
  1156. const warningsCount =
  1157. warnings !== undefined ? countIterable(warnings) : 0;
  1158. /** @type {KnownStatsModule} */
  1159. const statsModule = {
  1160. identifier: module.identifier(),
  1161. name: module.readableIdentifier(requestShortener),
  1162. nameForCondition: module.nameForCondition(),
  1163. index: /** @type {number} */ (moduleGraph.getPreOrderIndex(module)),
  1164. preOrderIndex: /** @type {number} */ (
  1165. moduleGraph.getPreOrderIndex(module)
  1166. ),
  1167. index2: /** @type {number} */ (moduleGraph.getPostOrderIndex(module)),
  1168. postOrderIndex: /** @type {number} */ (
  1169. moduleGraph.getPostOrderIndex(module)
  1170. ),
  1171. cacheable: /** @type {BuildInfo} */ (module.buildInfo).cacheable,
  1172. optional: module.isOptional(moduleGraph),
  1173. orphan:
  1174. !type.endsWith("module.modules[].module$visible") &&
  1175. compilation.chunkGraph.getNumberOfModuleChunks(module) === 0,
  1176. dependent: rootModules ? !rootModules.has(module) : undefined,
  1177. issuer: issuer && issuer.identifier(),
  1178. issuerName: issuer && issuer.readableIdentifier(requestShortener),
  1179. issuerPath:
  1180. issuer &&
  1181. factory.create(`${type.slice(0, -8)}.issuerPath`, path, context),
  1182. failed: errorsCount > 0,
  1183. errors: errorsCount,
  1184. warnings: warningsCount
  1185. };
  1186. Object.assign(object, statsModule);
  1187. if (profile) {
  1188. object.profile = factory.create(
  1189. `${type.slice(0, -8)}.profile`,
  1190. profile,
  1191. context
  1192. );
  1193. }
  1194. },
  1195. ids: (object, module, { compilation: { chunkGraph, moduleGraph } }) => {
  1196. object.id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  1197. const issuer = moduleGraph.getIssuer(module);
  1198. object.issuerId = issuer && chunkGraph.getModuleId(issuer);
  1199. object.chunks =
  1200. /** @type {ChunkId[]} */
  1201. (
  1202. Array.from(
  1203. chunkGraph.getOrderedModuleChunksIterable(
  1204. module,
  1205. compareChunksById
  1206. ),
  1207. chunk => chunk.id
  1208. )
  1209. );
  1210. },
  1211. moduleAssets: (object, module) => {
  1212. object.assets = /** @type {BuildInfo} */ (module.buildInfo).assets
  1213. ? Object.keys(/** @type {BuildInfo} */ (module.buildInfo).assets)
  1214. : [];
  1215. },
  1216. reasons: (object, module, context, options, factory) => {
  1217. const {
  1218. type,
  1219. compilation: { moduleGraph }
  1220. } = context;
  1221. const groupsReasons = factory.create(
  1222. `${type.slice(0, -8)}.reasons`,
  1223. Array.from(moduleGraph.getIncomingConnections(module)),
  1224. context
  1225. );
  1226. const limited = spaceLimited(
  1227. groupsReasons,
  1228. /** @type {number} */
  1229. (options.reasonsSpace)
  1230. );
  1231. object.reasons = limited.children;
  1232. object.filteredReasons = limited.filteredChildren;
  1233. },
  1234. usedExports: (
  1235. object,
  1236. module,
  1237. { runtime, compilation: { moduleGraph } }
  1238. ) => {
  1239. const usedExports = moduleGraph.getUsedExports(module, runtime);
  1240. if (usedExports === null) {
  1241. object.usedExports = null;
  1242. } else if (typeof usedExports === "boolean") {
  1243. object.usedExports = usedExports;
  1244. } else {
  1245. object.usedExports = Array.from(usedExports);
  1246. }
  1247. },
  1248. providedExports: (object, module, { compilation: { moduleGraph } }) => {
  1249. const providedExports = moduleGraph.getProvidedExports(module);
  1250. object.providedExports = Array.isArray(providedExports)
  1251. ? providedExports
  1252. : null;
  1253. },
  1254. optimizationBailout: (
  1255. object,
  1256. module,
  1257. { compilation: { moduleGraph } },
  1258. { requestShortener }
  1259. ) => {
  1260. object.optimizationBailout = moduleGraph
  1261. .getOptimizationBailout(module)
  1262. .map(item => {
  1263. if (typeof item === "function") return item(requestShortener);
  1264. return item;
  1265. });
  1266. },
  1267. depth: (object, module, { compilation: { moduleGraph } }) => {
  1268. object.depth = moduleGraph.getDepth(module);
  1269. },
  1270. nestedModules: (object, module, context, options, factory) => {
  1271. const { type } = context;
  1272. const innerModules = /** @type {Module & { modules?: Module[] }} */ (
  1273. module
  1274. ).modules;
  1275. if (Array.isArray(innerModules)) {
  1276. const groupedModules = factory.create(
  1277. `${type.slice(0, -8)}.modules`,
  1278. innerModules,
  1279. context
  1280. );
  1281. const limited = spaceLimited(
  1282. groupedModules,
  1283. options.nestedModulesSpace
  1284. );
  1285. object.modules = limited.children;
  1286. object.filteredModules = limited.filteredChildren;
  1287. }
  1288. },
  1289. source: (object, module) => {
  1290. const originalSource = module.originalSource();
  1291. if (originalSource) {
  1292. object.source = originalSource.source();
  1293. }
  1294. }
  1295. },
  1296. profile: {
  1297. _: (object, profile) => {
  1298. /** @type {KnownStatsProfile} */
  1299. const statsProfile = {
  1300. total:
  1301. profile.factory +
  1302. profile.restoring +
  1303. profile.integration +
  1304. profile.building +
  1305. profile.storing,
  1306. resolving: profile.factory,
  1307. restoring: profile.restoring,
  1308. building: profile.building,
  1309. integration: profile.integration,
  1310. storing: profile.storing,
  1311. additionalResolving: profile.additionalFactories,
  1312. additionalIntegration: profile.additionalIntegration,
  1313. // TODO remove this in webpack 6
  1314. factory: profile.factory,
  1315. // TODO remove this in webpack 6
  1316. dependencies: profile.additionalFactories
  1317. };
  1318. Object.assign(object, statsProfile);
  1319. }
  1320. },
  1321. moduleIssuer: {
  1322. _: (object, module, context, { requestShortener }, factory) => {
  1323. const { type } = context;
  1324. const compilation = /** @type {Compilation} */ (context.compilation);
  1325. const { moduleGraph } = compilation;
  1326. const profile = moduleGraph.getProfile(module);
  1327. /** @type {Partial<KnownStatsModuleIssuer>} */
  1328. const statsModuleIssuer = {
  1329. identifier: module.identifier(),
  1330. name: module.readableIdentifier(requestShortener)
  1331. };
  1332. Object.assign(object, statsModuleIssuer);
  1333. if (profile) {
  1334. object.profile = factory.create(`${type}.profile`, profile, context);
  1335. }
  1336. },
  1337. ids: (object, module, { compilation: { chunkGraph } }) => {
  1338. object.id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  1339. }
  1340. },
  1341. moduleReason: {
  1342. _: (object, reason, { runtime }, { requestShortener }) => {
  1343. const dep = reason.dependency;
  1344. const moduleDep =
  1345. dep && dep instanceof ModuleDependency ? dep : undefined;
  1346. /** @type {KnownStatsModuleReason} */
  1347. const statsModuleReason = {
  1348. moduleIdentifier: reason.originModule
  1349. ? reason.originModule.identifier()
  1350. : null,
  1351. module: reason.originModule
  1352. ? reason.originModule.readableIdentifier(requestShortener)
  1353. : null,
  1354. moduleName: reason.originModule
  1355. ? reason.originModule.readableIdentifier(requestShortener)
  1356. : null,
  1357. resolvedModuleIdentifier: reason.resolvedOriginModule
  1358. ? reason.resolvedOriginModule.identifier()
  1359. : null,
  1360. resolvedModule: reason.resolvedOriginModule
  1361. ? reason.resolvedOriginModule.readableIdentifier(requestShortener)
  1362. : null,
  1363. type: reason.dependency ? reason.dependency.type : null,
  1364. active: reason.isActive(runtime),
  1365. explanation: reason.explanation,
  1366. userRequest: (moduleDep && moduleDep.userRequest) || null
  1367. };
  1368. Object.assign(object, statsModuleReason);
  1369. if (reason.dependency) {
  1370. const locInfo = formatLocation(reason.dependency.loc);
  1371. if (locInfo) {
  1372. object.loc = locInfo;
  1373. }
  1374. }
  1375. },
  1376. ids: (object, reason, { compilation: { chunkGraph } }) => {
  1377. object.moduleId = reason.originModule
  1378. ? chunkGraph.getModuleId(reason.originModule)
  1379. : null;
  1380. object.resolvedModuleId = reason.resolvedOriginModule
  1381. ? chunkGraph.getModuleId(reason.resolvedOriginModule)
  1382. : null;
  1383. }
  1384. },
  1385. chunk: {
  1386. _: (object, chunk, { makePathsRelative, compilation: { chunkGraph } }) => {
  1387. const childIdByOrder = chunk.getChildIdsByOrders(chunkGraph);
  1388. /** @type {KnownStatsChunk} */
  1389. const statsChunk = {
  1390. rendered: chunk.rendered,
  1391. initial: chunk.canBeInitial(),
  1392. entry: chunk.hasRuntime(),
  1393. recorded: AggressiveSplittingPlugin.wasChunkRecorded(chunk),
  1394. reason: chunk.chunkReason,
  1395. size: chunkGraph.getChunkModulesSize(chunk),
  1396. sizes: chunkGraph.getChunkModulesSizes(chunk),
  1397. names: chunk.name ? [chunk.name] : [],
  1398. idHints: Array.from(chunk.idNameHints),
  1399. runtime:
  1400. chunk.runtime === undefined
  1401. ? undefined
  1402. : typeof chunk.runtime === "string"
  1403. ? [makePathsRelative(chunk.runtime)]
  1404. : Array.from(chunk.runtime.sort(), makePathsRelative),
  1405. files: Array.from(chunk.files),
  1406. auxiliaryFiles: Array.from(chunk.auxiliaryFiles).sort(compareIds),
  1407. hash: /** @type {string} */ (chunk.renderedHash),
  1408. childrenByOrder: childIdByOrder
  1409. };
  1410. Object.assign(object, statsChunk);
  1411. },
  1412. ids: (object, chunk) => {
  1413. object.id = /** @type {ChunkId} */ (chunk.id);
  1414. },
  1415. chunkRelations: (object, chunk, { compilation: { chunkGraph } }) => {
  1416. /** @type {Set<string|number>} */
  1417. const parents = new Set();
  1418. /** @type {Set<string|number>} */
  1419. const children = new Set();
  1420. /** @type {Set<string|number>} */
  1421. const siblings = new Set();
  1422. for (const chunkGroup of chunk.groupsIterable) {
  1423. for (const parentGroup of chunkGroup.parentsIterable) {
  1424. for (const chunk of parentGroup.chunks) {
  1425. parents.add(/** @type {ChunkId} */ (chunk.id));
  1426. }
  1427. }
  1428. for (const childGroup of chunkGroup.childrenIterable) {
  1429. for (const chunk of childGroup.chunks) {
  1430. children.add(/** @type {ChunkId} */ (chunk.id));
  1431. }
  1432. }
  1433. for (const sibling of chunkGroup.chunks) {
  1434. if (sibling !== chunk)
  1435. siblings.add(/** @type {ChunkId} */ (sibling.id));
  1436. }
  1437. }
  1438. object.siblings = Array.from(siblings).sort(compareIds);
  1439. object.parents = Array.from(parents).sort(compareIds);
  1440. object.children = Array.from(children).sort(compareIds);
  1441. },
  1442. chunkModules: (object, chunk, context, options, factory) => {
  1443. const {
  1444. type,
  1445. compilation: { chunkGraph }
  1446. } = context;
  1447. const array = chunkGraph.getChunkModules(chunk);
  1448. const groupedModules = factory.create(`${type}.modules`, array, {
  1449. ...context,
  1450. runtime: chunk.runtime,
  1451. rootModules: new Set(chunkGraph.getChunkRootModules(chunk))
  1452. });
  1453. const limited = spaceLimited(groupedModules, options.chunkModulesSpace);
  1454. object.modules = limited.children;
  1455. object.filteredModules = limited.filteredChildren;
  1456. },
  1457. chunkOrigins: (object, chunk, context, options, factory) => {
  1458. const {
  1459. type,
  1460. compilation: { chunkGraph }
  1461. } = context;
  1462. /** @type {Set<string>} */
  1463. const originsKeySet = new Set();
  1464. const origins = [];
  1465. for (const g of chunk.groupsIterable) {
  1466. origins.push(...g.origins);
  1467. }
  1468. const array = origins.filter(origin => {
  1469. const key = [
  1470. origin.module ? chunkGraph.getModuleId(origin.module) : undefined,
  1471. formatLocation(origin.loc),
  1472. origin.request
  1473. ].join();
  1474. if (originsKeySet.has(key)) return false;
  1475. originsKeySet.add(key);
  1476. return true;
  1477. });
  1478. object.origins = factory.create(`${type}.origins`, array, context);
  1479. }
  1480. },
  1481. chunkOrigin: {
  1482. _: (object, origin, context, { requestShortener }) => {
  1483. /** @type {KnownStatsChunkOrigin} */
  1484. const statsChunkOrigin = {
  1485. module: origin.module ? origin.module.identifier() : "",
  1486. moduleIdentifier: origin.module ? origin.module.identifier() : "",
  1487. moduleName: origin.module
  1488. ? origin.module.readableIdentifier(requestShortener)
  1489. : "",
  1490. loc: formatLocation(origin.loc),
  1491. request: origin.request
  1492. };
  1493. Object.assign(object, statsChunkOrigin);
  1494. },
  1495. ids: (object, origin, { compilation: { chunkGraph } }) => {
  1496. object.moduleId = origin.module
  1497. ? /** @type {ModuleId} */ (chunkGraph.getModuleId(origin.module))
  1498. : undefined;
  1499. }
  1500. },
  1501. error: EXTRACT_ERROR,
  1502. warning: EXTRACT_ERROR,
  1503. moduleTraceItem: {
  1504. _: (object, { origin, module }, context, { requestShortener }, factory) => {
  1505. const {
  1506. type,
  1507. compilation: { moduleGraph }
  1508. } = context;
  1509. object.originIdentifier = origin.identifier();
  1510. object.originName = origin.readableIdentifier(requestShortener);
  1511. object.moduleIdentifier = module.identifier();
  1512. object.moduleName = module.readableIdentifier(requestShortener);
  1513. const dependencies = Array.from(
  1514. moduleGraph.getIncomingConnections(module)
  1515. )
  1516. .filter(c => c.resolvedOriginModule === origin && c.dependency)
  1517. .map(c => c.dependency);
  1518. object.dependencies = factory.create(
  1519. `${type}.dependencies`,
  1520. Array.from(new Set(dependencies)),
  1521. context
  1522. );
  1523. },
  1524. ids: (object, { origin, module }, { compilation: { chunkGraph } }) => {
  1525. object.originId =
  1526. /** @type {ModuleId} */
  1527. (chunkGraph.getModuleId(origin));
  1528. object.moduleId =
  1529. /** @type {ModuleId} */
  1530. (chunkGraph.getModuleId(module));
  1531. }
  1532. },
  1533. moduleTraceDependency: {
  1534. _: (object, dependency) => {
  1535. object.loc = formatLocation(dependency.loc);
  1536. }
  1537. }
  1538. };
  1539. /** @type {Record<string, Record<string, (thing: any, context: StatsFactoryContext, options: NormalizedStatsOptions) => boolean | undefined>>} */
  1540. const FILTER = {
  1541. "module.reasons": {
  1542. "!orphanModules": (reason, { compilation: { chunkGraph } }) => {
  1543. if (
  1544. reason.originModule &&
  1545. chunkGraph.getNumberOfModuleChunks(reason.originModule) === 0
  1546. ) {
  1547. return false;
  1548. }
  1549. }
  1550. }
  1551. };
  1552. /** @type {Record<string, Record<string, (thing: KnownStatsError, context: StatsFactoryContext, options: NormalizedStatsOptions) => boolean | undefined>>} */
  1553. const FILTER_RESULTS = {
  1554. "compilation.warnings": {
  1555. warningsFilter: util.deprecate(
  1556. (warning, context, { warningsFilter }) => {
  1557. const warningString = Object.keys(warning)
  1558. .map(key => `${warning[/** @type {keyof KnownStatsError} */ (key)]}`)
  1559. .join("\n");
  1560. return !warningsFilter.some(filter => filter(warning, warningString));
  1561. },
  1562. "config.stats.warningsFilter is deprecated in favor of config.ignoreWarnings",
  1563. "DEP_WEBPACK_STATS_WARNINGS_FILTER"
  1564. )
  1565. }
  1566. };
  1567. /** @type {Record<string, (comparators: Function[], context: StatsFactoryContext) => void>} */
  1568. const MODULES_SORTER = {
  1569. _: (comparators, { compilation: { moduleGraph } }) => {
  1570. comparators.push(
  1571. compareSelect(
  1572. /**
  1573. * @param {Module} m module
  1574. * @returns {number | null} depth
  1575. */
  1576. m => moduleGraph.getDepth(m),
  1577. compareNumbers
  1578. ),
  1579. compareSelect(
  1580. /**
  1581. * @param {Module} m module
  1582. * @returns {number | null} index
  1583. */
  1584. m => moduleGraph.getPreOrderIndex(m),
  1585. compareNumbers
  1586. ),
  1587. compareSelect(
  1588. /**
  1589. * @param {Module} m module
  1590. * @returns {string} identifier
  1591. */
  1592. m => m.identifier(),
  1593. compareIds
  1594. )
  1595. );
  1596. }
  1597. };
  1598. /** @type {Record<string, Record<string, (comparators: Function[], context: StatsFactoryContext) => void>>} */
  1599. const SORTERS = {
  1600. "compilation.chunks": {
  1601. _: comparators => {
  1602. comparators.push(compareSelect(c => c.id, compareIds));
  1603. }
  1604. },
  1605. "compilation.modules": MODULES_SORTER,
  1606. "chunk.rootModules": MODULES_SORTER,
  1607. "chunk.modules": MODULES_SORTER,
  1608. "module.modules": MODULES_SORTER,
  1609. "module.reasons": {
  1610. _: (comparators, { compilation: { chunkGraph } }) => {
  1611. comparators.push(
  1612. compareSelect(x => x.originModule, compareModulesByIdentifier)
  1613. );
  1614. comparators.push(
  1615. compareSelect(x => x.resolvedOriginModule, compareModulesByIdentifier)
  1616. );
  1617. comparators.push(
  1618. compareSelect(
  1619. x => x.dependency,
  1620. concatComparators(
  1621. compareSelect(
  1622. /**
  1623. * @param {Dependency} x dependency
  1624. * @returns {DependencyLocation} location
  1625. */
  1626. x => x.loc,
  1627. compareLocations
  1628. ),
  1629. compareSelect(x => x.type, compareIds)
  1630. )
  1631. )
  1632. );
  1633. }
  1634. },
  1635. "chunk.origins": {
  1636. _: (comparators, { compilation: { chunkGraph } }) => {
  1637. comparators.push(
  1638. compareSelect(
  1639. origin =>
  1640. origin.module ? chunkGraph.getModuleId(origin.module) : undefined,
  1641. compareIds
  1642. ),
  1643. compareSelect(origin => formatLocation(origin.loc), compareIds),
  1644. compareSelect(origin => origin.request, compareIds)
  1645. );
  1646. }
  1647. }
  1648. };
  1649. /**
  1650. * @template T
  1651. * @typedef {T & { children: Children<T>[] | undefined, filteredChildren?: number }} Children
  1652. */
  1653. /**
  1654. * @template T
  1655. * @param {Children<T>} item item
  1656. * @returns {number} item size
  1657. */
  1658. const getItemSize = item =>
  1659. // Each item takes 1 line
  1660. // + the size of the children
  1661. // + 1 extra line when it has children and filteredChildren
  1662. !item.children
  1663. ? 1
  1664. : item.filteredChildren
  1665. ? 2 + getTotalSize(item.children)
  1666. : 1 + getTotalSize(item.children);
  1667. /**
  1668. * @template T
  1669. * @param {Children<T>[]} children children
  1670. * @returns {number} total size
  1671. */
  1672. const getTotalSize = children => {
  1673. let size = 0;
  1674. for (const child of children) {
  1675. size += getItemSize(child);
  1676. }
  1677. return size;
  1678. };
  1679. /**
  1680. * @template T
  1681. * @param {Children<T>[]} children children
  1682. * @returns {number} total items
  1683. */
  1684. const getTotalItems = children => {
  1685. let count = 0;
  1686. for (const child of children) {
  1687. if (!child.children && !child.filteredChildren) {
  1688. count++;
  1689. } else {
  1690. if (child.children) count += getTotalItems(child.children);
  1691. if (child.filteredChildren) count += child.filteredChildren;
  1692. }
  1693. }
  1694. return count;
  1695. };
  1696. /**
  1697. * @template T
  1698. * @param {Children<T>[]} children children
  1699. * @returns {Children<T>[]} collapsed children
  1700. */
  1701. const collapse = children => {
  1702. // After collapse each child must take exactly one line
  1703. const newChildren = [];
  1704. for (const child of children) {
  1705. if (child.children) {
  1706. let filteredChildren = child.filteredChildren || 0;
  1707. filteredChildren += getTotalItems(child.children);
  1708. newChildren.push({
  1709. ...child,
  1710. children: undefined,
  1711. filteredChildren
  1712. });
  1713. } else {
  1714. newChildren.push(child);
  1715. }
  1716. }
  1717. return newChildren;
  1718. };
  1719. /**
  1720. * @template T
  1721. * @param {Children<T>[]} itemsAndGroups item and groups
  1722. * @param {number} max max
  1723. * @param {boolean=} filteredChildrenLineReserved filtered children line reserved
  1724. * @returns {Children<T>} result
  1725. */
  1726. const spaceLimited = (
  1727. itemsAndGroups,
  1728. max,
  1729. filteredChildrenLineReserved = false
  1730. ) => {
  1731. if (max < 1) {
  1732. return /** @type {Children<T>} */ ({
  1733. children: undefined,
  1734. filteredChildren: getTotalItems(itemsAndGroups)
  1735. });
  1736. }
  1737. /** @type {Children<T>[] | undefined} */
  1738. let children;
  1739. /** @type {number | undefined} */
  1740. let filteredChildren;
  1741. // This are the groups, which take 1+ lines each
  1742. /** @type {Children<T>[] | undefined} */
  1743. const groups = [];
  1744. // The sizes of the groups are stored in groupSizes
  1745. /** @type {number[]} */
  1746. const groupSizes = [];
  1747. // This are the items, which take 1 line each
  1748. const items = [];
  1749. // The total of group sizes
  1750. let groupsSize = 0;
  1751. for (const itemOrGroup of itemsAndGroups) {
  1752. // is item
  1753. if (!itemOrGroup.children && !itemOrGroup.filteredChildren) {
  1754. items.push(itemOrGroup);
  1755. } else {
  1756. groups.push(itemOrGroup);
  1757. const size = getItemSize(itemOrGroup);
  1758. groupSizes.push(size);
  1759. groupsSize += size;
  1760. }
  1761. }
  1762. if (groupsSize + items.length <= max) {
  1763. // The total size in the current state fits into the max
  1764. // keep all
  1765. children = groups.length > 0 ? groups.concat(items) : items;
  1766. } else if (groups.length === 0) {
  1767. // slice items to max
  1768. // inner space marks that lines for filteredChildren already reserved
  1769. const limit = max - (filteredChildrenLineReserved ? 0 : 1);
  1770. filteredChildren = items.length - limit;
  1771. items.length = limit;
  1772. children = items;
  1773. } else {
  1774. // limit is the size when all groups are collapsed
  1775. const limit =
  1776. groups.length +
  1777. (filteredChildrenLineReserved || items.length === 0 ? 0 : 1);
  1778. if (limit < max) {
  1779. // calculate how much we are over the size limit
  1780. // this allows to approach the limit faster
  1781. let oversize;
  1782. // If each group would take 1 line the total would be below the maximum
  1783. // collapse some groups, keep items
  1784. while (
  1785. (oversize =
  1786. groupsSize +
  1787. items.length +
  1788. (filteredChildren && !filteredChildrenLineReserved ? 1 : 0) -
  1789. max) > 0
  1790. ) {
  1791. // Find the maximum group and process only this one
  1792. const maxGroupSize = Math.max(...groupSizes);
  1793. if (maxGroupSize < items.length) {
  1794. filteredChildren = items.length;
  1795. items.length = 0;
  1796. continue;
  1797. }
  1798. for (let i = 0; i < groups.length; i++) {
  1799. if (groupSizes[i] === maxGroupSize) {
  1800. const group = groups[i];
  1801. // run this algorithm recursively and limit the size of the children to
  1802. // current size - oversize / number of groups
  1803. // So it should always end up being smaller
  1804. const headerSize = group.filteredChildren ? 2 : 1;
  1805. const limited = spaceLimited(
  1806. /** @type {Children<T>} */ (group.children),
  1807. maxGroupSize -
  1808. // we should use ceil to always feet in max
  1809. Math.ceil(oversize / groups.length) -
  1810. // we substitute size of group head
  1811. headerSize,
  1812. headerSize === 2
  1813. );
  1814. groups[i] = {
  1815. ...group,
  1816. children: limited.children,
  1817. filteredChildren: limited.filteredChildren
  1818. ? (group.filteredChildren || 0) + limited.filteredChildren
  1819. : group.filteredChildren
  1820. };
  1821. const newSize = getItemSize(groups[i]);
  1822. groupsSize -= maxGroupSize - newSize;
  1823. groupSizes[i] = newSize;
  1824. break;
  1825. }
  1826. }
  1827. }
  1828. children = groups.concat(items);
  1829. } else if (limit === max) {
  1830. // If we have only enough space to show one line per group and one line for the filtered items
  1831. // collapse all groups and items
  1832. children = collapse(groups);
  1833. filteredChildren = items.length;
  1834. } else {
  1835. // If we have no space
  1836. // collapse complete group
  1837. filteredChildren = getTotalItems(itemsAndGroups);
  1838. }
  1839. }
  1840. return /** @type {Children<T>} */ ({ children, filteredChildren });
  1841. };
  1842. /**
  1843. * @param {StatsError[]} errors errors
  1844. * @param {number} max max
  1845. * @returns {[StatsError[], number]} error space limit
  1846. */
  1847. const errorsSpaceLimit = (errors, max) => {
  1848. let filtered = 0;
  1849. // Can not fit into limit
  1850. // print only messages
  1851. if (errors.length + 1 >= max)
  1852. return [
  1853. errors.map(error => {
  1854. if (typeof error === "string" || !error.details) return error;
  1855. filtered++;
  1856. return { ...error, details: "" };
  1857. }),
  1858. filtered
  1859. ];
  1860. let fullLength = errors.length;
  1861. let result = errors;
  1862. let i = 0;
  1863. for (; i < errors.length; i++) {
  1864. const error = errors[i];
  1865. if (typeof error !== "string" && error.details) {
  1866. const splitted = error.details.split("\n");
  1867. const len = splitted.length;
  1868. fullLength += len;
  1869. if (fullLength > max) {
  1870. result = i > 0 ? errors.slice(0, i) : [];
  1871. const overLimit = fullLength - max + 1;
  1872. const error = errors[i++];
  1873. result.push({
  1874. ...error,
  1875. details: error.details.split("\n").slice(0, -overLimit).join("\n"),
  1876. filteredDetails: overLimit
  1877. });
  1878. filtered = errors.length - i;
  1879. for (; i < errors.length; i++) {
  1880. const error = errors[i];
  1881. if (typeof error === "string" || !error.details) result.push(error);
  1882. result.push({ ...error, details: "" });
  1883. }
  1884. break;
  1885. } else if (fullLength === max) {
  1886. result = errors.slice(0, ++i);
  1887. filtered = errors.length - i;
  1888. for (; i < errors.length; i++) {
  1889. const error = errors[i];
  1890. if (typeof error === "string" || !error.details) result.push(error);
  1891. result.push({ ...error, details: "" });
  1892. }
  1893. break;
  1894. }
  1895. }
  1896. }
  1897. return [result, filtered];
  1898. };
  1899. /**
  1900. * @template {{ size: number }} T
  1901. * @template {{ size: number }} R
  1902. * @param {(R | T)[]} children children
  1903. * @param {T[]} assets assets
  1904. * @returns {{ size: number }} asset size
  1905. */
  1906. const assetGroup = (children, assets) => {
  1907. let size = 0;
  1908. for (const asset of children) {
  1909. size += asset.size;
  1910. }
  1911. return { size };
  1912. };
  1913. /**
  1914. * @template {{ size: number, sizes: Record<string, number> }} T
  1915. * @param {Children<T>[]} children children
  1916. * @param {KnownStatsModule[]} modules modules
  1917. * @returns {{ size: number, sizes: Record<string, number>}} size and sizes
  1918. */
  1919. const moduleGroup = (children, modules) => {
  1920. let size = 0;
  1921. /** @type {Record<string, number>} */
  1922. const sizes = {};
  1923. for (const module of children) {
  1924. size += module.size;
  1925. for (const key of Object.keys(module.sizes)) {
  1926. sizes[key] = (sizes[key] || 0) + module.sizes[key];
  1927. }
  1928. }
  1929. return {
  1930. size,
  1931. sizes
  1932. };
  1933. };
  1934. /**
  1935. * @template {{ active: boolean }} T
  1936. * @param {Children<T>[]} children children
  1937. * @param {KnownStatsModuleReason[]} reasons reasons
  1938. * @returns {{ active: boolean }} reason group
  1939. */
  1940. const reasonGroup = (children, reasons) => {
  1941. let active = false;
  1942. for (const reason of children) {
  1943. active = active || reason.active;
  1944. }
  1945. return {
  1946. active
  1947. };
  1948. };
  1949. const GROUP_EXTENSION_REGEXP = /(\.[^.]+?)(?:\?|(?: \+ \d+ modules?)?$)/;
  1950. const GROUP_PATH_REGEXP = /(.+)[/\\][^/\\]+?(?:\?|(?: \+ \d+ modules?)?$)/;
  1951. /** @typedef {Record<string, (groupConfigs: GroupConfig<KnownStatsAsset, TODO>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} AssetsGroupers */
  1952. /** @type {AssetsGroupers} */
  1953. const ASSETS_GROUPERS = {
  1954. _: (groupConfigs, context, options) => {
  1955. /**
  1956. * @param {keyof KnownStatsAsset} name name
  1957. * @param {boolean=} exclude need exclude?
  1958. */
  1959. const groupByFlag = (name, exclude) => {
  1960. groupConfigs.push({
  1961. getKeys: asset => (asset[name] ? ["1"] : undefined),
  1962. getOptions: () => ({
  1963. groupChildren: !exclude,
  1964. force: exclude
  1965. }),
  1966. createGroup: (key, children, assets) =>
  1967. exclude
  1968. ? {
  1969. type: "assets by status",
  1970. [name]: Boolean(key),
  1971. filteredChildren: assets.length,
  1972. ...assetGroup(children, assets)
  1973. }
  1974. : {
  1975. type: "assets by status",
  1976. [name]: Boolean(key),
  1977. children,
  1978. ...assetGroup(children, assets)
  1979. }
  1980. });
  1981. };
  1982. const {
  1983. groupAssetsByEmitStatus,
  1984. groupAssetsByPath,
  1985. groupAssetsByExtension
  1986. } = options;
  1987. if (groupAssetsByEmitStatus) {
  1988. groupByFlag("emitted");
  1989. groupByFlag("comparedForEmit");
  1990. groupByFlag("isOverSizeLimit");
  1991. }
  1992. if (groupAssetsByEmitStatus || !options.cachedAssets) {
  1993. groupByFlag("cached", !options.cachedAssets);
  1994. }
  1995. if (groupAssetsByPath || groupAssetsByExtension) {
  1996. groupConfigs.push({
  1997. getKeys: asset => {
  1998. const extensionMatch =
  1999. groupAssetsByExtension && GROUP_EXTENSION_REGEXP.exec(asset.name);
  2000. const extension = extensionMatch ? extensionMatch[1] : "";
  2001. const pathMatch =
  2002. groupAssetsByPath && GROUP_PATH_REGEXP.exec(asset.name);
  2003. const path = pathMatch ? pathMatch[1].split(/[/\\]/) : [];
  2004. const keys = [];
  2005. if (groupAssetsByPath) {
  2006. keys.push(".");
  2007. if (extension)
  2008. keys.push(
  2009. path.length
  2010. ? `${path.join("/")}/*${extension}`
  2011. : `*${extension}`
  2012. );
  2013. while (path.length > 0) {
  2014. keys.push(`${path.join("/")}/`);
  2015. path.pop();
  2016. }
  2017. } else if (extension) {
  2018. keys.push(`*${extension}`);
  2019. }
  2020. return keys;
  2021. },
  2022. createGroup: (key, children, assets) => ({
  2023. type: groupAssetsByPath ? "assets by path" : "assets by extension",
  2024. name: key,
  2025. children,
  2026. ...assetGroup(children, assets)
  2027. })
  2028. });
  2029. }
  2030. },
  2031. groupAssetsByInfo: (groupConfigs, context, options) => {
  2032. /**
  2033. * @param {string} name name
  2034. */
  2035. const groupByAssetInfoFlag = name => {
  2036. groupConfigs.push({
  2037. getKeys: asset => (asset.info && asset.info[name] ? ["1"] : undefined),
  2038. createGroup: (key, children, assets) => ({
  2039. type: "assets by info",
  2040. info: {
  2041. [name]: Boolean(key)
  2042. },
  2043. children,
  2044. ...assetGroup(children, assets)
  2045. })
  2046. });
  2047. };
  2048. groupByAssetInfoFlag("immutable");
  2049. groupByAssetInfoFlag("development");
  2050. groupByAssetInfoFlag("hotModuleReplacement");
  2051. },
  2052. groupAssetsByChunk: (groupConfigs, context, options) => {
  2053. /**
  2054. * @param {keyof KnownStatsAsset} name name
  2055. */
  2056. const groupByNames = name => {
  2057. groupConfigs.push({
  2058. getKeys: asset => /** @type {string[]} */ (asset[name]),
  2059. createGroup: (key, children, assets) => ({
  2060. type: "assets by chunk",
  2061. [name]: [key],
  2062. children,
  2063. ...assetGroup(children, assets)
  2064. })
  2065. });
  2066. };
  2067. groupByNames("chunkNames");
  2068. groupByNames("auxiliaryChunkNames");
  2069. groupByNames("chunkIdHints");
  2070. groupByNames("auxiliaryChunkIdHints");
  2071. },
  2072. excludeAssets: (groupConfigs, context, { excludeAssets }) => {
  2073. groupConfigs.push({
  2074. getKeys: asset => {
  2075. const ident = asset.name;
  2076. const excluded = excludeAssets.some(fn => fn(ident, asset));
  2077. if (excluded) return ["excluded"];
  2078. },
  2079. getOptions: () => ({
  2080. groupChildren: false,
  2081. force: true
  2082. }),
  2083. createGroup: (key, children, assets) => ({
  2084. type: "hidden assets",
  2085. filteredChildren: assets.length,
  2086. ...assetGroup(children, assets)
  2087. })
  2088. });
  2089. }
  2090. };
  2091. /** @typedef {Record<string, (groupConfigs: GroupConfig<KnownStatsModule, TODO>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} ModulesGroupers */
  2092. /** @type {function("module" | "chunk" | "root-of-chunk" | "nested"): ModulesGroupers} */
  2093. const MODULES_GROUPERS = type => ({
  2094. _: (groupConfigs, context, options) => {
  2095. /**
  2096. * @param {keyof KnownStatsModule} name name
  2097. * @param {string} type type
  2098. * @param {boolean=} exclude need exclude?
  2099. */
  2100. const groupByFlag = (name, type, exclude) => {
  2101. groupConfigs.push({
  2102. getKeys: module => (module[name] ? ["1"] : undefined),
  2103. getOptions: () => ({
  2104. groupChildren: !exclude,
  2105. force: exclude
  2106. }),
  2107. createGroup: (key, children, modules) => ({
  2108. type,
  2109. [name]: Boolean(key),
  2110. ...(exclude ? { filteredChildren: modules.length } : { children }),
  2111. ...moduleGroup(children, modules)
  2112. })
  2113. });
  2114. };
  2115. const {
  2116. groupModulesByCacheStatus,
  2117. groupModulesByLayer,
  2118. groupModulesByAttributes,
  2119. groupModulesByType,
  2120. groupModulesByPath,
  2121. groupModulesByExtension
  2122. } = options;
  2123. if (groupModulesByAttributes) {
  2124. groupByFlag("errors", "modules with errors");
  2125. groupByFlag("warnings", "modules with warnings");
  2126. groupByFlag("assets", "modules with assets");
  2127. groupByFlag("optional", "optional modules");
  2128. }
  2129. if (groupModulesByCacheStatus) {
  2130. groupByFlag("cacheable", "cacheable modules");
  2131. groupByFlag("built", "built modules");
  2132. groupByFlag("codeGenerated", "code generated modules");
  2133. }
  2134. if (groupModulesByCacheStatus || !options.cachedModules) {
  2135. groupByFlag("cached", "cached modules", !options.cachedModules);
  2136. }
  2137. if (groupModulesByAttributes || !options.orphanModules) {
  2138. groupByFlag("orphan", "orphan modules", !options.orphanModules);
  2139. }
  2140. if (groupModulesByAttributes || !options.dependentModules) {
  2141. groupByFlag("dependent", "dependent modules", !options.dependentModules);
  2142. }
  2143. if (groupModulesByType || !options.runtimeModules) {
  2144. groupConfigs.push({
  2145. getKeys: module => {
  2146. if (!module.moduleType) return;
  2147. if (groupModulesByType) {
  2148. return [module.moduleType.split("/", 1)[0]];
  2149. } else if (module.moduleType === WEBPACK_MODULE_TYPE_RUNTIME) {
  2150. return [WEBPACK_MODULE_TYPE_RUNTIME];
  2151. }
  2152. },
  2153. getOptions: key => {
  2154. const exclude =
  2155. key === WEBPACK_MODULE_TYPE_RUNTIME && !options.runtimeModules;
  2156. return {
  2157. groupChildren: !exclude,
  2158. force: exclude
  2159. };
  2160. },
  2161. createGroup: (key, children, modules) => {
  2162. const exclude =
  2163. key === WEBPACK_MODULE_TYPE_RUNTIME && !options.runtimeModules;
  2164. return {
  2165. type: `${key} modules`,
  2166. moduleType: key,
  2167. ...(exclude ? { filteredChildren: modules.length } : { children }),
  2168. ...moduleGroup(children, modules)
  2169. };
  2170. }
  2171. });
  2172. }
  2173. if (groupModulesByLayer) {
  2174. groupConfigs.push({
  2175. getKeys: module => /** @type {string[]} */ ([module.layer]),
  2176. createGroup: (key, children, modules) => ({
  2177. type: "modules by layer",
  2178. layer: key,
  2179. children,
  2180. ...moduleGroup(children, modules)
  2181. })
  2182. });
  2183. }
  2184. if (groupModulesByPath || groupModulesByExtension) {
  2185. groupConfigs.push({
  2186. getKeys: module => {
  2187. if (!module.name) return;
  2188. const resource = parseResource(
  2189. /** @type {string} */ (module.name.split("!").pop())
  2190. ).path;
  2191. const dataUrl = /^data:[^,;]+/.exec(resource);
  2192. if (dataUrl) return [dataUrl[0]];
  2193. const extensionMatch =
  2194. groupModulesByExtension && GROUP_EXTENSION_REGEXP.exec(resource);
  2195. const extension = extensionMatch ? extensionMatch[1] : "";
  2196. const pathMatch =
  2197. groupModulesByPath && GROUP_PATH_REGEXP.exec(resource);
  2198. const path = pathMatch ? pathMatch[1].split(/[/\\]/) : [];
  2199. const keys = [];
  2200. if (groupModulesByPath) {
  2201. if (extension)
  2202. keys.push(
  2203. path.length
  2204. ? `${path.join("/")}/*${extension}`
  2205. : `*${extension}`
  2206. );
  2207. while (path.length > 0) {
  2208. keys.push(`${path.join("/")}/`);
  2209. path.pop();
  2210. }
  2211. } else if (extension) {
  2212. keys.push(`*${extension}`);
  2213. }
  2214. return keys;
  2215. },
  2216. createGroup: (key, children, modules) => {
  2217. const isDataUrl = key.startsWith("data:");
  2218. return {
  2219. type: isDataUrl
  2220. ? "modules by mime type"
  2221. : groupModulesByPath
  2222. ? "modules by path"
  2223. : "modules by extension",
  2224. name: isDataUrl ? key.slice(/* 'data:'.length */ 5) : key,
  2225. children,
  2226. ...moduleGroup(children, modules)
  2227. };
  2228. }
  2229. });
  2230. }
  2231. },
  2232. excludeModules: (groupConfigs, context, { excludeModules }) => {
  2233. groupConfigs.push({
  2234. getKeys: module => {
  2235. const name = module.name;
  2236. if (name) {
  2237. const excluded = excludeModules.some(fn => fn(name, module, type));
  2238. if (excluded) return ["1"];
  2239. }
  2240. },
  2241. getOptions: () => ({
  2242. groupChildren: false,
  2243. force: true
  2244. }),
  2245. createGroup: (key, children, modules) => ({
  2246. type: "hidden modules",
  2247. filteredChildren: children.length,
  2248. ...moduleGroup(children, modules)
  2249. })
  2250. });
  2251. }
  2252. });
  2253. /** @typedef {Record<string, (groupConfigs: import("../util/smartGrouping").GroupConfig<KnownStatsModuleReason, TODO>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} ModuleReasonsGroupers */
  2254. /** @type {ModuleReasonsGroupers} */
  2255. const MODULE_REASONS_GROUPERS = {
  2256. groupReasonsByOrigin: groupConfigs => {
  2257. groupConfigs.push({
  2258. getKeys: reason => /** @type {string[]} */ ([reason.module]),
  2259. createGroup: (key, children, reasons) => ({
  2260. type: "from origin",
  2261. module: key,
  2262. children,
  2263. ...reasonGroup(children, reasons)
  2264. })
  2265. });
  2266. }
  2267. };
  2268. /** @type {Record<string, AssetsGroupers | ModulesGroupers | ModuleReasonsGroupers>} */
  2269. const RESULT_GROUPERS = {
  2270. "compilation.assets": ASSETS_GROUPERS,
  2271. "asset.related": ASSETS_GROUPERS,
  2272. "compilation.modules": MODULES_GROUPERS("module"),
  2273. "chunk.modules": MODULES_GROUPERS("chunk"),
  2274. "chunk.rootModules": MODULES_GROUPERS("root-of-chunk"),
  2275. "module.modules": MODULES_GROUPERS("nested"),
  2276. "module.reasons": MODULE_REASONS_GROUPERS
  2277. };
  2278. // remove a prefixed "!" that can be specified to reverse sort order
  2279. /**
  2280. * @param {string} field a field name
  2281. * @returns {field} normalized field
  2282. */
  2283. const normalizeFieldKey = field => {
  2284. if (field[0] === "!") {
  2285. return field.slice(1);
  2286. }
  2287. return field;
  2288. };
  2289. // if a field is prefixed by a "!" reverse sort order
  2290. /**
  2291. * @param {string} field a field name
  2292. * @returns {boolean} result
  2293. */
  2294. const sortOrderRegular = field => {
  2295. if (field[0] === "!") {
  2296. return false;
  2297. }
  2298. return true;
  2299. };
  2300. /**
  2301. * @template T
  2302. * @param {string} field field name
  2303. * @returns {function(T, T): 0 | 1 | -1} comparators
  2304. */
  2305. const sortByField = field => {
  2306. if (!field) {
  2307. /**
  2308. * @param {any} a first
  2309. * @param {any} b second
  2310. * @returns {-1|0|1} zero
  2311. */
  2312. const noSort = (a, b) => 0;
  2313. return noSort;
  2314. }
  2315. const fieldKey = normalizeFieldKey(field);
  2316. let sortFn = compareSelect(m => m[fieldKey], compareIds);
  2317. // if a field is prefixed with a "!" the sort is reversed!
  2318. const sortIsRegular = sortOrderRegular(field);
  2319. if (!sortIsRegular) {
  2320. const oldSortFn = sortFn;
  2321. sortFn = (a, b) => oldSortFn(b, a);
  2322. }
  2323. return sortFn;
  2324. };
  2325. /** @type {Record<string, (comparators: Comparator<TODO>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} */
  2326. const ASSET_SORTERS = {
  2327. assetsSort: (comparators, context, { assetsSort }) => {
  2328. comparators.push(sortByField(assetsSort));
  2329. },
  2330. _: comparators => {
  2331. comparators.push(compareSelect(a => a.name, compareIds));
  2332. }
  2333. };
  2334. /** @type {Record<string, Record<string, (comparators: Comparator<TODO>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>>} */
  2335. const RESULT_SORTERS = {
  2336. "compilation.chunks": {
  2337. chunksSort: (comparators, context, { chunksSort }) => {
  2338. comparators.push(sortByField(chunksSort));
  2339. }
  2340. },
  2341. "compilation.modules": {
  2342. modulesSort: (comparators, context, { modulesSort }) => {
  2343. comparators.push(sortByField(modulesSort));
  2344. }
  2345. },
  2346. "chunk.modules": {
  2347. chunkModulesSort: (comparators, context, { chunkModulesSort }) => {
  2348. comparators.push(sortByField(chunkModulesSort));
  2349. }
  2350. },
  2351. "module.modules": {
  2352. nestedModulesSort: (comparators, context, { nestedModulesSort }) => {
  2353. comparators.push(sortByField(nestedModulesSort));
  2354. }
  2355. },
  2356. "compilation.assets": ASSET_SORTERS,
  2357. "asset.related": ASSET_SORTERS
  2358. };
  2359. /**
  2360. * @param {Record<string, Record<string, Function>>} config the config see above
  2361. * @param {NormalizedStatsOptions} options stats options
  2362. * @param {function(string, Function): void} fn handler function called for every active line in config
  2363. * @returns {void}
  2364. */
  2365. const iterateConfig = (config, options, fn) => {
  2366. for (const hookFor of Object.keys(config)) {
  2367. const subConfig = config[hookFor];
  2368. for (const option of Object.keys(subConfig)) {
  2369. if (option !== "_") {
  2370. if (option.startsWith("!")) {
  2371. if (options[option.slice(1)]) continue;
  2372. } else {
  2373. const value = options[option];
  2374. if (
  2375. value === false ||
  2376. value === undefined ||
  2377. (Array.isArray(value) && value.length === 0)
  2378. )
  2379. continue;
  2380. }
  2381. }
  2382. fn(hookFor, subConfig[option]);
  2383. }
  2384. }
  2385. };
  2386. /** @type {Record<string, string>} */
  2387. const ITEM_NAMES = {
  2388. "compilation.children[]": "compilation",
  2389. "compilation.modules[]": "module",
  2390. "compilation.entrypoints[]": "chunkGroup",
  2391. "compilation.namedChunkGroups[]": "chunkGroup",
  2392. "compilation.errors[]": "error",
  2393. "compilation.warnings[]": "warning",
  2394. "chunk.modules[]": "module",
  2395. "chunk.rootModules[]": "module",
  2396. "chunk.origins[]": "chunkOrigin",
  2397. "compilation.chunks[]": "chunk",
  2398. "compilation.assets[]": "asset",
  2399. "asset.related[]": "asset",
  2400. "module.issuerPath[]": "moduleIssuer",
  2401. "module.reasons[]": "moduleReason",
  2402. "module.modules[]": "module",
  2403. "module.children[]": "module",
  2404. "moduleTrace[]": "moduleTraceItem",
  2405. "moduleTraceItem.dependencies[]": "moduleTraceDependency"
  2406. };
  2407. /**
  2408. * @template T
  2409. * @typedef {{ name: T }} NamedObject
  2410. */
  2411. /**
  2412. * @template {{ name: string }} T
  2413. * @param {T[]} items items to be merged
  2414. * @returns {NamedObject<T>} an object
  2415. */
  2416. const mergeToObject = items => {
  2417. const obj = Object.create(null);
  2418. for (const item of items) {
  2419. obj[item.name] = item;
  2420. }
  2421. return obj;
  2422. };
  2423. /**
  2424. * @template {{ name: string }} T
  2425. * @type {Record<string, (items: T[]) => NamedObject<T>>}
  2426. */
  2427. const MERGER = {
  2428. "compilation.entrypoints": mergeToObject,
  2429. "compilation.namedChunkGroups": mergeToObject
  2430. };
  2431. class DefaultStatsFactoryPlugin {
  2432. /**
  2433. * Apply the plugin
  2434. * @param {Compiler} compiler the compiler instance
  2435. * @returns {void}
  2436. */
  2437. apply(compiler) {
  2438. compiler.hooks.compilation.tap("DefaultStatsFactoryPlugin", compilation => {
  2439. compilation.hooks.statsFactory.tap(
  2440. "DefaultStatsFactoryPlugin",
  2441. /**
  2442. * @param {StatsFactory} stats stats factory
  2443. * @param {NormalizedStatsOptions} options stats options
  2444. */
  2445. (stats, options) => {
  2446. iterateConfig(SIMPLE_EXTRACTORS, options, (hookFor, fn) => {
  2447. stats.hooks.extract
  2448. .for(hookFor)
  2449. .tap("DefaultStatsFactoryPlugin", (obj, data, ctx) =>
  2450. fn(obj, data, ctx, options, stats)
  2451. );
  2452. });
  2453. iterateConfig(FILTER, options, (hookFor, fn) => {
  2454. stats.hooks.filter
  2455. .for(hookFor)
  2456. .tap("DefaultStatsFactoryPlugin", (item, ctx, idx, i) =>
  2457. fn(item, ctx, options, idx, i)
  2458. );
  2459. });
  2460. iterateConfig(FILTER_RESULTS, options, (hookFor, fn) => {
  2461. stats.hooks.filterResults
  2462. .for(hookFor)
  2463. .tap("DefaultStatsFactoryPlugin", (item, ctx, idx, i) =>
  2464. fn(item, ctx, options, idx, i)
  2465. );
  2466. });
  2467. iterateConfig(SORTERS, options, (hookFor, fn) => {
  2468. stats.hooks.sort
  2469. .for(hookFor)
  2470. .tap("DefaultStatsFactoryPlugin", (comparators, ctx) =>
  2471. fn(comparators, ctx, options)
  2472. );
  2473. });
  2474. iterateConfig(RESULT_SORTERS, options, (hookFor, fn) => {
  2475. stats.hooks.sortResults
  2476. .for(hookFor)
  2477. .tap("DefaultStatsFactoryPlugin", (comparators, ctx) =>
  2478. fn(comparators, ctx, options)
  2479. );
  2480. });
  2481. iterateConfig(RESULT_GROUPERS, options, (hookFor, fn) => {
  2482. stats.hooks.groupResults
  2483. .for(hookFor)
  2484. .tap("DefaultStatsFactoryPlugin", (groupConfigs, ctx) =>
  2485. fn(groupConfigs, ctx, options)
  2486. );
  2487. });
  2488. for (const key of Object.keys(ITEM_NAMES)) {
  2489. const itemName = ITEM_NAMES[key];
  2490. stats.hooks.getItemName
  2491. .for(key)
  2492. .tap("DefaultStatsFactoryPlugin", () => itemName);
  2493. }
  2494. for (const key of Object.keys(MERGER)) {
  2495. const merger = MERGER[key];
  2496. stats.hooks.merge.for(key).tap("DefaultStatsFactoryPlugin", merger);
  2497. }
  2498. if (options.children) {
  2499. if (Array.isArray(options.children)) {
  2500. stats.hooks.getItemFactory
  2501. .for("compilation.children[].compilation")
  2502. .tap(
  2503. "DefaultStatsFactoryPlugin",
  2504. /**
  2505. * @param {Compilation} comp compilation
  2506. * @param {StatsFactoryContext} options options
  2507. * @returns {StatsFactory | undefined} stats factory
  2508. */
  2509. (comp, { _index: idx }) => {
  2510. const children =
  2511. /** @type {TODO} */
  2512. (options.children);
  2513. if (idx < children.length) {
  2514. return compilation.createStatsFactory(
  2515. compilation.createStatsOptions(children[idx])
  2516. );
  2517. }
  2518. }
  2519. );
  2520. } else if (options.children !== true) {
  2521. const childFactory = compilation.createStatsFactory(
  2522. compilation.createStatsOptions(options.children)
  2523. );
  2524. stats.hooks.getItemFactory
  2525. .for("compilation.children[].compilation")
  2526. .tap("DefaultStatsFactoryPlugin", () => childFactory);
  2527. }
  2528. }
  2529. }
  2530. );
  2531. });
  2532. }
  2533. }
  2534. module.exports = DefaultStatsFactoryPlugin;