Skip to content

Commit f1d56dc

Browse files
committed
add support for multiple different stats objects
1 parent 19c7260 commit f1d56dc

9 files changed

Lines changed: 101 additions & 48 deletions

File tree

bin/webpack.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,6 @@ function ifArg(name, fn, init) {
161161
}
162162
}
163163

164-
function hasPresetPredicate(options) {
165-
return typeof options.stats === "boolean" || typeof options.stats === "string";
166-
}
167-
168-
function hasPreset(optionsArray) {
169-
options = Array.isArray(optionsArray) ? optionsArray : [optionsArray];
170-
return options.some(hasPresetPredicate);
171-
}
172-
173164
function processOptions(options) {
174165
// process Promise
175166
if(typeof options.then === "function") {
@@ -180,20 +171,18 @@ function processOptions(options) {
180171
return;
181172
}
182173

183-
var firstOptions = Array.isArray(options) ? (options[0] || {}) : options;
184-
if(hasPreset(options)) {
185-
var statsPresetToOptions = require("../lib/Stats.js").presetToOptions;
186-
187-
if(Array.isArray(options)) {
188-
options.forEach(function(option) {
189-
option.stats = statsPresetToOptions(option.stats);
190-
});
191-
} else {
192-
options.stats = statsPresetToOptions(options.stats);
193-
}
174+
var firstOptions = [].concat(options)[0];
175+
var statsPresetToOptions = require("../lib/Stats.js").presetToOptions;
176+
177+
let outputOptions = options.stats;
178+
if(typeof outputOptions === "boolean" || typeof outputOptions === "string")
179+
outputOptions = statsPresetToOptions(outputOptions);
180+
else
181+
outputOptions = {};
182+
outputOptions = Object.create(outputOptions);
183+
if(Array.isArray(options) && !outputOptions.children) {
184+
outputOptions.children = options.map(o => o.stats);
194185
}
195-
196-
var outputOptions = Object.create(options.stats || firstOptions.stats || {});
197186
if(typeof outputOptions.context === "undefined")
198187
outputOptions.context = firstOptions.context;
199188

lib/MultiStats.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,23 @@ MultiStats.prototype.hasWarnings = function() {
2828
};
2929

3030
MultiStats.prototype.toJson = function(options, forToString) {
31-
var jsons = this.stats.map(function(stat) {
32-
var obj = stat.toJson(options, forToString);
31+
if(typeof options === "boolean" || typeof options === "string") {
32+
options = Stats.presetToOptions(options);
33+
} else if(!options) {
34+
options = {};
35+
}
36+
var jsons = this.stats.map((stat, idx) => {
37+
const childOptions = Stats.getChildOptions(options, idx)
38+
var obj = stat.toJson(childOptions, forToString);
3339
obj.name = stat.compilation && stat.compilation.name;
3440
return obj;
3541
});
42+
const showVersion = typeof options.version === "undefined" ? jsons.every(j => j.version) : options.version !== false;
43+
const showHash = typeof options.hash === "undefined" ? jsons.every(j => j.hash) : options.hash !== false;
44+
jsons.forEach(j => {
45+
if(showVersion)
46+
delete j.version;
47+
});
3648
var obj = {
3749
errors: jsons.reduce(function(arr, j) {
3850
return arr.concat(j.errors.map(function(msg) {
@@ -45,11 +57,11 @@ MultiStats.prototype.toJson = function(options, forToString) {
4557
}));
4658
}, [])
4759
};
48-
if(!options || options.version !== false)
60+
if(showVersion)
4961
obj.version = require("../package.json").version;
50-
if(!options || options.hash !== false)
62+
if(showHash)
5163
obj.hash = this.hash;
52-
if(!options || options.children !== false)
64+
if(options.children !== false)
5365
obj.children = jsons;
5466
return obj;
5567
};

lib/Stats.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,9 @@ class Stats {
330330
obj.modules.sort(sortByField(sortModules));
331331
}
332332
if(showChildren) {
333-
obj.children = compilation.children.map(child => {
334-
const obj = new Stats(child).toJson(options, forToString);
333+
obj.children = compilation.children.map((child, idx) => {
334+
const childOptions = Stats.getChildOptions(options, idx);
335+
const obj = new Stats(child).toJson(childOptions, forToString);
335336
delete obj.hash;
336337
delete obj.version;
337338
obj.name = child.name;
@@ -766,17 +767,20 @@ class Stats {
766767
}
767768
if(obj.children) {
768769
obj.children.forEach(child => {
769-
if(child.name) {
770-
colors.normal("Child ");
771-
colors.bold(child.name);
772-
colors.normal(":");
773-
} else {
774-
colors.normal("Child");
770+
let childString = Stats.jsonToString(child, useColors);
771+
if(childString) {
772+
if(child.name) {
773+
colors.normal("Child ");
774+
colors.bold(child.name);
775+
colors.normal(":");
776+
} else {
777+
colors.normal("Child");
778+
}
779+
newline();
780+
buf.push(" ");
781+
buf.push(childString.replace(/\n/g, "\n "));
782+
newline();
775783
}
776-
newline();
777-
buf.push(" ");
778-
buf.push(Stats.jsonToString(child, useColors).replace(/\n/g, "\n "));
779-
newline();
780784
});
781785
}
782786
if(obj.needAdditionalPass) {
@@ -834,6 +838,24 @@ class Stats {
834838
}
835839

836840
}
841+
842+
static getChildOptions(options, idx) {
843+
let innerOptions;
844+
if(Array.isArray(options.children)) {
845+
if(idx < options.children.length)
846+
innerOptions = options.children[idx];
847+
} else if(typeof options.children === "object" && options.children) {
848+
innerOptions = options.children;
849+
}
850+
if(typeof innerOptions === "boolean" || typeof innerOptions === "string")
851+
innerOptions = Stats.presetToOptions(innerOptions);
852+
if(!innerOptions)
853+
return options;
854+
let childOptions = Object.assign({}, options);
855+
delete childOptions.children; // do not inherit children
856+
childOptions = Object.assign(childOptions, innerOptions);
857+
return childOptions;
858+
}
837859
}
838860

839861
module.exports = Stats;

test/MultiStats.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ var createStat = function(overides) {
1111
},
1212
hasErrors: () => false,
1313
hasWarnings: () => false,
14-
toJson: () => ({
14+
toJson: () => Object.assign({
15+
hash: "foo",
16+
version: "version",
1517
warnings: [],
1618
errors: []
17-
})
19+
}, overides)
1820
}, overides);
1921
};
2022

@@ -173,13 +175,11 @@ describe("MultiStats", function() {
173175
})
174176
];
175177
myMultiStats = new MultiStats(stats);
176-
result = myMultiStats.toJson();
178+
result = myMultiStats.toJson({ version: false, hash: false });
177179
});
178180

179181
it("returns plain object representation", function() {
180182
result.should.deepEqual({
181-
hash: "abc123xyz890",
182-
version: "1.2.3",
183183
errors: [
184184
"(abc123-compilation) abc123-error"
185185
],
@@ -235,9 +235,9 @@ describe("MultiStats", function() {
235235
"Hash: abc123xyz890\n" +
236236
"Version: webpack 1.2.3\n" +
237237
"Child abc123-compilation:\n" +
238-
" \n" +
238+
" Hash: abc123\n" +
239239
"Child xyz890-compilation:\n" +
240-
" "
240+
" Hash: xyz890"
241241
);
242242
});
243243
});

test/Stats.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ describe("Stats", function() {
4747
c.apply(new webpack.optimize.OccurrenceOrderPlugin());
4848
});
4949
c.run(function(err, stats) {
50-
options = Array.isArray(options) ? options[0] : options;
5150
if(err) return done(err);
5251

5352
if(/error$/.test(testName)) {
@@ -65,6 +64,9 @@ describe("Stats", function() {
6564

6665
hasColorSetting = typeof options.stats.colors !== "undefined";
6766
}
67+
if(Array.isArray(options) && !toStringOptions.children) {
68+
toStringOptions.children = options.map(o => o.stats);
69+
}
6870

6971
var actual = stats.toString(toStringOptions);
7072
(typeof actual).should.be.eql("string");

test/binCases/stats/single-config/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
module.exports = function testAssertions(code, stdout, stderr) {
4-
code.should.be.oneOf(0, 1);
4+
code.should.be.eql(0);
55

66
stdout.should.be.ok();
77
stdout[0].should.containEql("Hash: ");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Child minimal:
2+
chunk {0} main.js (main) 8 bytes [entry] [rendered]
3+
Child verbose:
4+
Entrypoint main = main.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// huh?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = [
2+
{
3+
name: "minimal",
4+
entry: "./index",
5+
stats: "minimal"
6+
},
7+
{
8+
name: "none",
9+
entry: "./index",
10+
stats: false
11+
},
12+
{
13+
name: "verbose",
14+
entry: "./index",
15+
stats: {
16+
entrypoints: true,
17+
hash: false,
18+
timings: false,
19+
chunks: false,
20+
assets: false
21+
}
22+
}
23+
]

0 commit comments

Comments
 (0)