@@ -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 ;
@@ -117,6 +123,11 @@ Stats.prototype.toJson = function toJson(options, forToString) {
117123 warnings : compilation . warnings . map ( formatError )
118124 } ;
119125
126+ //We just hint other renderers since actually omitting errors/warnings
127+ //from the JSON would be kind of weird.
128+ Object . defineProperty ( obj , "_showWarnings" , { value : showWarnings , enumerable : false } ) ;
129+ Object . defineProperty ( obj , "_showErrors" , { value : showErrors , enumerable : false } ) ;
130+
120131 if ( showVersion ) {
121132 obj . version = require ( "../package.json" ) . version ;
122133 }
@@ -272,7 +283,9 @@ Stats.prototype.toJson = function toJson(options, forToString) {
272283} ;
273284
274285Stats . prototype . toString = function toString ( options ) {
275- if ( ! options ) options = { } ;
286+ if ( typeof options === "boolean" || typeof options === "string" ) {
287+ options = Stats . presetToOptions ( options ) ;
288+ } else if ( ! options ) options = { } ;
276289
277290 function d ( v , def ) {
278291 return v === undefined ? def : v ;
@@ -639,14 +652,14 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
639652 newline ( ) ;
640653 }
641654 }
642- if ( obj . warnings ) {
655+ if ( obj . _showWarnings && obj . warnings ) {
643656 obj . warnings . forEach ( function ( warning ) {
644657 newline ( ) ;
645658 yellow ( "WARNING in " + warning ) ;
646659 newline ( ) ;
647660 } ) ;
648661 }
649- if ( obj . errors ) {
662+ if ( obj . _showErrors && obj . errors ) {
650663 obj . errors . forEach ( function ( error ) {
651664 newline ( ) ;
652665 red ( "ERROR in " + error ) ;
@@ -672,3 +685,39 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
672685 while ( buf [ buf . length - 1 ] === "\n" ) buf . pop ( ) ;
673686 return buf . join ( "" ) ;
674687} ;
688+
689+ Stats . presetToOptions = function ( name ) {
690+ //Accepted values: none, errors-only, minimal, normal, verbose
691+ //Any other falsy value will behave as 'none', truthy values as 'normal'
692+ var pn = ( typeof name === "string" ) && name . toLowerCase ( ) || name ;
693+ if ( pn === "none" || ! pn ) {
694+ return {
695+ hash : false ,
696+ version : false ,
697+ timings : false ,
698+ assets : false ,
699+ chunks : false ,
700+ modules : false ,
701+ reasons : false ,
702+ children : false ,
703+ source : false ,
704+ errors : false ,
705+ errorDetails : false ,
706+ warnings : false ,
707+ publicPath : false
708+ } ;
709+ } else {
710+ return {
711+ assets : pn === "verbose" ,
712+ version : pn === "verbose" ,
713+ timings : pn !== "errors-only" && pn !== "minimal" ,
714+ hash : pn !== "errors-only" && pn !== "minimal" ,
715+ chunks : pn !== "errors-only" ,
716+ chunkModules : pn === "verbose" ,
717+ //warnings: pn !== "errors-only",
718+ errorDetails : pn !== "errors-only" && pn !== "minimal" ,
719+ reasons : pn === "verbose" ,
720+ colors : true
721+ } ;
722+ }
723+ } ;
0 commit comments