Stats.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 optionsOrFallback = function() {
  11. let optionValues = [];
  12. optionValues.push.apply(optionValues, arguments);
  13. return optionValues.find(optionValue => typeof optionValue !== "undefined");
  14. };
  15. class Stats {
  16. constructor(compilation) {
  17. this.compilation = compilation;
  18. this.hash = compilation.hash;
  19. }
  20. static filterWarnings(warnings, warningsFilter) {
  21. // we dont have anything to filter so all warnings can be shown
  22. if(!warningsFilter) {
  23. return warnings;
  24. }
  25. // create a chain of filters
  26. // if they return "true" a warning should be surpressed
  27. const normalizedWarningsFilters = [].concat(warningsFilter).map(filter => {
  28. if(typeof filter === "string") {
  29. return warning => warning.indexOf(filter) > -1;
  30. }
  31. if(filter instanceof RegExp) {
  32. return warning => filter.test(warning);
  33. }
  34. if(typeof filter === "function") {
  35. return filter;
  36. }
  37. throw new Error(`Can only filter warnings with Strings or RegExps. (Given: ${filter})`);
  38. });
  39. return warnings.filter(warning => {
  40. return !normalizedWarningsFilters.some(check => check(warning));
  41. });
  42. }
  43. hasWarnings() {
  44. return this.compilation.warnings.length > 0 ||
  45. this.compilation.children.some(child => child.getStats().hasWarnings());
  46. }
  47. hasErrors() {
  48. return this.compilation.errors.length > 0 ||
  49. this.compilation.children.some(child => child.getStats().hasErrors());
  50. }
  51. // remove a prefixed "!" that can be specified to reverse sort order
  52. normalizeFieldKey(field) {
  53. if(field[0] === "!") {
  54. return field.substr(1);
  55. }
  56. return field;
  57. }
  58. // if a field is prefixed by a "!" reverse sort order
  59. sortOrderRegular(field) {
  60. if(field[0] === "!") {
  61. return false;
  62. }
  63. return true;
  64. }
  65. toJson(options, forToString) {
  66. if(typeof options === "boolean" || typeof options === "string") {
  67. options = Stats.presetToOptions(options);
  68. } else if(!options) {
  69. options = {};
  70. }
  71. const optionOrLocalFallback = (v, def) =>
  72. typeof v !== "undefined" ? v :
  73. typeof options.all !== "undefined" ? options.all : def;
  74. const testAgainstGivenOption = (item) => {
  75. if(typeof item === "string") {
  76. const regExp = new RegExp(`[\\\\/]${item.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")}([\\\\/]|$|!|\\?)`); // eslint-disable-line no-useless-escape
  77. return ident => regExp.test(ident);
  78. }
  79. if(item && typeof item === "object" && typeof item.test === "function")
  80. return ident => item.test(ident);
  81. if(typeof item === "function")
  82. return item;
  83. };
  84. const compilation = this.compilation;
  85. const context = optionsOrFallback(options.context, process.cwd());
  86. const requestShortener = new RequestShortener(context);
  87. const showPerformance = optionOrLocalFallback(options.performance, true);
  88. const showHash = optionOrLocalFallback(options.hash, true);
  89. const showEnv = optionOrLocalFallback(options.env, false);
  90. const showVersion = optionOrLocalFallback(options.version, true);
  91. const showTimings = optionOrLocalFallback(options.timings, true);
  92. const showAssets = optionOrLocalFallback(options.assets, true);
  93. const showEntrypoints = optionOrLocalFallback(options.entrypoints, !forToString);
  94. const showChunks = optionOrLocalFallback(options.chunks, !forToString);
  95. const showChunkModules = optionOrLocalFallback(options.chunkModules, true);
  96. const showChunkOrigins = optionOrLocalFallback(options.chunkOrigins, !forToString);
  97. const showModules = optionOrLocalFallback(options.modules, true);
  98. const showDepth = optionOrLocalFallback(options.depth, !forToString);
  99. const showCachedModules = optionOrLocalFallback(options.cached, true);
  100. const showCachedAssets = optionOrLocalFallback(options.cachedAssets, true);
  101. const showReasons = optionOrLocalFallback(options.reasons, !forToString);
  102. const showUsedExports = optionOrLocalFallback(options.usedExports, !forToString);
  103. const showProvidedExports = optionOrLocalFallback(options.providedExports, !forToString);
  104. const showOptimizationBailout = optionOrLocalFallback(options.optimizationBailout, !forToString);
  105. const showChildren = optionOrLocalFallback(options.children, true);
  106. const showSource = optionOrLocalFallback(options.source, !forToString);
  107. const showModuleTrace = optionOrLocalFallback(options.moduleTrace, true);
  108. const showErrors = optionOrLocalFallback(options.errors, true);
  109. const showErrorDetails = optionOrLocalFallback(options.errorDetails, !forToString);
  110. const showWarnings = optionOrLocalFallback(options.warnings, true);
  111. const warningsFilter = optionsOrFallback(options.warningsFilter, null);
  112. const showPublicPath = optionOrLocalFallback(options.publicPath, !forToString);
  113. const excludeModules = [].concat(optionsOrFallback(options.excludeModules, options.exclude, [])).map(testAgainstGivenOption);
  114. const excludeAssets = [].concat(optionsOrFallback(options.excludeAssets, [])).map(testAgainstGivenOption);
  115. const maxModules = optionsOrFallback(options.maxModules, forToString ? 15 : Infinity);
  116. const sortModules = optionsOrFallback(options.modulesSort, "id");
  117. const sortChunks = optionsOrFallback(options.chunksSort, "id");
  118. const sortAssets = optionsOrFallback(options.assetsSort, "");
  119. if(!showCachedModules) {
  120. excludeModules.push((ident, module) => !module.built);
  121. }
  122. const createModuleFilter = () => {
  123. let i = 0;
  124. return module => {
  125. if(excludeModules.length > 0) {
  126. const ident = requestShortener.shorten(module.resource);
  127. const excluded = excludeModules.some(fn => fn(ident, module));
  128. if(excluded)
  129. return false;
  130. }
  131. return i++ < maxModules;
  132. };
  133. };
  134. const createAssetFilter = () => {
  135. return asset => {
  136. if(excludeAssets.length > 0) {
  137. const ident = asset.name;
  138. const excluded = excludeAssets.some(fn => fn(ident, asset));
  139. if(excluded)
  140. return false;
  141. }
  142. return showCachedAssets || asset.emitted;
  143. };
  144. };
  145. const sortByFieldAndOrder = (fieldKey, a, b) => {
  146. if(a[fieldKey] === null && b[fieldKey] === null) return 0;
  147. if(a[fieldKey] === null) return 1;
  148. if(b[fieldKey] === null) return -1;
  149. if(a[fieldKey] === b[fieldKey]) return 0;
  150. return a[fieldKey] < b[fieldKey] ? -1 : 1;
  151. };
  152. const sortByField = (field) => (a, b) => {
  153. if(!field) {
  154. return 0;
  155. }
  156. const fieldKey = this.normalizeFieldKey(field);
  157. // if a field is prefixed with a "!" the sort is reversed!
  158. const sortIsRegular = this.sortOrderRegular(field);
  159. return sortByFieldAndOrder(fieldKey, sortIsRegular ? a : b, sortIsRegular ? b : a);
  160. };
  161. const formatError = (e) => {
  162. let text = "";
  163. if(typeof e === "string")
  164. e = {
  165. message: e
  166. };
  167. if(e.chunk) {
  168. text += `chunk ${e.chunk.name || e.chunk.id}${e.chunk.hasRuntime() ? " [entry]" : e.chunk.isInitial() ? " [initial]" : ""}\n`;
  169. }
  170. if(e.file) {
  171. text += `${e.file}\n`;
  172. }
  173. if(e.module && e.module.readableIdentifier && typeof e.module.readableIdentifier === "function") {
  174. text += `${e.module.readableIdentifier(requestShortener)}\n`;
  175. }
  176. text += e.message;
  177. if(showErrorDetails && e.details) text += `\n${e.details}`;
  178. if(showErrorDetails && e.missing) text += e.missing.map(item => `\n[${item}]`).join("");
  179. if(showModuleTrace && e.origin) {
  180. text += `\n @ ${e.origin.readableIdentifier(requestShortener)}`;
  181. if(typeof e.originLoc === "object") {
  182. const locInfo = formatLocation(e.originLoc);
  183. if(locInfo)
  184. text += ` ${locInfo}`;
  185. }
  186. if(e.dependencies) {
  187. e.dependencies.forEach(dep => {
  188. if(!dep.loc) return;
  189. if(typeof dep.loc === "string") return;
  190. const locInfo = formatLocation(dep.loc);
  191. if(!locInfo) return;
  192. text += ` ${locInfo}`;
  193. });
  194. }
  195. let current = e.origin;
  196. while(current.issuer) {
  197. current = current.issuer;
  198. text += `\n @ ${current.readableIdentifier(requestShortener)}`;
  199. }
  200. }
  201. return text;
  202. };
  203. const obj = {
  204. errors: compilation.errors.map(formatError),
  205. warnings: Stats.filterWarnings(compilation.warnings.map(formatError), warningsFilter)
  206. };
  207. //We just hint other renderers since actually omitting
  208. //errors/warnings from the JSON would be kind of weird.
  209. Object.defineProperty(obj, "_showWarnings", {
  210. value: showWarnings,
  211. enumerable: false
  212. });
  213. Object.defineProperty(obj, "_showErrors", {
  214. value: showErrors,
  215. enumerable: false
  216. });
  217. if(showVersion) {
  218. obj.version = require("../package.json").version;
  219. }
  220. if(showHash) obj.hash = this.hash;
  221. if(showTimings && this.startTime && this.endTime) {
  222. obj.time = this.endTime - this.startTime;
  223. }
  224. if(showEnv && options._env) {
  225. obj.env = options._env;
  226. }
  227. if(compilation.needAdditionalPass) {
  228. obj.needAdditionalPass = true;
  229. }
  230. if(showPublicPath) {
  231. obj.publicPath = this.compilation.mainTemplate.getPublicPath({
  232. hash: this.compilation.hash
  233. });
  234. }
  235. if(showAssets) {
  236. const assetsByFile = {};
  237. const compilationAssets = Object.keys(compilation.assets);
  238. obj.assetsByChunkName = {};
  239. obj.assets = compilationAssets.map(asset => {
  240. const obj = {
  241. name: asset,
  242. size: compilation.assets[asset].size(),
  243. chunks: [],
  244. chunkNames: [],
  245. emitted: compilation.assets[asset].emitted
  246. };
  247. if(showPerformance) {
  248. obj.isOverSizeLimit = compilation.assets[asset].isOverSizeLimit;
  249. }
  250. assetsByFile[asset] = obj;
  251. return obj;
  252. }).filter(createAssetFilter());
  253. obj.filteredAssets = compilationAssets.length - obj.assets.length;
  254. compilation.chunks.forEach(chunk => {
  255. chunk.files.forEach(asset => {
  256. if(assetsByFile[asset]) {
  257. chunk.ids.forEach(id => {
  258. assetsByFile[asset].chunks.push(id);
  259. });
  260. if(chunk.name) {
  261. assetsByFile[asset].chunkNames.push(chunk.name);
  262. if(obj.assetsByChunkName[chunk.name])
  263. obj.assetsByChunkName[chunk.name] = [].concat(obj.assetsByChunkName[chunk.name]).concat([asset]);
  264. else
  265. obj.assetsByChunkName[chunk.name] = asset;
  266. }
  267. }
  268. });
  269. });
  270. obj.assets.sort(sortByField(sortAssets));
  271. }
  272. if(showEntrypoints) {
  273. obj.entrypoints = {};
  274. Object.keys(compilation.entrypoints).forEach(name => {
  275. const ep = compilation.entrypoints[name];
  276. obj.entrypoints[name] = {
  277. chunks: ep.chunks.map(c => c.id),
  278. assets: ep.chunks.reduce((array, c) => array.concat(c.files || []), [])
  279. };
  280. if(showPerformance) {
  281. obj.entrypoints[name].isOverSizeLimit = ep.isOverSizeLimit;
  282. }
  283. });
  284. }
  285. function fnModule(module) {
  286. const obj = {
  287. id: module.id,
  288. identifier: module.identifier(),
  289. name: module.readableIdentifier(requestShortener),
  290. index: module.index,
  291. index2: module.index2,
  292. size: module.size(),
  293. cacheable: !!module.cacheable,
  294. built: !!module.built,
  295. optional: !!module.optional,
  296. prefetched: !!module.prefetched,
  297. chunks: module.mapChunks(chunk => chunk.id),
  298. assets: Object.keys(module.assets || {}),
  299. issuer: module.issuer && module.issuer.identifier(),
  300. issuerId: module.issuer && module.issuer.id,
  301. issuerName: module.issuer && module.issuer.readableIdentifier(requestShortener),
  302. profile: module.profile,
  303. failed: !!module.error,
  304. errors: module.errors && module.dependenciesErrors && (module.errors.length + module.dependenciesErrors.length),
  305. warnings: module.errors && module.dependenciesErrors && (module.warnings.length + module.dependenciesWarnings.length)
  306. };
  307. if(showReasons) {
  308. obj.reasons = module.reasons.filter(reason => reason.dependency && reason.module).map(reason => {
  309. const obj = {
  310. moduleId: reason.module.id,
  311. moduleIdentifier: reason.module.identifier(),
  312. module: reason.module.readableIdentifier(requestShortener),
  313. moduleName: reason.module.readableIdentifier(requestShortener),
  314. type: reason.dependency.type,
  315. userRequest: reason.dependency.userRequest
  316. };
  317. const locInfo = formatLocation(reason.dependency.loc);
  318. if(locInfo) obj.loc = locInfo;
  319. return obj;
  320. }).sort((a, b) => a.moduleId - b.moduleId);
  321. }
  322. if(showUsedExports) {
  323. obj.usedExports = module.used ? module.usedExports : false;
  324. }
  325. if(showProvidedExports) {
  326. obj.providedExports = Array.isArray(module.providedExports) ? module.providedExports : null;
  327. }
  328. if(showOptimizationBailout) {
  329. obj.optimizationBailout = module.optimizationBailout.map(item => {
  330. if(typeof item === "function") return item(requestShortener);
  331. return item;
  332. });
  333. }
  334. if(showDepth) {
  335. obj.depth = module.depth;
  336. }
  337. if(showSource && module._source) {
  338. obj.source = module._source.source();
  339. }
  340. return obj;
  341. }
  342. if(showChunks) {
  343. obj.chunks = compilation.chunks.map(chunk => {
  344. const obj = {
  345. id: chunk.id,
  346. rendered: chunk.rendered,
  347. initial: chunk.isInitial(),
  348. entry: chunk.hasRuntime(),
  349. recorded: chunk.recorded,
  350. extraAsync: !!chunk.extraAsync,
  351. size: chunk.mapModules(m => m.size()).reduce((size, moduleSize) => size + moduleSize, 0),
  352. names: chunk.name ? [chunk.name] : [],
  353. files: chunk.files.slice(),
  354. hash: chunk.renderedHash,
  355. parents: chunk.parents.map(c => c.id)
  356. };
  357. if(showChunkModules) {
  358. obj.modules = chunk
  359. .getModules()
  360. .sort(sortByField("depth"))
  361. .filter(createModuleFilter())
  362. .map(fnModule);
  363. obj.filteredModules = chunk.getNumberOfModules() - obj.modules.length;
  364. obj.modules.sort(sortByField(sortModules));
  365. }
  366. if(showChunkOrigins) {
  367. obj.origins = chunk.origins.map(origin => ({
  368. moduleId: origin.module ? origin.module.id : undefined,
  369. module: origin.module ? origin.module.identifier() : "",
  370. moduleIdentifier: origin.module ? origin.module.identifier() : "",
  371. moduleName: origin.module ? origin.module.readableIdentifier(requestShortener) : "",
  372. loc: formatLocation(origin.loc),
  373. name: origin.name,
  374. reasons: origin.reasons || []
  375. }));
  376. }
  377. return obj;
  378. });
  379. obj.chunks.sort(sortByField(sortChunks));
  380. }
  381. if(showModules) {
  382. obj.modules = compilation.modules
  383. .slice()
  384. .sort(sortByField("depth"))
  385. .filter(createModuleFilter())
  386. .map(fnModule);
  387. obj.filteredModules = compilation.modules.length - obj.modules.length;
  388. obj.modules.sort(sortByField(sortModules));
  389. }
  390. if(showChildren) {
  391. obj.children = compilation.children.map((child, idx) => {
  392. const childOptions = Stats.getChildOptions(options, idx);
  393. const obj = new Stats(child).toJson(childOptions, forToString);
  394. delete obj.hash;
  395. delete obj.version;
  396. if(child.name)
  397. obj.name = identifierUtils.makePathsRelative(context, child.name, compilation.cache);
  398. return obj;
  399. });
  400. }
  401. return obj;
  402. }
  403. toString(options) {
  404. if(typeof options === "boolean" || typeof options === "string") {
  405. options = Stats.presetToOptions(options);
  406. } else if(!options) {
  407. options = {};
  408. }
  409. const useColors = optionsOrFallback(options.colors, false);
  410. const obj = this.toJson(options, true);
  411. return Stats.jsonToString(obj, useColors);
  412. }
  413. static jsonToString(obj, useColors) {
  414. const buf = [];
  415. const defaultColors = {
  416. bold: "\u001b[1m",
  417. yellow: "\u001b[1m\u001b[33m",
  418. red: "\u001b[1m\u001b[31m",
  419. green: "\u001b[1m\u001b[32m",
  420. cyan: "\u001b[1m\u001b[36m",
  421. magenta: "\u001b[1m\u001b[35m"
  422. };
  423. const colors = Object.keys(defaultColors).reduce((obj, color) => {
  424. obj[color] = str => {
  425. if(useColors) {
  426. buf.push(
  427. (useColors === true || useColors[color] === undefined) ?
  428. defaultColors[color] : useColors[color]
  429. );
  430. }
  431. buf.push(str);
  432. if(useColors) {
  433. buf.push("\u001b[39m\u001b[22m");
  434. }
  435. };
  436. return obj;
  437. }, {
  438. normal: (str) => buf.push(str)
  439. });
  440. const coloredTime = (time) => {
  441. let times = [800, 400, 200, 100];
  442. if(obj.time) {
  443. times = [obj.time / 2, obj.time / 4, obj.time / 8, obj.time / 16];
  444. }
  445. if(time < times[3])
  446. colors.normal(`${time}ms`);
  447. else if(time < times[2])
  448. colors.bold(`${time}ms`);
  449. else if(time < times[1])
  450. colors.green(`${time}ms`);
  451. else if(time < times[0])
  452. colors.yellow(`${time}ms`);
  453. else
  454. colors.red(`${time}ms`);
  455. };
  456. const newline = () => buf.push("\n");
  457. const getText = (arr, row, col) => {
  458. return arr[row][col].value;
  459. };
  460. const table = (array, align, splitter) => {
  461. const rows = array.length;
  462. const cols = array[0].length;
  463. const colSizes = new Array(cols);
  464. for(let col = 0; col < cols; col++)
  465. colSizes[col] = 0;
  466. for(let row = 0; row < rows; row++) {
  467. for(let col = 0; col < cols; col++) {
  468. const value = `${getText(array, row, col)}`;
  469. if(value.length > colSizes[col]) {
  470. colSizes[col] = value.length;
  471. }
  472. }
  473. }
  474. for(let row = 0; row < rows; row++) {
  475. for(let col = 0; col < cols; col++) {
  476. const format = array[row][col].color;
  477. const value = `${getText(array, row, col)}`;
  478. let l = value.length;
  479. if(align[col] === "l")
  480. format(value);
  481. for(; l < colSizes[col] && col !== cols - 1; l++)
  482. colors.normal(" ");
  483. if(align[col] === "r")
  484. format(value);
  485. if(col + 1 < cols && colSizes[col] !== 0)
  486. colors.normal(splitter || " ");
  487. }
  488. newline();
  489. }
  490. };
  491. const getAssetColor = (asset, defaultColor) => {
  492. if(asset.isOverSizeLimit) {
  493. return colors.yellow;
  494. }
  495. return defaultColor;
  496. };
  497. if(obj.hash) {
  498. colors.normal("Hash: ");
  499. colors.bold(obj.hash);
  500. newline();
  501. }
  502. if(obj.version) {
  503. colors.normal("Version: webpack ");
  504. colors.bold(obj.version);
  505. newline();
  506. }
  507. if(typeof obj.time === "number") {
  508. colors.normal("Time: ");
  509. colors.bold(obj.time);
  510. colors.normal("ms");
  511. newline();
  512. }
  513. if(obj.env) {
  514. colors.normal("Environment (--env): ");
  515. colors.bold(JSON.stringify(obj.env, null, 2));
  516. newline();
  517. }
  518. if(obj.publicPath) {
  519. colors.normal("PublicPath: ");
  520. colors.bold(obj.publicPath);
  521. newline();
  522. }
  523. if(obj.assets && obj.assets.length > 0) {
  524. const t = [
  525. [{
  526. value: "Asset",
  527. color: colors.bold
  528. }, {
  529. value: "Size",
  530. color: colors.bold
  531. }, {
  532. value: "Chunks",
  533. color: colors.bold
  534. }, {
  535. value: "",
  536. color: colors.bold
  537. }, {
  538. value: "",
  539. color: colors.bold
  540. }, {
  541. value: "Chunk Names",
  542. color: colors.bold
  543. }]
  544. ];
  545. obj.assets.forEach(asset => {
  546. t.push([{
  547. value: asset.name,
  548. color: getAssetColor(asset, colors.green)
  549. }, {
  550. value: SizeFormatHelpers.formatSize(asset.size),
  551. color: getAssetColor(asset, colors.normal)
  552. }, {
  553. value: asset.chunks.join(", "),
  554. color: colors.bold
  555. }, {
  556. value: asset.emitted ? "[emitted]" : "",
  557. color: colors.green
  558. }, {
  559. value: asset.isOverSizeLimit ? "[big]" : "",
  560. color: getAssetColor(asset, colors.normal)
  561. }, {
  562. value: asset.chunkNames.join(", "),
  563. color: colors.normal
  564. }]);
  565. });
  566. table(t, "rrrlll");
  567. }
  568. if(obj.filteredAssets > 0) {
  569. colors.normal(" ");
  570. if(obj.assets.length > 0)
  571. colors.normal("+ ");
  572. colors.normal(obj.filteredAssets);
  573. if(obj.assets.length > 0)
  574. colors.normal(" hidden");
  575. colors.normal(obj.filteredAssets !== 1 ? " assets" : " asset");
  576. newline();
  577. }
  578. if(obj.entrypoints) {
  579. Object.keys(obj.entrypoints).forEach(name => {
  580. const ep = obj.entrypoints[name];
  581. colors.normal("Entrypoint ");
  582. colors.bold(name);
  583. if(ep.isOverSizeLimit) {
  584. colors.normal(" ");
  585. colors.yellow("[big]");
  586. }
  587. colors.normal(" =");
  588. ep.assets.forEach(asset => {
  589. colors.normal(" ");
  590. colors.green(asset);
  591. });
  592. newline();
  593. });
  594. }
  595. const modulesByIdentifier = {};
  596. if(obj.modules) {
  597. obj.modules.forEach(module => {
  598. modulesByIdentifier[`$${module.identifier}`] = module;
  599. });
  600. } else if(obj.chunks) {
  601. obj.chunks.forEach(chunk => {
  602. if(chunk.modules) {
  603. chunk.modules.forEach(module => {
  604. modulesByIdentifier[`$${module.identifier}`] = module;
  605. });
  606. }
  607. });
  608. }
  609. const processModuleAttributes = (module) => {
  610. colors.normal(" ");
  611. colors.normal(SizeFormatHelpers.formatSize(module.size));
  612. if(module.chunks) {
  613. module.chunks.forEach(chunk => {
  614. colors.normal(" {");
  615. colors.yellow(chunk);
  616. colors.normal("}");
  617. });
  618. }
  619. if(typeof module.depth === "number") {
  620. colors.normal(` [depth ${module.depth}]`);
  621. }
  622. if(!module.cacheable) {
  623. colors.red(" [not cacheable]");
  624. }
  625. if(module.optional) {
  626. colors.yellow(" [optional]");
  627. }
  628. if(module.built) {
  629. colors.green(" [built]");
  630. }
  631. if(module.prefetched) {
  632. colors.magenta(" [prefetched]");
  633. }
  634. if(module.failed)
  635. colors.red(" [failed]");
  636. if(module.warnings)
  637. colors.yellow(` [${module.warnings} warning${module.warnings === 1 ? "" : "s"}]`);
  638. if(module.errors)
  639. colors.red(` [${module.errors} error${module.errors === 1 ? "" : "s"}]`);
  640. };
  641. const processModuleContent = (module, prefix) => {
  642. if(Array.isArray(module.providedExports)) {
  643. colors.normal(prefix);
  644. if(module.providedExports.length === 0)
  645. colors.cyan("[no exports]");
  646. else
  647. colors.cyan(`[exports: ${module.providedExports.join(", ")}]`);
  648. newline();
  649. }
  650. if(module.usedExports !== undefined) {
  651. if(module.usedExports !== true) {
  652. colors.normal(prefix);
  653. if(module.usedExports === false || module.usedExports.length === 0)
  654. colors.cyan("[no exports used]");
  655. else
  656. colors.cyan(`[only some exports used: ${module.usedExports.join(", ")}]`);
  657. newline();
  658. }
  659. }
  660. if(Array.isArray(module.optimizationBailout)) {
  661. module.optimizationBailout.forEach(item => {
  662. colors.normal(prefix);
  663. colors.yellow(item);
  664. newline();
  665. });
  666. }
  667. if(module.reasons) {
  668. module.reasons.forEach(reason => {
  669. colors.normal(prefix);
  670. colors.normal(reason.type);
  671. colors.normal(" ");
  672. colors.cyan(reason.userRequest);
  673. colors.normal(" [");
  674. colors.normal(reason.moduleId);
  675. colors.normal("] ");
  676. colors.magenta(reason.module);
  677. if(reason.loc) {
  678. colors.normal(" ");
  679. colors.normal(reason.loc);
  680. }
  681. newline();
  682. });
  683. }
  684. if(module.profile) {
  685. colors.normal(prefix);
  686. let sum = 0;
  687. const path = [];
  688. let current = module;
  689. while(current.issuer) {
  690. path.push(current = current.issuer);
  691. }
  692. path.reverse().forEach(module => {
  693. colors.normal("[");
  694. colors.normal(module.id);
  695. colors.normal("] ");
  696. if(module.profile) {
  697. const time = (module.profile.factory || 0) + (module.profile.building || 0);
  698. coloredTime(time);
  699. sum += time;
  700. colors.normal(" ");
  701. }
  702. colors.normal("->");
  703. });
  704. Object.keys(module.profile).forEach(key => {
  705. colors.normal(` ${key}:`);
  706. const time = module.profile[key];
  707. coloredTime(time);
  708. sum += time;
  709. });
  710. colors.normal(" = ");
  711. coloredTime(sum);
  712. newline();
  713. }
  714. };
  715. const processModulesList = (obj, prefix) => {
  716. if(obj.modules) {
  717. obj.modules.forEach(module => {
  718. colors.normal(prefix);
  719. if(module.id < 1000) colors.normal(" ");
  720. if(module.id < 100) colors.normal(" ");
  721. if(module.id < 10) colors.normal(" ");
  722. colors.normal("[");
  723. colors.normal(module.id);
  724. colors.normal("] ");
  725. colors.bold(module.name || module.identifier);
  726. processModuleAttributes(module);
  727. newline();
  728. processModuleContent(module, prefix + " ");
  729. });
  730. if(obj.filteredModules > 0) {
  731. colors.normal(prefix);
  732. colors.normal(" ");
  733. if(obj.modules.length > 0)
  734. colors.normal(" + ");
  735. colors.normal(obj.filteredModules);
  736. if(obj.modules.length > 0)
  737. colors.normal(" hidden");
  738. colors.normal(obj.filteredModules !== 1 ? " modules" : " module");
  739. newline();
  740. }
  741. }
  742. };
  743. if(obj.chunks) {
  744. obj.chunks.forEach(chunk => {
  745. colors.normal("chunk ");
  746. if(chunk.id < 1000) colors.normal(" ");
  747. if(chunk.id < 100) colors.normal(" ");
  748. if(chunk.id < 10) colors.normal(" ");
  749. colors.normal("{");
  750. colors.yellow(chunk.id);
  751. colors.normal("} ");
  752. colors.green(chunk.files.join(", "));
  753. if(chunk.names && chunk.names.length > 0) {
  754. colors.normal(" (");
  755. colors.normal(chunk.names.join(", "));
  756. colors.normal(")");
  757. }
  758. colors.normal(" ");
  759. colors.normal(SizeFormatHelpers.formatSize(chunk.size));
  760. chunk.parents.forEach(id => {
  761. colors.normal(" {");
  762. colors.yellow(id);
  763. colors.normal("}");
  764. });
  765. if(chunk.entry) {
  766. colors.yellow(" [entry]");
  767. } else if(chunk.initial) {
  768. colors.yellow(" [initial]");
  769. }
  770. if(chunk.rendered) {
  771. colors.green(" [rendered]");
  772. }
  773. if(chunk.recorded) {
  774. colors.green(" [recorded]");
  775. }
  776. newline();
  777. if(chunk.origins) {
  778. chunk.origins.forEach(origin => {
  779. colors.normal(" > ");
  780. if(origin.reasons && origin.reasons.length) {
  781. colors.yellow(origin.reasons.join(" "));
  782. colors.normal(" ");
  783. }
  784. if(origin.name) {
  785. colors.normal(origin.name);
  786. colors.normal(" ");
  787. }
  788. if(origin.module) {
  789. colors.normal("[");
  790. colors.normal(origin.moduleId);
  791. colors.normal("] ");
  792. const module = modulesByIdentifier[`$${origin.module}`];
  793. if(module) {
  794. colors.bold(module.name);
  795. colors.normal(" ");
  796. }
  797. if(origin.loc) {
  798. colors.normal(origin.loc);
  799. }
  800. }
  801. newline();
  802. });
  803. }
  804. processModulesList(chunk, " ");
  805. });
  806. }
  807. processModulesList(obj, "");
  808. if(obj._showWarnings && obj.warnings) {
  809. obj.warnings.forEach(warning => {
  810. newline();
  811. colors.yellow(`WARNING in ${warning}`);
  812. newline();
  813. });
  814. }
  815. if(obj._showErrors && obj.errors) {
  816. obj.errors.forEach(error => {
  817. newline();
  818. colors.red(`ERROR in ${error}`);
  819. newline();
  820. });
  821. }
  822. if(obj.children) {
  823. obj.children.forEach(child => {
  824. const childString = Stats.jsonToString(child, useColors);
  825. if(childString) {
  826. if(child.name) {
  827. colors.normal("Child ");
  828. colors.bold(child.name);
  829. colors.normal(":");
  830. } else {
  831. colors.normal("Child");
  832. }
  833. newline();
  834. buf.push(" ");
  835. buf.push(childString.replace(/\n/g, "\n "));
  836. newline();
  837. }
  838. });
  839. }
  840. if(obj.needAdditionalPass) {
  841. colors.yellow("Compilation needs an additional pass and will compile again.");
  842. }
  843. while(buf[buf.length - 1] === "\n") buf.pop();
  844. return buf.join("");
  845. }
  846. static presetToOptions(name) {
  847. // Accepted values: none, errors-only, minimal, normal, detailed, verbose
  848. // Any other falsy value will behave as 'none', truthy values as 'normal'
  849. const pn = (typeof name === "string") && name.toLowerCase() || name || "none";
  850. switch(pn) {
  851. case "none":
  852. return {
  853. all: false
  854. };
  855. case "verbose":
  856. return {
  857. entrypoints: true,
  858. modules: false,
  859. chunks: true,
  860. chunkModules: true,
  861. chunkOrigins: true,
  862. depth: true,
  863. env: true,
  864. reasons: true,
  865. usedExports: true,
  866. providedExports: true,
  867. optimizationBailout: true,
  868. errorDetails: true,
  869. publicPath: true,
  870. exclude: () => false,
  871. maxModules: Infinity,
  872. };
  873. case "detailed":
  874. return {
  875. entrypoints: true,
  876. chunks: true,
  877. chunkModules: false,
  878. chunkOrigins: true,
  879. depth: true,
  880. usedExports: true,
  881. providedExports: true,
  882. optimizationBailout: true,
  883. errorDetails: true,
  884. publicPath: true,
  885. exclude: () => false,
  886. maxModules: Infinity,
  887. };
  888. case "minimal":
  889. return {
  890. all: false,
  891. modules: true,
  892. maxModules: 0,
  893. errors: true,
  894. warnings: true,
  895. };
  896. case "errors-only":
  897. return {
  898. all: false,
  899. errors: true,
  900. moduleTrace: true,
  901. };
  902. default:
  903. return {};
  904. }
  905. }
  906. static getChildOptions(options, idx) {
  907. let innerOptions;
  908. if(Array.isArray(options.children)) {
  909. if(idx < options.children.length)
  910. innerOptions = options.children[idx];
  911. } else if(typeof options.children === "object" && options.children) {
  912. innerOptions = options.children;
  913. }
  914. if(typeof innerOptions === "boolean" || typeof innerOptions === "string")
  915. innerOptions = Stats.presetToOptions(innerOptions);
  916. if(!innerOptions)
  917. return options;
  918. const childOptions = Object.assign({}, options);
  919. delete childOptions.children; // do not inherit children
  920. return Object.assign(childOptions, innerOptions);
  921. }
  922. }
  923. module.exports = Stats;