DefaultStatsFactoryPlugin.js 84 KB

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