Skip to content

Commit 7e58665

Browse files
author
Juan Lomán
committed
Add stats config presets.
`stats.toJson` and `stats.toString` now accept string/boolean values: `none` (or false), `errors-only`, `minimal`, `normal` (or true), `verbose`. A new static function was added: `Stats.presetToOptions(name)`, returns the options object from a preset name.
1 parent 7c5da65 commit 7e58665

43 files changed

Lines changed: 255 additions & 10 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/Stats.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ Stats.prototype.hasErrors = function() {
1919
};
2020

2121
Stats.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

274285
Stats.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+
};

test/Stats.test.js

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*globals describe it */
2-
require("should");
2+
var should = require("should");
33
var path = require("path");
44
var fs = require("fs");
55

@@ -44,11 +44,26 @@ describe("Stats", function() {
4444
c.apply(new webpack.optimize.OccurrenceOrderPlugin());
4545
c.run(function(err, stats) {
4646
if(err) return done(err);
47-
var actual = stats.toString({
48-
colors: false
49-
});
47+
48+
if(/error$/.test(testName)) {
49+
stats.compilation.errors.length.should.be.above(0);
50+
} else {
51+
stats.compilation.errors.length.should.equal(0);
52+
}
53+
54+
var toStringOptions = { colors: false };
55+
if(typeof options.stats !== "undefined") {
56+
toStringOptions = options.stats;
57+
}
58+
59+
var actual = stats.toString(toStringOptions);
5060
(typeof actual).should.be.eql("string");
51-
actual = actual.replace(/Version:.+\n/, "").replace(/[0-9]+(\s?ms)/g, "X$1").replace(/\r/g, "");
61+
actual =
62+
actual.replace(/\u001b\[[0-9;]*m/g, "")
63+
.replace(/Version:.+\n/, "")
64+
.replace(/[0-9]+(\s?ms)/g, "X$1")
65+
.replace(/\r/g, "")
66+
.replace(base, "Xdir");
5267
var expected = fs.readFileSync(path.join(base, testName, "expected.txt"), "utf-8").replace(/\r/g, "");
5368
if(actual !== expected) {
5469
fs.writeFileSync(path.join(base, testName, "actual.txt"), actual, "utf-8");
@@ -98,5 +113,55 @@ describe("Stats", function() {
98113
obj.errors[0].should.be.equal('firstError');
99114
});
100115
});
101-
116+
describe("Presets", function(){
117+
describe("presetToOptions", function() {
118+
it("returns correct object with 'Normal'", function() {
119+
Stats.presetToOptions("Normal").should.eql({
120+
assets: false,
121+
version: false,
122+
timings: true,
123+
hash: true,
124+
chunks: true,
125+
chunkModules: false,
126+
errorDetails: true,
127+
reasons: false,
128+
colors: true
129+
});
130+
});
131+
it("truthy values behave as 'normal'", function() {
132+
var normalOpts = Stats.presetToOptions('normal');
133+
Stats.presetToOptions("pizza").should.eql(normalOpts);
134+
Stats.presetToOptions(true).should.eql(normalOpts);
135+
Stats.presetToOptions(1).should.eql(normalOpts);
136+
137+
Stats.presetToOptions("verbose").should.not.eql(normalOpts);
138+
Stats.presetToOptions(false).should.not.eql(normalOpts);
139+
});
140+
it("returns correct object with 'none'", function() {
141+
Stats.presetToOptions("none").should.eql({
142+
hash: false,
143+
version: false,
144+
timings: false,
145+
assets: false,
146+
chunks: false,
147+
modules: false,
148+
reasons: false,
149+
children: false,
150+
source: false,
151+
errors: false,
152+
errorDetails: false,
153+
warnings: false,
154+
publicPath: false
155+
});
156+
});
157+
it("falsy values behave as 'none'", function() {
158+
var noneOpts = Stats.presetToOptions('none');
159+
Stats.presetToOptions("").should.eql(noneOpts);
160+
Stats.presetToOptions(null).should.eql(noneOpts);
161+
Stats.presetToOptions().should.eql(noneOpts);
162+
Stats.presetToOptions(0).should.eql(noneOpts);
163+
Stats.presetToOptions(false).should.eql(noneOpts);
164+
});
165+
});
166+
});
102167
});

test/statsCases/chunks/expected.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,44 @@ Time: Xms
66
2.bundle.js 101 bytes 2 [emitted]
77
3.bundle.js 180 bytes 3 [emitted]
88
chunk {0} bundle.js (main) 73 bytes [rendered]
9+
> main [0] (webpack)/test/statsCases/chunks/index.js
910
[0] (webpack)/test/statsCases/chunks/index.js 51 bytes {0} [built]
1011
factory:Xms building:Xms = Xms
1112
[1] (webpack)/test/statsCases/chunks/a.js 22 bytes {0} [built]
13+
cjs require ./a [0] (webpack)/test/statsCases/chunks/index.js 1:0-14
1214
[0] Xms -> factory:Xms building:Xms = Xms
1315
chunk {1} 1.bundle.js 54 bytes {0} [rendered]
16+
> [0] (webpack)/test/statsCases/chunks/index.js 3:0-16
1417
[3] (webpack)/test/statsCases/chunks/c.js 54 bytes {1} [built]
18+
amd require ./c [0] (webpack)/test/statsCases/chunks/index.js 3:0-16
1519
[0] Xms -> factory:Xms building:Xms = Xms
1620
chunk {2} 2.bundle.js 22 bytes {0} [rendered]
21+
> [0] (webpack)/test/statsCases/chunks/index.js 2:0-16
1722
[2] (webpack)/test/statsCases/chunks/b.js 22 bytes {2} [built]
23+
amd require ./b [0] (webpack)/test/statsCases/chunks/index.js 2:0-16
1824
[0] Xms -> factory:Xms building:Xms = Xms
1925
chunk {3} 3.bundle.js 44 bytes {1} [rendered]
26+
> [3] (webpack)/test/statsCases/chunks/c.js 1:0-52
2027
[4] (webpack)/test/statsCases/chunks/d.js 22 bytes {3} [built]
28+
require.ensure item ./d [3] (webpack)/test/statsCases/chunks/c.js 1:0-52
2129
[0] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
2230
[5] (webpack)/test/statsCases/chunks/e.js 22 bytes {3} [built]
31+
require.ensure item ./e [3] (webpack)/test/statsCases/chunks/c.js 1:0-52
32+
[0] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
33+
[0] (webpack)/test/statsCases/chunks/index.js 51 bytes {0} [built]
34+
factory:Xms building:Xms = Xms
35+
[1] (webpack)/test/statsCases/chunks/a.js 22 bytes {0} [built]
36+
cjs require ./a [0] (webpack)/test/statsCases/chunks/index.js 1:0-14
37+
[0] Xms -> factory:Xms building:Xms = Xms
38+
[2] (webpack)/test/statsCases/chunks/b.js 22 bytes {2} [built]
39+
amd require ./b [0] (webpack)/test/statsCases/chunks/index.js 2:0-16
40+
[0] Xms -> factory:Xms building:Xms = Xms
41+
[3] (webpack)/test/statsCases/chunks/c.js 54 bytes {1} [built]
42+
amd require ./c [0] (webpack)/test/statsCases/chunks/index.js 3:0-16
43+
[0] Xms -> factory:Xms building:Xms = Xms
44+
[4] (webpack)/test/statsCases/chunks/d.js 22 bytes {3} [built]
45+
require.ensure item ./d [3] (webpack)/test/statsCases/chunks/c.js 1:0-52
46+
[0] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
47+
[5] (webpack)/test/statsCases/chunks/e.js 22 bytes {3} [built]
48+
require.ensure item ./e [3] (webpack)/test/statsCases/chunks/c.js 1:0-52
2349
[0] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
ERROR in (webpack)/test/statsCases/preset-errors-only-error/index.js
3+
Module not found: Error: Cannot resolve module 'does-not-exist' in Xdir/preset-errors-only-error
4+
@ (webpack)/test/statsCases/preset-errors-only-error/index.js 1:0-25
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('does-not-exist')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
entry: "./index",
3+
stats: "errors-only"
4+
};

test/statsCases/preset-errors-only/expected.txt

Whitespace-only changes.

test/statsCases/preset-errors-only/index.js

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
entry: "./index",
3+
stats: "errors-only"
4+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
chunk {0} bundle.js (main) 0 bytes [rendered]

0 commit comments

Comments
 (0)