@@ -19,7 +19,11 @@ Stats.prototype.hasErrors = function() {
1919} ;
2020
2121Stats . prototype . toJson = function toJson ( options , forToString ) {
22- if ( ! options ) options = { } ;
22+ if ( typeof options === "boolean" || typeof options === "string" ) {
23+ options = Stats . presetToOptions ( options ) ;
24+ } else if ( ! options ) {
25+ options = { } ;
26+ }
2327
2428 function d ( v , def ) {
2529 return v === undefined ? def : v ;
@@ -39,7 +43,9 @@ Stats.prototype.toJson = function toJson(options, forToString) {
3943 var showReasons = d ( options . reasons , ! forToString ) ;
4044 var showChildren = d ( options . children , true ) ;
4145 var showSource = d ( options . source , ! forToString ) ;
46+ var showErrors = d ( options . errors , true ) ;
4247 var showErrorDetails = d ( options . errorDetails , ! forToString ) ;
48+ var showWarnings = d ( options . warnings , true ) ;
4349 var showPublicPath = d ( options . publicPath , ! forToString ) ;
4450 var excludeModules = [ ] . concat ( d ( options . exclude , [ ] ) ) . map ( function ( str ) {
4551 if ( typeof str !== "string" ) return str ;
@@ -112,11 +118,23 @@ Stats.prototype.toJson = function toJson(options, forToString) {
112118 }
113119 return text ;
114120 }
121+
115122 var obj = {
116123 errors : compilation . errors . map ( formatError ) ,
117124 warnings : compilation . warnings . map ( formatError )
118125 } ;
119126
127+ //We just hint other renderers since actually omitting
128+ //errors/warnings from the JSON would be kind of weird.
129+ Object . defineProperty ( obj , "_showWarnings" , {
130+ value : showWarnings ,
131+ enumerable : false
132+ } ) ;
133+ Object . defineProperty ( obj , "_showErrors" , {
134+ value : showErrors ,
135+ enumerable : false
136+ } ) ;
137+
120138 if ( showVersion ) {
121139 obj . version = require ( "../package.json" ) . version ;
122140 }
@@ -272,7 +290,9 @@ Stats.prototype.toJson = function toJson(options, forToString) {
272290} ;
273291
274292Stats . prototype . toString = function toString ( options ) {
275- if ( ! options ) options = { } ;
293+ if ( typeof options === "boolean" || typeof options === "string" ) {
294+ options = Stats . presetToOptions ( options ) ;
295+ } else if ( ! options ) options = { } ;
276296
277297 function d ( v , def ) {
278298 return v === undefined ? def : v ;
@@ -639,14 +659,14 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
639659 newline ( ) ;
640660 }
641661 }
642- if ( obj . warnings ) {
662+ if ( obj . _showWarnings && obj . warnings ) {
643663 obj . warnings . forEach ( function ( warning ) {
644664 newline ( ) ;
645665 yellow ( "WARNING in " + warning ) ;
646666 newline ( ) ;
647667 } ) ;
648668 }
649- if ( obj . errors ) {
669+ if ( obj . _showErrors && obj . errors ) {
650670 obj . errors . forEach ( function ( error ) {
651671 newline ( ) ;
652672 red ( "ERROR in " + error ) ;
@@ -672,3 +692,39 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
672692 while ( buf [ buf . length - 1 ] === "\n" ) buf . pop ( ) ;
673693 return buf . join ( "" ) ;
674694} ;
695+
696+ Stats . presetToOptions = function ( name ) {
697+ //Accepted values: none, errors-only, minimal, normal, verbose
698+ //Any other falsy value will behave as 'none', truthy values as 'normal'
699+ var pn = ( typeof name === "string" ) && name . toLowerCase ( ) || name ;
700+ if ( pn === "none" || ! pn ) {
701+ return {
702+ hash : false ,
703+ version : false ,
704+ timings : false ,
705+ assets : false ,
706+ chunks : false ,
707+ modules : false ,
708+ reasons : false ,
709+ children : false ,
710+ source : false ,
711+ errors : false ,
712+ errorDetails : false ,
713+ warnings : false ,
714+ publicPath : false
715+ } ;
716+ } else {
717+ return {
718+ assets : pn === "verbose" ,
719+ version : pn === "verbose" ,
720+ timings : pn !== "errors-only" && pn !== "minimal" ,
721+ hash : pn !== "errors-only" && pn !== "minimal" ,
722+ chunks : pn !== "errors-only" ,
723+ chunkModules : pn === "verbose" ,
724+ //warnings: pn !== "errors-only",
725+ errorDetails : pn !== "errors-only" && pn !== "minimal" ,
726+ reasons : pn === "verbose" ,
727+ colors : true
728+ } ;
729+ }
730+ } ;
0 commit comments