You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Stats.js 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequestShortener = require("./RequestShortener");
  7. const SizeFormatHelpers = require("./SizeFormatHelpers");
  8. const formatLocation = require("./formatLocation");
  9. const identifierUtils = require("./util/identifier");
  10. const compareLocations = require("./compareLocations");
  11. const { LogType } = require("./logging/Logger");
  12. const optionsOrFallback = (...args) => {
  13. let optionValues = [];
  14. optionValues.push(...args);
  15. return optionValues.find(optionValue => optionValue !== undefined);
  16. };
  17. const compareId = (a, b) => {
  18. if (typeof a !== typeof b) {
  19. return typeof a < typeof b ? -1 : 1;
  20. }
  21. if (a < b) return -1;
  22. if (a > b) return 1;
  23. return 0;
  24. };
  25. class Stats {
  26. constructor(compilation) {
  27. this.compilation = compilation;
  28. this.hash = compilation.hash;
  29. this.startTime = undefined;
  30. this.endTime = undefined;
  31. }
  32. static filterWarnings(warnings, warningsFilter) {
  33. // we dont have anything to filter so all warnings can be shown
  34. if (!warningsFilter) {
  35. return warnings;
  36. }
  37. // create a chain of filters
  38. // if they return "true" a warning should be suppressed
  39. const normalizedWarningsFilters = [].concat(warningsFilter).map(filter => {
  40. if (typeof filter === "string") {
  41. return warning => warning.includes(filter);
  42. }
  43. if (filter instanceof RegExp) {
  44. return warning => filter.test(warning);
  45. }
  46. if (typeof filter === "function") {
  47. return filter;
  48. }
  49. throw new Error(
  50. `Can only filter warnings with Strings or RegExps. (Given: ${filter})`
  51. );
  52. });
  53. return warnings.filter(warning => {
  54. return !normalizedWarningsFilters.some(check => check(warning));
  55. });
  56. }
  57. formatFilePath(filePath) {
  58. const OPTIONS_REGEXP = /^(\s|\S)*!/;
  59. return filePath.includes("!")
  60. ? `${filePath.replace(OPTIONS_REGEXP, "")} (${filePath})`
  61. : `${filePath}`;
  62. }
  63. hasWarnings() {
  64. return (
  65. this.compilation.warnings.length > 0 ||
  66. this.compilation.children.some(child => child.getStats().hasWarnings())
  67. );
  68. }
  69. hasErrors() {
  70. return (
  71. this.compilation.errors.length > 0 ||
  72. this.compilation.children.some(child => child.getStats().hasErrors())
  73. );
  74. }
  75. // remove a prefixed "!" that can be specified to reverse sort order
  76. normalizeFieldKey(field) {
  77. if (field[0] === "!") {
  78. return field.substr(1);
  79. }
  80. return field;
  81. }
  82. // if a field is prefixed by a "!" reverse sort order
  83. sortOrderRegular(field) {
  84. if (field[0] === "!") {
  85. return false;
  86. }
  87. return true;
  88. }
  89. toJson(options, forToString) {
  90. if (typeof options === "boolean" || typeof options === "string") {
  91. options = Stats.presetToOptions(options);
  92. } else if (!options) {
  93. options = {};
  94. }
  95. const optionOrLocalFallback = (v, def) =>
  96. v !== undefined ? v : options.all !== undefined ? options.all : def;
  97. const testAgainstGivenOption = item => {
  98. if (typeof item === "string") {
  99. const regExp = new RegExp(
  100. `[\\\\/]${item.replace(
  101. // eslint-disable-next-line no-useless-escape
  102. /[-[\]{}()*+?.\\^$|]/g,
  103. "\\$&"
  104. )}([\\\\/]|$|!|\\?)`
  105. );
  106. return ident => regExp.test(ident);
  107. }
  108. if (item && typeof item === "object" && typeof item.test === "function") {
  109. return ident => item.test(ident);
  110. }
  111. if (typeof item === "function") {
  112. return item;
  113. }
  114. if (typeof item === "boolean") {
  115. return () => item;
  116. }
  117. };
  118. const compilation = this.compilation;
  119. const context = optionsOrFallback(
  120. options.context,
  121. compilation.compiler.context
  122. );
  123. const requestShortener =
  124. compilation.compiler.context === context
  125. ? compilation.requestShortener
  126. : new RequestShortener(context);
  127. const showPerformance = optionOrLocalFallback(options.performance, true);
  128. const showHash = optionOrLocalFallback(options.hash, true);
  129. const showEnv = optionOrLocalFallback(options.env, false);
  130. const showVersion = optionOrLocalFallback(options.version, true);
  131. const showTimings = optionOrLocalFallback(options.timings, true);
  132. const showBuiltAt = optionOrLocalFallback(options.builtAt, true);
  133. const showAssets = optionOrLocalFallback(options.assets, true);
  134. const showEntrypoints = optionOrLocalFallback(options.entrypoints, true);
  135. const showChunkGroups = optionOrLocalFallback(
  136. options.chunkGroups,
  137. !forToString
  138. );
  139. const showChunks = optionOrLocalFallback(options.chunks, !forToString);
  140. const showChunkModules = optionOrLocalFallback(options.chunkModules, true);
  141. const showChunkOrigins = optionOrLocalFallback(
  142. options.chunkOrigins,
  143. !forToString
  144. );
  145. const showModules = optionOrLocalFallback(options.modules, true);
  146. const showNestedModules = optionOrLocalFallback(
  147. options.nestedModules,
  148. true
  149. );
  150. const showModuleAssets = optionOrLocalFallback(
  151. options.moduleAssets,
  152. !forToString
  153. );
  154. const showDepth = optionOrLocalFallback(options.depth, !forToString);
  155. const showCachedModules = optionOrLocalFallback(options.cached, true);
  156. const showCachedAssets = optionOrLocalFallback(options.cachedAssets, true);
  157. const showReasons = optionOrLocalFallback(options.reasons, !forToString);
  158. const showUsedExports = optionOrLocalFallback(
  159. options.usedExports,
  160. !forToString
  161. );
  162. const showProvidedExports = optionOrLocalFallback(
  163. options.providedExports,
  164. !forToString
  165. );
  166. const showOptimizationBailout = optionOrLocalFallback(
  167. options.optimizationBailout,
  168. !forToString
  169. );
  170. const showChildren = optionOrLocalFallback(options.children, true);
  171. const showSource = optionOrLocalFallback(options.source, !forToString);
  172. const showModuleTrace = optionOrLocalFallback(options.moduleTrace, true);
  173. const showErrors = optionOrLocalFallback(options.errors, true);
  174. const showErrorDetails = optionOrLocalFallback(
  175. options.errorDetails,
  176. !forToString
  177. );
  178. const showWarnings = optionOrLocalFallback(options.warnings, true);
  179. const warningsFilter = optionsOrFallback(options.warningsFilter, null);
  180. const showPublicPath = optionOrLocalFallback(
  181. options.publicPath,
  182. !forToString
  183. );
  184. const showLogging = optionOrLocalFallback(
  185. options.logging,
  186. forToString ? "info" : true
  187. );
  188. const showLoggingTrace = optionOrLocalFallback(
  189. options.loggingTrace,
  190. !forToString
  191. );
  192. const loggingDebug = []
  193. .concat(optionsOrFallback(options.loggingDebug, []))
  194. .map(testAgainstGivenOption);
  195. const excludeModules = []
  196. .concat(optionsOrFallback(options.excludeModules, options.exclude, []))
  197. .map(testAgainstGivenOption);
  198. const excludeAssets = []
  199. .concat(optionsOrFallback(options.excludeAssets, []))
  200. .map(testAgainstGivenOption);
  201. const maxModules = optionsOrFallback(
  202. options.maxModules,
  203. forToString ? 15 : Infinity
  204. );
  205. const sortModules = optionsOrFallback(options.modulesSort, "id");
  206. const sortChunks = optionsOrFallback(options.chunksSort, "id");
  207. const sortAssets = optionsOrFallback(options.assetsSort, "");
  208. const showOutputPath = optionOrLocalFallback(
  209. options.outputPath,
  210. !forToString
  211. );
  212. if (!showCachedModules) {
  213. excludeModules.push((ident, module) => !module.built);
  214. }
  215. const createModuleFilter = () => {
  216. let i = 0;
  217. return module => {
  218. if (excludeModules.length > 0) {
  219. const ident = requestShortener.shorten(module.resource);
  220. const excluded = excludeModules.some(fn => fn(ident, module));
  221. if (excluded) return false;
  222. }
  223. const result = i < maxModules;
  224. i++;
  225. return result;
  226. };
  227. };
  228. const createAssetFilter = () => {
  229. return asset => {
  230. if (excludeAssets.length > 0) {
  231. const ident = asset.name;
  232. const excluded = excludeAssets.some(fn => fn(ident, asset));
  233. if (excluded) return false;
  234. }
  235. return showCachedAssets || asset.emitted;
  236. };
  237. };
  238. const sortByFieldAndOrder = (fieldKey, a, b) => {
  239. if (a[fieldKey] === null && b[fieldKey] === null) return 0;
  240. if (a[fieldKey] === null) return 1;
  241. if (b[fieldKey] === null) return -1;
  242. if (a[fieldKey] === b[fieldKey]) return 0;
  243. if (typeof a[fieldKey] !== typeof b[fieldKey])
  244. return typeof a[fieldKey] < typeof b[fieldKey] ? -1 : 1;
  245. return a[fieldKey] < b[fieldKey] ? -1 : 1;
  246. };
  247. const sortByField = (field, originalArray) => {
  248. const originalMap = originalArray.reduce((map, v, i) => {
  249. map.set(v, i);
  250. return map;
  251. }, new Map());
  252. return (a, b) => {
  253. if (field) {
  254. const fieldKey = this.normalizeFieldKey(field);
  255. // if a field is prefixed with a "!" the sort is reversed!
  256. const sortIsRegular = this.sortOrderRegular(field);
  257. const cmp = sortByFieldAndOrder(
  258. fieldKey,
  259. sortIsRegular ? a : b,
  260. sortIsRegular ? b : a
  261. );
  262. if (cmp) return cmp;
  263. }
  264. return originalMap.get(a) - originalMap.get(b);
  265. };
  266. };
  267. const formatError = e => {
  268. let text = "";
  269. if (typeof e === "string") {
  270. e = { message: e };
  271. }
  272. if (e.chunk) {
  273. text += `chunk ${e.chunk.name || e.chunk.id}${
  274. e.chunk.hasRuntime()
  275. ? " [entry]"
  276. : e.chunk.canBeInitial()
  277. ? " [initial]"
  278. : ""
  279. }\n`;
  280. }
  281. if (e.file) {
  282. text += `${e.file}\n`;
  283. }
  284. if (
  285. e.module &&
  286. e.module.readableIdentifier &&
  287. typeof e.module.readableIdentifier === "function"
  288. ) {
  289. text += this.formatFilePath(
  290. e.module.readableIdentifier(requestShortener)
  291. );
  292. if (typeof e.loc === "object") {
  293. const locInfo = formatLocation(e.loc);
  294. if (locInfo) text += ` ${locInfo}`;
  295. }
  296. text += "\n";
  297. }
  298. text += e.message;
  299. if (showErrorDetails && e.details) {
  300. text += `\n${e.details}`;
  301. }
  302. if (showErrorDetails && e.missing) {
  303. text += e.missing.map(item => `\n[${item}]`).join("");
  304. }
  305. if (showModuleTrace && e.origin) {
  306. text += `\n @ ${this.formatFilePath(
  307. e.origin.readableIdentifier(requestShortener)
  308. )}`;
  309. if (typeof e.originLoc === "object") {
  310. const locInfo = formatLocation(e.originLoc);
  311. if (locInfo) text += ` ${locInfo}`;
  312. }
  313. if (e.dependencies) {
  314. for (const dep of e.dependencies) {
  315. if (!dep.loc) continue;
  316. if (typeof dep.loc === "string") continue;
  317. const locInfo = formatLocation(dep.loc);
  318. if (!locInfo) continue;
  319. text += ` ${locInfo}`;
  320. }
  321. }
  322. let current = e.origin;
  323. while (current.issuer) {
  324. current = current.issuer;
  325. text += `\n @ ${current.readableIdentifier(requestShortener)}`;
  326. }
  327. }
  328. return text;
  329. };
  330. const obj = {
  331. errors: compilation.errors.map(formatError),
  332. warnings: Stats.filterWarnings(
  333. compilation.warnings.map(formatError),
  334. warningsFilter
  335. )
  336. };
  337. //We just hint other renderers since actually omitting
  338. //errors/warnings from the JSON would be kind of weird.
  339. Object.defineProperty(obj, "_showWarnings", {
  340. value: showWarnings,
  341. enumerable: false
  342. });
  343. Object.defineProperty(obj, "_showErrors", {
  344. value: showErrors,
  345. enumerable: false
  346. });
  347. if (showVersion) {
  348. obj.version = require("../package.json").version;
  349. }
  350. if (showHash) obj.hash = this.hash;
  351. if (showTimings && this.startTime && this.endTime) {
  352. obj.time = this.endTime - this.startTime;
  353. }
  354. if (showBuiltAt && this.endTime) {
  355. obj.builtAt = this.endTime;
  356. }
  357. if (showEnv && options._env) {
  358. obj.env = options._env;
  359. }
  360. if (compilation.needAdditionalPass) {
  361. obj.needAdditionalPass = true;
  362. }
  363. if (showPublicPath) {
  364. obj.publicPath = this.compilation.mainTemplate.getPublicPath({
  365. hash: this.compilation.hash
  366. });
  367. }
  368. if (showOutputPath) {
  369. obj.outputPath = this.compilation.mainTemplate.outputOptions.path;
  370. }
  371. if (showAssets) {
  372. const assetsByFile = {};
  373. const compilationAssets = compilation
  374. .getAssets()
  375. .sort((a, b) => (a.name < b.name ? -1 : 1));
  376. obj.assetsByChunkName = {};
  377. obj.assets = compilationAssets
  378. .map(({ name, source, info }) => {
  379. const obj = {
  380. name,
  381. size: source.size(),
  382. chunks: [],
  383. chunkNames: [],
  384. info,
  385. // TODO webpack 5: remove .emitted
  386. emitted: source.emitted || compilation.emittedAssets.has(name)
  387. };
  388. if (showPerformance) {
  389. obj.isOverSizeLimit = source.isOverSizeLimit;
  390. }
  391. assetsByFile[name] = obj;
  392. return obj;
  393. })
  394. .filter(createAssetFilter());
  395. obj.filteredAssets = compilationAssets.length - obj.assets.length;
  396. for (const chunk of compilation.chunks) {
  397. for (const asset of chunk.files) {
  398. if (assetsByFile[asset]) {
  399. for (const id of chunk.ids) {
  400. assetsByFile[asset].chunks.push(id);
  401. }
  402. if (chunk.name) {
  403. assetsByFile[asset].chunkNames.push(chunk.name);
  404. if (obj.assetsByChunkName[chunk.name]) {
  405. obj.assetsByChunkName[chunk.name] = []
  406. .concat(obj.assetsByChunkName[chunk.name])
  407. .concat([asset]);
  408. } else {
  409. obj.assetsByChunkName[chunk.name] = asset;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. obj.assets.sort(sortByField(sortAssets, obj.assets));
  416. }
  417. const fnChunkGroup = groupMap => {
  418. const obj = {};
  419. for (const keyValuePair of groupMap) {
  420. const name = keyValuePair[0];
  421. const cg = keyValuePair[1];
  422. const children = cg.getChildrenByOrders();
  423. obj[name] = {
  424. chunks: cg.chunks.map(c => c.id),
  425. assets: cg.chunks.reduce(
  426. (array, c) => array.concat(c.files || []),
  427. []
  428. ),
  429. children: Object.keys(children).reduce((obj, key) => {
  430. const groups = children[key];
  431. obj[key] = groups.map(group => ({
  432. name: group.name,
  433. chunks: group.chunks.map(c => c.id),
  434. assets: group.chunks.reduce(
  435. (array, c) => array.concat(c.files || []),
  436. []
  437. )
  438. }));
  439. return obj;
  440. }, Object.create(null)),
  441. childAssets: Object.keys(children).reduce((obj, key) => {
  442. const groups = children[key];
  443. obj[key] = Array.from(
  444. groups.reduce((set, group) => {
  445. for (const chunk of group.chunks) {
  446. for (const asset of chunk.files) {
  447. set.add(asset);
  448. }
  449. }
  450. return set;
  451. }, new Set())
  452. );
  453. return obj;
  454. }, Object.create(null))
  455. };
  456. if (showPerformance) {
  457. obj[name].isOverSizeLimit = cg.isOverSizeLimit;
  458. }
  459. }
  460. return obj;
  461. };
  462. if (showEntrypoints) {
  463. obj.entrypoints = fnChunkGroup(compilation.entrypoints);
  464. }
  465. if (showChunkGroups) {
  466. obj.namedChunkGroups = fnChunkGroup(compilation.namedChunkGroups);
  467. }
  468. const fnModule = module => {
  469. const path = [];
  470. let current = module;
  471. while (current.issuer) {
  472. path.push((current = current.issuer));
  473. }
  474. path.reverse();
  475. const obj = {
  476. id: module.id,
  477. identifier: module.identifier(),
  478. name: module.readableIdentifier(requestShortener),
  479. index: module.index,
  480. index2: module.index2,
  481. size: module.size(),
  482. cacheable: module.buildInfo.cacheable,
  483. built: !!module.built,
  484. optional: module.optional,
  485. prefetched: module.prefetched,
  486. chunks: Array.from(module.chunksIterable, chunk => chunk.id),
  487. issuer: module.issuer && module.issuer.identifier(),
  488. issuerId: module.issuer && module.issuer.id,
  489. issuerName:
  490. module.issuer && module.issuer.readableIdentifier(requestShortener),
  491. issuerPath:
  492. module.issuer &&
  493. path.map(module => ({
  494. id: module.id,
  495. identifier: module.identifier(),
  496. name: module.readableIdentifier(requestShortener),
  497. profile: module.profile
  498. })),
  499. profile: module.profile,
  500. failed: !!module.error,
  501. errors: module.errors ? module.errors.length : 0,
  502. warnings: module.warnings ? module.warnings.length : 0
  503. };
  504. if (showModuleAssets) {
  505. obj.assets = Object.keys(module.buildInfo.assets || {});
  506. }
  507. if (showReasons) {
  508. obj.reasons = module.reasons
  509. .sort((a, b) => {
  510. if (a.module && !b.module) return -1;
  511. if (!a.module && b.module) return 1;
  512. if (a.module && b.module) {
  513. const cmp = compareId(a.module.id, b.module.id);
  514. if (cmp) return cmp;
  515. }
  516. if (a.dependency && !b.dependency) return -1;
  517. if (!a.dependency && b.dependency) return 1;
  518. if (a.dependency && b.dependency) {
  519. const cmp = compareLocations(a.dependency.loc, b.dependency.loc);
  520. if (cmp) return cmp;
  521. if (a.dependency.type < b.dependency.type) return -1;
  522. if (a.dependency.type > b.dependency.type) return 1;
  523. }
  524. return 0;
  525. })
  526. .map(reason => {
  527. const obj = {
  528. moduleId: reason.module ? reason.module.id : null,
  529. moduleIdentifier: reason.module
  530. ? reason.module.identifier()
  531. : null,
  532. module: reason.module
  533. ? reason.module.readableIdentifier(requestShortener)
  534. : null,
  535. moduleName: reason.module
  536. ? reason.module.readableIdentifier(requestShortener)
  537. : null,
  538. type: reason.dependency ? reason.dependency.type : null,
  539. explanation: reason.explanation,
  540. userRequest: reason.dependency
  541. ? reason.dependency.userRequest
  542. : null
  543. };
  544. if (reason.dependency) {
  545. const locInfo = formatLocation(reason.dependency.loc);
  546. if (locInfo) {
  547. obj.loc = locInfo;
  548. }
  549. }
  550. return obj;
  551. });
  552. }
  553. if (showUsedExports) {
  554. if (module.used === true) {
  555. obj.usedExports = module.usedExports;
  556. } else if (module.used === false) {
  557. obj.usedExports = false;
  558. }
  559. }
  560. if (showProvidedExports) {
  561. obj.providedExports = Array.isArray(module.buildMeta.providedExports)
  562. ? module.buildMeta.providedExports
  563. : null;
  564. }
  565. if (showOptimizationBailout) {
  566. obj.optimizationBailout = module.optimizationBailout.map(item => {
  567. if (typeof item === "function") return item(requestShortener);
  568. return item;
  569. });
  570. }
  571. if (showDepth) {
  572. obj.depth = module.depth;
  573. }
  574. if (showNestedModules) {
  575. if (module.modules) {
  576. const modules = module.modules;
  577. obj.modules = modules
  578. .sort(sortByField("depth", modules))
  579. .filter(createModuleFilter())
  580. .map(fnModule);
  581. obj.filteredModules = modules.length - obj.modules.length;
  582. obj.modules.sort(sortByField(sortModules, obj.modules));
  583. }
  584. }
  585. if (showSource && module._source) {
  586. obj.source = module._source.source();
  587. }
  588. return obj;
  589. };
  590. if (showChunks) {
  591. obj.chunks = compilation.chunks.map(chunk => {
  592. const parents = new Set();
  593. const children = new Set();
  594. const siblings = new Set();
  595. const childIdByOrder = chunk.getChildIdsByOrders();
  596. for (const chunkGroup of chunk.groupsIterable) {
  597. for (const parentGroup of chunkGroup.parentsIterable) {
  598. for (const chunk of parentGroup.chunks) {
  599. parents.add(chunk.id);
  600. }
  601. }
  602. for (const childGroup of chunkGroup.childrenIterable) {
  603. for (const chunk of childGroup.chunks) {
  604. children.add(chunk.id);
  605. }
  606. }
  607. for (const sibling of chunkGroup.chunks) {
  608. if (sibling !== chunk) siblings.add(sibling.id);
  609. }
  610. }
  611. const obj = {
  612. id: chunk.id,
  613. rendered: chunk.rendered,
  614. initial: chunk.canBeInitial(),
  615. entry: chunk.hasRuntime(),
  616. recorded: chunk.recorded,
  617. reason: chunk.chunkReason,
  618. size: chunk.modulesSize(),
  619. names: chunk.name ? [chunk.name] : [],
  620. files: chunk.files.slice(),
  621. hash: chunk.renderedHash,
  622. siblings: Array.from(siblings).sort(compareId),
  623. parents: Array.from(parents).sort(compareId),
  624. children: Array.from(children).sort(compareId),
  625. childrenByOrder: childIdByOrder
  626. };
  627. if (showChunkModules) {
  628. const modules = chunk.getModules();
  629. obj.modules = modules
  630. .slice()
  631. .sort(sortByField("depth", modules))
  632. .filter(createModuleFilter())
  633. .map(fnModule);
  634. obj.filteredModules = chunk.getNumberOfModules() - obj.modules.length;
  635. obj.modules.sort(sortByField(sortModules, obj.modules));
  636. }
  637. if (showChunkOrigins) {
  638. obj.origins = Array.from(chunk.groupsIterable, g => g.origins)
  639. .reduce((a, b) => a.concat(b), [])
  640. .map(origin => ({
  641. moduleId: origin.module ? origin.module.id : undefined,
  642. module: origin.module ? origin.module.identifier() : "",
  643. moduleIdentifier: origin.module ? origin.module.identifier() : "",
  644. moduleName: origin.module
  645. ? origin.module.readableIdentifier(requestShortener)
  646. : "",
  647. loc: formatLocation(origin.loc),
  648. request: origin.request,
  649. reasons: origin.reasons || []
  650. }))
  651. .sort((a, b) => {
  652. const cmp1 = compareId(a.moduleId, b.moduleId);
  653. if (cmp1) return cmp1;
  654. const cmp2 = compareId(a.loc, b.loc);
  655. if (cmp2) return cmp2;
  656. const cmp3 = compareId(a.request, b.request);
  657. if (cmp3) return cmp3;
  658. return 0;
  659. });
  660. }
  661. return obj;
  662. });
  663. obj.chunks.sort(sortByField(sortChunks, obj.chunks));
  664. }
  665. if (showModules) {
  666. obj.modules = compilation.modules
  667. .slice()
  668. .sort(sortByField("depth", compilation.modules))
  669. .filter(createModuleFilter())
  670. .map(fnModule);
  671. obj.filteredModules = compilation.modules.length - obj.modules.length;
  672. obj.modules.sort(sortByField(sortModules, obj.modules));
  673. }
  674. if (showLogging) {
  675. const util = require("util");
  676. obj.logging = {};
  677. let acceptedTypes;
  678. let collapsedGroups = false;
  679. switch (showLogging) {
  680. case "none":
  681. acceptedTypes = new Set([]);
  682. break;
  683. case "error":
  684. acceptedTypes = new Set([LogType.error]);
  685. break;
  686. case "warn":
  687. acceptedTypes = new Set([LogType.error, LogType.warn]);
  688. break;
  689. case "info":
  690. acceptedTypes = new Set([LogType.error, LogType.warn, LogType.info]);
  691. break;
  692. case true:
  693. case "log":
  694. acceptedTypes = new Set([
  695. LogType.error,
  696. LogType.warn,
  697. LogType.info,
  698. LogType.log,
  699. LogType.group,
  700. LogType.groupEnd,
  701. LogType.groupCollapsed,
  702. LogType.clear
  703. ]);
  704. break;
  705. case "verbose":
  706. acceptedTypes = new Set([
  707. LogType.error,
  708. LogType.warn,
  709. LogType.info,
  710. LogType.log,
  711. LogType.group,
  712. LogType.groupEnd,
  713. LogType.groupCollapsed,
  714. LogType.profile,
  715. LogType.profileEnd,
  716. LogType.time,
  717. LogType.status,
  718. LogType.clear
  719. ]);
  720. collapsedGroups = true;
  721. break;
  722. }
  723. for (const [origin, logEntries] of compilation.logging) {
  724. const debugMode = loggingDebug.some(fn => fn(origin));
  725. let collapseCounter = 0;
  726. let processedLogEntries = logEntries;
  727. if (!debugMode) {
  728. processedLogEntries = processedLogEntries.filter(entry => {
  729. if (!acceptedTypes.has(entry.type)) return false;
  730. if (!collapsedGroups) {
  731. switch (entry.type) {
  732. case LogType.groupCollapsed:
  733. collapseCounter++;
  734. return collapseCounter === 1;
  735. case LogType.group:
  736. if (collapseCounter > 0) collapseCounter++;
  737. return collapseCounter === 0;
  738. case LogType.groupEnd:
  739. if (collapseCounter > 0) {
  740. collapseCounter--;
  741. return false;
  742. }
  743. return true;
  744. default:
  745. return collapseCounter === 0;
  746. }
  747. }
  748. return true;
  749. });
  750. }
  751. processedLogEntries = processedLogEntries.map(entry => {
  752. let message = undefined;
  753. if (entry.type === LogType.time) {
  754. message = `${entry.args[0]}: ${entry.args[1] * 1000 +
  755. entry.args[2] / 1000000}ms`;
  756. } else if (entry.args && entry.args.length > 0) {
  757. message = util.format(entry.args[0], ...entry.args.slice(1));
  758. }
  759. return {
  760. type:
  761. (debugMode || collapsedGroups) &&
  762. entry.type === LogType.groupCollapsed
  763. ? LogType.group
  764. : entry.type,
  765. message,
  766. trace: showLoggingTrace && entry.trace ? entry.trace : undefined
  767. };
  768. });
  769. let name = identifierUtils
  770. .makePathsRelative(context, origin, compilation.cache)
  771. .replace(/\|/g, " ");
  772. if (name in obj.logging) {
  773. let i = 1;
  774. while (`${name}#${i}` in obj.logging) {
  775. i++;
  776. }
  777. name = `${name}#${i}`;
  778. }
  779. obj.logging[name] = {
  780. entries: processedLogEntries,
  781. filteredEntries: logEntries.length - processedLogEntries.length,
  782. debug: debugMode
  783. };
  784. }
  785. }
  786. if (showChildren) {
  787. obj.children = compilation.children.map((child, idx) => {
  788. const childOptions = Stats.getChildOptions(options, idx);
  789. const obj = new Stats(child).toJson(childOptions, forToString);
  790. delete obj.hash;
  791. delete obj.version;
  792. if (child.name) {
  793. obj.name = identifierUtils.makePathsRelative(
  794. context,
  795. child.name,
  796. compilation.cache
  797. );
  798. }
  799. return obj;
  800. });
  801. }
  802. return obj;
  803. }
  804. toString(options) {
  805. if (typeof options === "boolean" || typeof options === "string") {
  806. options = Stats.presetToOptions(options);
  807. } else if (!options) {
  808. options = {};
  809. }
  810. const useColors = optionsOrFallback(options.colors, false);
  811. const obj = this.toJson(options, true);
  812. return Stats.jsonToString(obj, useColors);
  813. }
  814. static jsonToString(obj, useColors) {
  815. const buf = [];
  816. const defaultColors = {
  817. bold: "\u001b[1m",
  818. yellow: "\u001b[1m\u001b[33m",
  819. red: "\u001b[1m\u001b[31m",
  820. green: "\u001b[1m\u001b[32m",
  821. cyan: "\u001b[1m\u001b[36m",
  822. magenta: "\u001b[1m\u001b[35m"
  823. };
  824. const colors = Object.keys(defaultColors).reduce(
  825. (obj, color) => {
  826. obj[color] = str => {
  827. if (useColors) {
  828. buf.push(
  829. useColors === true || useColors[color] === undefined
  830. ? defaultColors[color]
  831. : useColors[color]
  832. );
  833. }
  834. buf.push(str);
  835. if (useColors) {
  836. buf.push("\u001b[39m\u001b[22m");
  837. }
  838. };
  839. return obj;
  840. },
  841. {
  842. normal: str => buf.push(str)
  843. }
  844. );
  845. const coloredTime = time => {
  846. let times = [800, 400, 200, 100];
  847. if (obj.time) {
  848. times = [obj.time / 2, obj.time / 4, obj.time / 8, obj.time / 16];
  849. }
  850. if (time < times[3]) colors.normal(`${time}ms`);
  851. else if (time < times[2]) colors.bold(`${time}ms`);
  852. else if (time < times[1]) colors.green(`${time}ms`);
  853. else if (time < times[0]) colors.yellow(`${time}ms`);
  854. else colors.red(`${time}ms`);
  855. };
  856. const newline = () => buf.push("\n");
  857. const getText = (arr, row, col) => {
  858. return arr[row][col].value;
  859. };
  860. const table = (array, align, splitter) => {
  861. const rows = array.length;
  862. const cols = array[0].length;
  863. const colSizes = new Array(cols);
  864. for (let col = 0; col < cols; col++) {
  865. colSizes[col] = 0;
  866. }
  867. for (let row = 0; row < rows; row++) {
  868. for (let col = 0; col < cols; col++) {
  869. const value = `${getText(array, row, col)}`;
  870. if (value.length > colSizes[col]) {
  871. colSizes[col] = value.length;
  872. }
  873. }
  874. }
  875. for (let row = 0; row < rows; row++) {
  876. for (let col = 0; col < cols; col++) {
  877. const format = array[row][col].color;
  878. const value = `${getText(array, row, col)}`;
  879. let l = value.length;
  880. if (align[col] === "l") {
  881. format(value);
  882. }
  883. for (; l < colSizes[col] && col !== cols - 1; l++) {
  884. colors.normal(" ");
  885. }
  886. if (align[col] === "r") {
  887. format(value);
  888. }
  889. if (col + 1 < cols && colSizes[col] !== 0) {
  890. colors.normal(splitter || " ");
  891. }
  892. }
  893. newline();
  894. }
  895. };
  896. const getAssetColor = (asset, defaultColor) => {
  897. if (asset.isOverSizeLimit) {
  898. return colors.yellow;
  899. }
  900. return defaultColor;
  901. };
  902. if (obj.hash) {
  903. colors.normal("Hash: ");
  904. colors.bold(obj.hash);
  905. newline();
  906. }
  907. if (obj.version) {
  908. colors.normal("Version: webpack ");
  909. colors.bold(obj.version);
  910. newline();
  911. }
  912. if (typeof obj.time === "number") {
  913. colors.normal("Time: ");
  914. colors.bold(obj.time);
  915. colors.normal("ms");
  916. newline();
  917. }
  918. if (typeof obj.builtAt === "number") {
  919. const builtAtDate = new Date(obj.builtAt);
  920. colors.normal("Built at: ");
  921. colors.normal(
  922. builtAtDate.toLocaleDateString(undefined, {
  923. day: "2-digit",
  924. month: "2-digit",
  925. year: "numeric"
  926. })
  927. );
  928. colors.normal(" ");
  929. colors.bold(builtAtDate.toLocaleTimeString());
  930. newline();
  931. }
  932. if (obj.env) {
  933. colors.normal("Environment (--env): ");
  934. colors.bold(JSON.stringify(obj.env, null, 2));
  935. newline();
  936. }
  937. if (obj.publicPath) {
  938. colors.normal("PublicPath: ");
  939. colors.bold(obj.publicPath);
  940. newline();
  941. }
  942. if (obj.assets && obj.assets.length > 0) {
  943. const t = [
  944. [
  945. {
  946. value: "Asset",
  947. color: colors.bold
  948. },
  949. {
  950. value: "Size",
  951. color: colors.bold
  952. },
  953. {
  954. value: "Chunks",
  955. color: colors.bold
  956. },
  957. {
  958. value: "",
  959. color: colors.bold
  960. },
  961. {
  962. value: "",
  963. color: colors.bold
  964. },
  965. {
  966. value: "Chunk Names",
  967. color: colors.bold
  968. }
  969. ]
  970. ];
  971. for (const asset of obj.assets) {
  972. t.push([
  973. {
  974. value: asset.name,
  975. color: getAssetColor(asset, colors.green)
  976. },
  977. {
  978. value: SizeFormatHelpers.formatSize(asset.size),
  979. color: getAssetColor(asset, colors.normal)
  980. },
  981. {
  982. value: asset.chunks.join(", "),
  983. color: colors.bold
  984. },
  985. {
  986. value: [
  987. asset.emitted && "[emitted]",
  988. asset.info.immutable && "[immutable]",
  989. asset.info.development && "[dev]",
  990. asset.info.hotModuleReplacement && "[hmr]"
  991. ]
  992. .filter(Boolean)
  993. .join(" "),
  994. color: colors.green
  995. },
  996. {
  997. value: asset.isOverSizeLimit ? "[big]" : "",
  998. color: getAssetColor(asset, colors.normal)
  999. },
  1000. {
  1001. value: asset.chunkNames.join(", "),
  1002. color: colors.normal
  1003. }
  1004. ]);
  1005. }
  1006. table(t, "rrrlll");
  1007. }
  1008. if (obj.filteredAssets > 0) {
  1009. colors.normal(" ");
  1010. if (obj.assets.length > 0) colors.normal("+ ");
  1011. colors.normal(obj.filteredAssets);
  1012. if (obj.assets.length > 0) colors.normal(" hidden");
  1013. colors.normal(obj.filteredAssets !== 1 ? " assets" : " asset");
  1014. newline();
  1015. }
  1016. const processChunkGroups = (namedGroups, prefix) => {
  1017. for (const name of Object.keys(namedGroups)) {
  1018. const cg = namedGroups[name];
  1019. colors.normal(`${prefix} `);
  1020. colors.bold(name);
  1021. if (cg.isOverSizeLimit) {
  1022. colors.normal(" ");
  1023. colors.yellow("[big]");
  1024. }
  1025. colors.normal(" =");
  1026. for (const asset of cg.assets) {
  1027. colors.normal(" ");
  1028. colors.green(asset);
  1029. }
  1030. for (const name of Object.keys(cg.childAssets)) {
  1031. const assets = cg.childAssets[name];
  1032. if (assets && assets.length > 0) {
  1033. colors.normal(" ");
  1034. colors.magenta(`(${name}:`);
  1035. for (const asset of assets) {
  1036. colors.normal(" ");
  1037. colors.green(asset);
  1038. }
  1039. colors.magenta(")");
  1040. }
  1041. }
  1042. newline();
  1043. }
  1044. };
  1045. if (obj.entrypoints) {
  1046. processChunkGroups(obj.entrypoints, "Entrypoint");
  1047. }
  1048. if (obj.namedChunkGroups) {
  1049. let outputChunkGroups = obj.namedChunkGroups;
  1050. if (obj.entrypoints) {
  1051. outputChunkGroups = Object.keys(outputChunkGroups)
  1052. .filter(name => !obj.entrypoints[name])
  1053. .reduce((result, name) => {
  1054. result[name] = obj.namedChunkGroups[name];
  1055. return result;
  1056. }, {});
  1057. }
  1058. processChunkGroups(outputChunkGroups, "Chunk Group");
  1059. }
  1060. const modulesByIdentifier = {};
  1061. if (obj.modules) {
  1062. for (const module of obj.modules) {
  1063. modulesByIdentifier[`$${module.identifier}`] = module;
  1064. }
  1065. } else if (obj.chunks) {
  1066. for (const chunk of obj.chunks) {
  1067. if (chunk.modules) {
  1068. for (const module of chunk.modules) {
  1069. modulesByIdentifier[`$${module.identifier}`] = module;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. const processModuleAttributes = module => {
  1075. colors.normal(" ");
  1076. colors.normal(SizeFormatHelpers.formatSize(module.size));
  1077. if (module.chunks) {
  1078. for (const chunk of module.chunks) {
  1079. colors.normal(" {");
  1080. colors.yellow(chunk);
  1081. colors.normal("}");
  1082. }
  1083. }
  1084. if (typeof module.depth === "number") {
  1085. colors.normal(` [depth ${module.depth}]`);
  1086. }
  1087. if (module.cacheable === false) {
  1088. colors.red(" [not cacheable]");
  1089. }
  1090. if (module.optional) {
  1091. colors.yellow(" [optional]");
  1092. }
  1093. if (module.built) {
  1094. colors.green(" [built]");
  1095. }
  1096. if (module.assets && module.assets.length) {
  1097. colors.magenta(
  1098. ` [${module.assets.length} asset${
  1099. module.assets.length === 1 ? "" : "s"
  1100. }]`
  1101. );
  1102. }
  1103. if (module.prefetched) {
  1104. colors.magenta(" [prefetched]");
  1105. }
  1106. if (module.failed) colors.red(" [failed]");
  1107. if (module.warnings) {
  1108. colors.yellow(
  1109. ` [${module.warnings} warning${module.warnings === 1 ? "" : "s"}]`
  1110. );
  1111. }
  1112. if (module.errors) {
  1113. colors.red(
  1114. ` [${module.errors} error${module.errors === 1 ? "" : "s"}]`
  1115. );
  1116. }
  1117. };
  1118. const processModuleContent = (module, prefix) => {
  1119. if (Array.isArray(module.providedExports)) {
  1120. colors.normal(prefix);
  1121. if (module.providedExports.length === 0) {
  1122. colors.cyan("[no exports]");
  1123. } else {
  1124. colors.cyan(`[exports: ${module.providedExports.join(", ")}]`);
  1125. }
  1126. newline();
  1127. }
  1128. if (module.usedExports !== undefined) {
  1129. if (module.usedExports !== true) {
  1130. colors.normal(prefix);
  1131. if (module.usedExports === null) {
  1132. colors.cyan("[used exports unknown]");
  1133. } else if (module.usedExports === false) {
  1134. colors.cyan("[no exports used]");
  1135. } else if (
  1136. Array.isArray(module.usedExports) &&
  1137. module.usedExports.length === 0
  1138. ) {
  1139. colors.cyan("[no exports used]");
  1140. } else if (Array.isArray(module.usedExports)) {
  1141. const providedExportsCount = Array.isArray(module.providedExports)
  1142. ? module.providedExports.length
  1143. : null;
  1144. if (
  1145. providedExportsCount !== null &&
  1146. providedExportsCount === module.usedExports.length
  1147. ) {
  1148. colors.cyan("[all exports used]");
  1149. } else {
  1150. colors.cyan(
  1151. `[only some exports used: ${module.usedExports.join(", ")}]`
  1152. );
  1153. }
  1154. }
  1155. newline();
  1156. }
  1157. }
  1158. if (Array.isArray(module.optimizationBailout)) {
  1159. for (const item of module.optimizationBailout) {
  1160. colors.normal(prefix);
  1161. colors.yellow(item);
  1162. newline();
  1163. }
  1164. }
  1165. if (module.reasons) {
  1166. for (const reason of module.reasons) {
  1167. colors.normal(prefix);
  1168. if (reason.type) {
  1169. colors.normal(reason.type);
  1170. colors.normal(" ");
  1171. }
  1172. if (reason.userRequest) {
  1173. colors.cyan(reason.userRequest);
  1174. colors.normal(" ");
  1175. }
  1176. if (reason.moduleId !== null) {
  1177. colors.normal("[");
  1178. colors.normal(reason.moduleId);
  1179. colors.normal("]");
  1180. }
  1181. if (reason.module && reason.module !== reason.moduleId) {
  1182. colors.normal(" ");
  1183. colors.magenta(reason.module);
  1184. }
  1185. if (reason.loc) {
  1186. colors.normal(" ");
  1187. colors.normal(reason.loc);
  1188. }
  1189. if (reason.explanation) {
  1190. colors.normal(" ");
  1191. colors.cyan(reason.explanation);
  1192. }
  1193. newline();
  1194. }
  1195. }
  1196. if (module.profile) {
  1197. colors.normal(prefix);
  1198. let sum = 0;
  1199. if (module.issuerPath) {
  1200. for (const m of module.issuerPath) {
  1201. colors.normal("[");
  1202. colors.normal(m.id);
  1203. colors.normal("] ");
  1204. if (m.profile) {
  1205. const time = (m.profile.factory || 0) + (m.profile.building || 0);
  1206. coloredTime(time);
  1207. sum += time;
  1208. colors.normal(" ");
  1209. }
  1210. colors.normal("-> ");
  1211. }
  1212. }
  1213. for (const key of Object.keys(module.profile)) {
  1214. colors.normal(`${key}:`);
  1215. const time = module.profile[key];
  1216. coloredTime(time);
  1217. colors.normal(" ");
  1218. sum += time;
  1219. }
  1220. colors.normal("= ");
  1221. coloredTime(sum);
  1222. newline();
  1223. }
  1224. if (module.modules) {
  1225. processModulesList(module, prefix + "| ");
  1226. }
  1227. };
  1228. const processModulesList = (obj, prefix) => {
  1229. if (obj.modules) {
  1230. let maxModuleId = 0;
  1231. for (const module of obj.modules) {
  1232. if (typeof module.id === "number") {
  1233. if (maxModuleId < module.id) maxModuleId = module.id;
  1234. }
  1235. }
  1236. let contentPrefix = prefix + " ";
  1237. if (maxModuleId >= 10) contentPrefix += " ";
  1238. if (maxModuleId >= 100) contentPrefix += " ";
  1239. if (maxModuleId >= 1000) contentPrefix += " ";
  1240. for (const module of obj.modules) {
  1241. colors.normal(prefix);
  1242. const name = module.name || module.identifier;
  1243. if (typeof module.id === "string" || typeof module.id === "number") {
  1244. if (typeof module.id === "number") {
  1245. if (module.id < 1000 && maxModuleId >= 1000) colors.normal(" ");
  1246. if (module.id < 100 && maxModuleId >= 100) colors.normal(" ");
  1247. if (module.id < 10 && maxModuleId >= 10) colors.normal(" ");
  1248. } else {
  1249. if (maxModuleId >= 1000) colors.normal(" ");
  1250. if (maxModuleId >= 100) colors.normal(" ");
  1251. if (maxModuleId >= 10) colors.normal(" ");
  1252. }
  1253. if (name !== module.id) {
  1254. colors.normal("[");
  1255. colors.normal(module.id);
  1256. colors.normal("]");
  1257. colors.normal(" ");
  1258. } else {
  1259. colors.normal("[");
  1260. colors.bold(module.id);
  1261. colors.normal("]");
  1262. }
  1263. }
  1264. if (name !== module.id) {
  1265. colors.bold(name);
  1266. }
  1267. processModuleAttributes(module);
  1268. newline();
  1269. processModuleContent(module, contentPrefix);
  1270. }
  1271. if (obj.filteredModules > 0) {
  1272. colors.normal(prefix);
  1273. colors.normal(" ");
  1274. if (obj.modules.length > 0) colors.normal(" + ");
  1275. colors.normal(obj.filteredModules);
  1276. if (obj.modules.length > 0) colors.normal(" hidden");
  1277. colors.normal(obj.filteredModules !== 1 ? " modules" : " module");
  1278. newline();
  1279. }
  1280. }
  1281. };
  1282. if (obj.chunks) {
  1283. for (const chunk of obj.chunks) {
  1284. colors.normal("chunk ");
  1285. if (chunk.id < 1000) colors.normal(" ");
  1286. if (chunk.id < 100) colors.normal(" ");
  1287. if (chunk.id < 10) colors.normal(" ");
  1288. colors.normal("{");
  1289. colors.yellow(chunk.id);
  1290. colors.normal("} ");
  1291. colors.green(chunk.files.join(", "));
  1292. if (chunk.names && chunk.names.length > 0) {
  1293. colors.normal(" (");
  1294. colors.normal(chunk.names.join(", "));
  1295. colors.normal(")");
  1296. }
  1297. colors.normal(" ");
  1298. colors.normal(SizeFormatHelpers.formatSize(chunk.size));
  1299. for (const id of chunk.parents) {
  1300. colors.normal(" <{");
  1301. colors.yellow(id);
  1302. colors.normal("}>");
  1303. }
  1304. for (const id of chunk.siblings) {
  1305. colors.normal(" ={");
  1306. colors.yellow(id);
  1307. colors.normal("}=");
  1308. }
  1309. for (const id of chunk.children) {
  1310. colors.normal(" >{");
  1311. colors.yellow(id);
  1312. colors.normal("}<");
  1313. }
  1314. if (chunk.childrenByOrder) {
  1315. for (const name of Object.keys(chunk.childrenByOrder)) {
  1316. const children = chunk.childrenByOrder[name];
  1317. colors.normal(" ");
  1318. colors.magenta(`(${name}:`);
  1319. for (const id of children) {
  1320. colors.normal(" {");
  1321. colors.yellow(id);
  1322. colors.normal("}");
  1323. }
  1324. colors.magenta(")");
  1325. }
  1326. }
  1327. if (chunk.entry) {
  1328. colors.yellow(" [entry]");
  1329. } else if (chunk.initial) {
  1330. colors.yellow(" [initial]");
  1331. }
  1332. if (chunk.rendered) {
  1333. colors.green(" [rendered]");
  1334. }
  1335. if (chunk.recorded) {
  1336. colors.green(" [recorded]");
  1337. }
  1338. if (chunk.reason) {
  1339. colors.yellow(` ${chunk.reason}`);
  1340. }
  1341. newline();
  1342. if (chunk.origins) {
  1343. for (const origin of chunk.origins) {
  1344. colors.normal(" > ");
  1345. if (origin.reasons && origin.reasons.length) {
  1346. colors.yellow(origin.reasons.join(" "));
  1347. colors.normal(" ");
  1348. }
  1349. if (origin.request) {
  1350. colors.normal(origin.request);
  1351. colors.normal(" ");
  1352. }
  1353. if (origin.module) {
  1354. colors.normal("[");
  1355. colors.normal(origin.moduleId);
  1356. colors.normal("] ");
  1357. const module = modulesByIdentifier[`$${origin.module}`];
  1358. if (module) {
  1359. colors.bold(module.name);
  1360. colors.normal(" ");
  1361. }
  1362. }
  1363. if (origin.loc) {
  1364. colors.normal(origin.loc);
  1365. }
  1366. newline();
  1367. }
  1368. }
  1369. processModulesList(chunk, " ");
  1370. }
  1371. }
  1372. processModulesList(obj, "");
  1373. if (obj.logging) {
  1374. for (const origin of Object.keys(obj.logging)) {
  1375. const logData = obj.logging[origin];
  1376. if (logData.entries.length > 0) {
  1377. newline();
  1378. if (logData.debug) {
  1379. colors.red("DEBUG ");
  1380. }
  1381. colors.bold("LOG from " + origin);
  1382. newline();
  1383. let indent = "";
  1384. for (const entry of logData.entries) {
  1385. let color = colors.normal;
  1386. let prefix = " ";
  1387. switch (entry.type) {
  1388. case LogType.clear:
  1389. colors.normal(`${indent}-------`);
  1390. newline();
  1391. continue;
  1392. case LogType.error:
  1393. color = colors.red;
  1394. prefix = "<e> ";
  1395. break;
  1396. case LogType.warn:
  1397. color = colors.yellow;
  1398. prefix = "<w> ";
  1399. break;
  1400. case LogType.info:
  1401. color = colors.green;
  1402. prefix = "<i> ";
  1403. break;
  1404. case LogType.log:
  1405. color = colors.bold;
  1406. break;
  1407. case LogType.trace:
  1408. case LogType.debug:
  1409. color = colors.normal;
  1410. break;
  1411. case LogType.status:
  1412. color = colors.magenta;
  1413. prefix = "<s> ";
  1414. break;
  1415. case LogType.profile:
  1416. color = colors.magenta;
  1417. prefix = "<p> ";
  1418. break;
  1419. case LogType.profileEnd:
  1420. color = colors.magenta;
  1421. prefix = "</p> ";
  1422. break;
  1423. case LogType.time:
  1424. color = colors.magenta;
  1425. prefix = "<t> ";
  1426. break;
  1427. case LogType.group:
  1428. color = colors.cyan;
  1429. prefix = "<-> ";
  1430. break;
  1431. case LogType.groupCollapsed:
  1432. color = colors.cyan;
  1433. prefix = "<+> ";
  1434. break;
  1435. case LogType.groupEnd:
  1436. if (indent.length >= 2)
  1437. indent = indent.slice(0, indent.length - 2);
  1438. continue;
  1439. }
  1440. if (entry.message) {
  1441. for (const line of entry.message.split("\n")) {
  1442. colors.normal(`${indent}${prefix}`);
  1443. color(line);
  1444. newline();
  1445. }
  1446. }
  1447. if (entry.trace) {
  1448. for (const line of entry.trace) {
  1449. colors.normal(`${indent}| ${line}`);
  1450. newline();
  1451. }
  1452. }
  1453. switch (entry.type) {
  1454. case LogType.group:
  1455. indent += " ";
  1456. break;
  1457. }
  1458. }
  1459. if (logData.filteredEntries) {
  1460. colors.normal(`+ ${logData.filteredEntries} hidden lines`);
  1461. newline();
  1462. }
  1463. }
  1464. }
  1465. }
  1466. if (obj._showWarnings && obj.warnings) {
  1467. for (const warning of obj.warnings) {
  1468. newline();
  1469. colors.yellow(`WARNING in ${warning}`);
  1470. newline();
  1471. }
  1472. }
  1473. if (obj._showErrors && obj.errors) {
  1474. for (const error of obj.errors) {
  1475. newline();
  1476. colors.red(`ERROR in ${error}`);
  1477. newline();
  1478. }
  1479. }
  1480. if (obj.children) {
  1481. for (const child of obj.children) {
  1482. const childString = Stats.jsonToString(child, useColors);
  1483. if (childString) {
  1484. if (child.name) {
  1485. colors.normal("Child ");
  1486. colors.bold(child.name);
  1487. colors.normal(":");
  1488. } else {
  1489. colors.normal("Child");
  1490. }
  1491. newline();
  1492. buf.push(" ");
  1493. buf.push(childString.replace(/\n/g, "\n "));
  1494. newline();
  1495. }
  1496. }
  1497. }
  1498. if (obj.needAdditionalPass) {
  1499. colors.yellow(
  1500. "Compilation needs an additional pass and will compile again."
  1501. );
  1502. }
  1503. while (buf[buf.length - 1] === "\n") {
  1504. buf.pop();
  1505. }
  1506. return buf.join("");
  1507. }
  1508. static presetToOptions(name) {
  1509. // Accepted values: none, errors-only, minimal, normal, detailed, verbose
  1510. // Any other falsy value will behave as 'none', truthy values as 'normal'
  1511. const pn =
  1512. (typeof name === "string" && name.toLowerCase()) || name || "none";
  1513. switch (pn) {
  1514. case "none":
  1515. return {
  1516. all: false
  1517. };
  1518. case "verbose":
  1519. return {
  1520. entrypoints: true,
  1521. chunkGroups: true,
  1522. modules: false,
  1523. chunks: true,
  1524. chunkModules: true,
  1525. chunkOrigins: true,
  1526. depth: true,
  1527. env: true,
  1528. reasons: true,
  1529. usedExports: true,
  1530. providedExports: true,
  1531. optimizationBailout: true,
  1532. errorDetails: true,
  1533. publicPath: true,
  1534. logging: "verbose",
  1535. exclude: false,
  1536. maxModules: Infinity
  1537. };
  1538. case "detailed":
  1539. return {
  1540. entrypoints: true,
  1541. chunkGroups: true,
  1542. chunks: true,
  1543. chunkModules: false,
  1544. chunkOrigins: true,
  1545. depth: true,
  1546. usedExports: true,
  1547. providedExports: true,
  1548. optimizationBailout: true,
  1549. errorDetails: true,
  1550. publicPath: true,
  1551. logging: true,
  1552. exclude: false,
  1553. maxModules: Infinity
  1554. };
  1555. case "minimal":
  1556. return {
  1557. all: false,
  1558. modules: true,
  1559. maxModules: 0,
  1560. errors: true,
  1561. warnings: true,
  1562. logging: "warn"
  1563. };
  1564. case "errors-only":
  1565. return {
  1566. all: false,
  1567. errors: true,
  1568. moduleTrace: true,
  1569. logging: "error"
  1570. };
  1571. case "errors-warnings":
  1572. return {
  1573. all: false,
  1574. errors: true,
  1575. warnings: true,
  1576. logging: "warn"
  1577. };
  1578. default:
  1579. return {};
  1580. }
  1581. }
  1582. static getChildOptions(options, idx) {
  1583. let innerOptions;
  1584. if (Array.isArray(options.children)) {
  1585. if (idx < options.children.length) {
  1586. innerOptions = options.children[idx];
  1587. }
  1588. } else if (typeof options.children === "object" && options.children) {
  1589. innerOptions = options.children;
  1590. }
  1591. if (typeof innerOptions === "boolean" || typeof innerOptions === "string") {
  1592. innerOptions = Stats.presetToOptions(innerOptions);
  1593. }
  1594. if (!innerOptions) {
  1595. return options;
  1596. }
  1597. const childOptions = Object.assign({}, options);
  1598. delete childOptions.children; // do not inherit children
  1599. return Object.assign(childOptions, innerOptions);
  1600. }
  1601. }
  1602. module.exports = Stats;