Stats.js 26 KB

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