Skip to content

Commit aa90166

Browse files
authored
Merge pull request webpack#3524 from webpack/bugfix/fix_stats_when_passed_array
fix(stats): allow stats to respect array of configs, also bump deps.
2 parents 951a760 + 784af50 commit aa90166

18 files changed

Lines changed: 146 additions & 41 deletions

File tree

‎bin/webpack.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,18 @@ function processOptions(options) {
171171
return;
172172
}
173173

174-
var firstOptions = Array.isArray(options) ? (options[0] || {}) : options;
175-
176-
if(typeof options.stats === "boolean" || typeof options.stats === "string") {
177-
var statsPresetToOptions = require("../lib/Stats.js").presetToOptions;
178-
options.stats = statsPresetToOptions(options.stats);
174+
var firstOptions = [].concat(options)[0];
175+
var statsPresetToOptions = require("../lib/Stats.js").presetToOptions;
176+
177+
var 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);
179185
}
180-
181-
var outputOptions = Object.create(options.stats || firstOptions.stats || {});
182186
if(typeof outputOptions.context === "undefined")
183187
outputOptions.context = firstOptions.context;
184188

‎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+
var 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+
var showVersion = typeof options.version === "undefined" ? jsons.every(j => j.version) : options.version !== false;
43+
var 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: 39 additions & 14 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) {
@@ -810,7 +814,8 @@ class Stats {
810814
errors: false,
811815
errorDetails: false,
812816
warnings: false,
813-
publicPath: false
817+
publicPath: false,
818+
performance: false
814819
};
815820
} else {
816821
return {
@@ -827,9 +832,29 @@ class Stats {
827832
depth: pn === "verbose",
828833
usedExports: pn === "verbose",
829834
providedExports: pn === "verbose",
830-
colors: true
835+
colors: true,
836+
performance: true
831837
};
832838
}
839+
840+
}
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;
833858
}
834859
}
835860

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
66
"dependencies": {
7-
"acorn": "^4.0.3",
7+
"acorn": "^4.0.4",
88
"acorn-dynamic-import": "^2.0.0",
99
"ajv": "^4.7.0",
1010
"ajv-keywords": "^1.1.1",
@@ -61,7 +61,7 @@
6161
"url-loader": "~0.5.0",
6262
"val-loader": "~0.5.0",
6363
"vm-browserify": "~0.0.0",
64-
"webpack-dev-middleware": "^1.0.0",
64+
"webpack-dev-middleware": "^1.9.0",
6565
"worker-loader": "~0.7.0"
6666
},
6767
"engines": {

‎test/MultiStats.test.js‎

Lines changed: 10 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,14 @@ describe("MultiStats", function() {
173175
})
174176
];
175177
myMultiStats = new MultiStats(stats);
176-
result = myMultiStats.toJson();
178+
result = myMultiStats.toJson({
179+
version: false,
180+
hash: false
181+
});
177182
});
178183

179184
it("returns plain object representation", function() {
180185
result.should.deepEqual({
181-
hash: "abc123xyz890",
182-
version: "1.2.3",
183186
errors: [
184187
"(abc123-compilation) abc123-error"
185188
],
@@ -235,9 +238,9 @@ describe("MultiStats", function() {
235238
"Hash: abc123xyz890\n" +
236239
"Version: webpack 1.2.3\n" +
237240
"Child abc123-compilation:\n" +
238-
" \n" +
241+
" Hash: abc123\n" +
239242
"Child xyz890-compilation:\n" +
240-
" "
243+
" Hash: xyz890"
241244
);
242245
});
243246
});

‎test/Stats.test.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ describe("Stats", function() {
5959
colors: false
6060
};
6161
var hasColorSetting = false;
62-
6362
if(typeof options.stats !== "undefined") {
6463
toStringOptions = options.stats;
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");
@@ -166,7 +168,8 @@ describe("Stats", function() {
166168
depth: false,
167169
usedExports: false,
168170
providedExports: false,
169-
colors: true
171+
colors: true,
172+
performance: true
170173
});
171174
});
172175
it("truthy values behave as 'normal'", function() {
@@ -197,7 +200,8 @@ describe("Stats", function() {
197200
errors: false,
198201
errorDetails: false,
199202
warnings: false,
200-
publicPath: false
203+
publicPath: false,
204+
performance: false
201205
});
202206
});
203207
it("falsy values behave as 'none'", function() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "foo";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "bar";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = function testAssertions(code, stdout, stderr) {
4+
code.should.be.eql(0);
5+
stdout.should.be.ok();
6+
stderr.should.be.empty();
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var path = require("path");
2+
3+
module.exports = [
4+
{
5+
entry: path.resolve(__dirname, "./index"),
6+
stats: "errors-only"
7+
},
8+
{
9+
entry: path.resolve(__dirname, "./index2"),
10+
stats: "errors-only"
11+
}
12+
];

0 commit comments

Comments
 (0)