Skip to content

Commit a148d6e

Browse files
authored
Merge pull request webpack#3350 from webpack/feature/custom_stats
feat(performance): webpack performance budgets
2 parents 7de320b + 3dfab10 commit a148d6e

39 files changed

Lines changed: 444 additions & 26 deletions

‎lib/Compilation.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function Compilation(compiler) {
3131
this.outputOptions = options && options.output;
3232
this.bail = options && options.bail;
3333
this.profile = options && options.profile;
34+
this.performance = options && options.performance;
3435

3536
this.mainTemplate = new MainTemplate(this.outputOptions);
3637
this.chunkTemplate = new ChunkTemplate(this.outputOptions, this.mainTemplate);

‎lib/Entrypoint.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,29 @@ Entrypoint.prototype.insertChunk = function(chunk, before) {
2222
}
2323
chunk.entrypoints.push(this);
2424
};
25+
26+
Entrypoint.prototype.getFiles = function() {
27+
var files = [];
28+
29+
for(var chunkIdx = 0; chunkIdx < this.chunks.length; chunkIdx++) {
30+
for(var fileIdx = 0; fileIdx < this.chunks[chunkIdx].files.length; fileIdx++) {
31+
if(files.indexOf(this.chunks[chunkIdx].files[fileIdx]) === -1) {
32+
files.push(this.chunks[chunkIdx].files[fileIdx]);
33+
}
34+
}
35+
}
36+
37+
return files;
38+
}
39+
40+
Entrypoint.prototype.getSize = function(compilation) {
41+
var files = this.getFiles();
42+
43+
return files
44+
.map(function(file) {
45+
return compilation.assets[file].size()
46+
})
47+
.reduce(function(currentSize, nextSize) {
48+
return currentSize + nextSize
49+
}, 0);
50+
}

‎lib/SizeFormatHelpers.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Sean Larkin @thelarkinn
4+
*/
5+
var SizeFormatHelpers = exports;
6+
7+
SizeFormatHelpers.formatSize = function(size) {
8+
if(size <= 0) {
9+
return "0 bytes";
10+
}
11+
12+
var abbreviations = ["bytes", "kB", "MB", "GB"];
13+
var index = Math.floor(Math.log(size) / Math.log(1000));
14+
15+
return +(size / Math.pow(1000, index))
16+
.toPrecision(3) + " " + abbreviations[index];
17+
}

‎lib/Stats.js‎

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
var RequestShortener = require("./RequestShortener");
6+
var formatSize = require("./SizeFormatHelpers").formatSize;
67

78
function Stats(compilation) {
89
this.compilation = compilation;
@@ -30,6 +31,7 @@ Stats.prototype.toJson = function toJson(options, forToString) {
3031
}
3132
var compilation = this.compilation;
3233
var requestShortener = new RequestShortener(d(options.context, process.cwd()));
34+
var showPerformance = d(options.performance, true);
3335
var showHash = d(options.hash, true);
3436
var showVersion = d(options.version, true);
3537
var showTimings = d(options.timings, true);
@@ -170,6 +172,11 @@ Stats.prototype.toJson = function toJson(options, forToString) {
170172
chunkNames: [],
171173
emitted: compilation.assets[asset].emitted
172174
};
175+
176+
if(showPerformance) {
177+
obj.isOverSizeLimit = compilation.assets[asset].isOverSizeLimit;
178+
}
179+
173180
assetsByFile[asset] = obj;
174181
return obj;
175182
}).filter(function(asset) {
@@ -323,6 +330,7 @@ Stats.prototype.toJson = function toJson(options, forToString) {
323330
return obj;
324331
});
325332
}
333+
326334
return obj;
327335
};
328336

@@ -394,7 +402,11 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
394402
buf.push("\n");
395403
}
396404

397-
function table(array, formats, align, splitter) {
405+
function getText(arr, row, col) {
406+
return arr[row][col].value;
407+
}
408+
409+
function table(array, align, splitter) {
398410
var row;
399411
var rows = array.length;
400412
var col;
@@ -405,38 +417,39 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
405417
colSizes[col] = 3;
406418
for(row = 0; row < rows; row++) {
407419
for(col = 0; col < cols; col++) {
408-
value = array[row][col] + "";
420+
value = getText(array, row, col) + "";
421+
if(value.length === 0) {
422+
colSizes[col] = 0;
423+
}
409424
if(value.length > colSizes[col]) {
410425
colSizes[col] = value.length;
411426
}
412427
}
413428
}
414429
for(row = 0; row < rows; row++) {
415430
for(col = 0; col < cols; col++) {
416-
var format = row === 0 ? colors.bold : formats[col];
417-
value = array[row][col] + "";
431+
var format = array[row][col].color;
432+
value = getText(array, row, col) + "";
418433
var l = value.length;
419434
if(align[col] === "l")
420435
format(value);
421436
for(; l < colSizes[col] && col !== cols - 1; l++)
422437
colors.normal(" ");
423438
if(align[col] === "r")
424439
format(value);
425-
if(col + 1 < cols)
440+
if(col + 1 < cols && colSizes[col] != 0)
426441
colors.normal(splitter || " ");
427442
}
428443
newline();
429444
}
430445
}
431446

432-
function formatSize(size) {
433-
if(size <= 0) return "0 bytes";
434-
435-
var abbreviations = ["bytes", "kB", "MB", "GB"];
436-
var index = Math.floor(Math.log(size) / Math.log(1000));
447+
function getAssetColor(asset, defaultColor) {
448+
if(asset.isOverSizeLimit) {
449+
return colors.yellow;
450+
}
437451

438-
return +(size / Math.pow(1000, index))
439-
.toPrecision(3) + " " + abbreviations[index];
452+
return defaultColor;
440453
}
441454

442455
if(obj.hash) {
@@ -460,20 +473,51 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
460473
colors.bold(obj.publicPath);
461474
newline();
462475
}
476+
463477
if(obj.assets && obj.assets.length > 0) {
464478
var t = [
465-
["Asset", "Size", "Chunks", "", "Chunk Names"]
479+
[{
480+
value: "Asset",
481+
color: colors.bold
482+
}, {
483+
value: "Size",
484+
color: colors.bold
485+
}, {
486+
value: "Chunks",
487+
color: colors.bold
488+
}, {
489+
value: "",
490+
color: colors.bold
491+
}, {
492+
value: "",
493+
color: colors.bold
494+
}, {
495+
value: "Chunk Names",
496+
color: colors.bold
497+
}]
466498
];
467499
obj.assets.forEach(function(asset) {
468-
t.push([
469-
asset.name,
470-
formatSize(asset.size),
471-
asset.chunks.join(", "),
472-
asset.emitted ? "[emitted]" : "",
473-
asset.chunkNames.join(", ")
474-
]);
500+
t.push([{
501+
value: asset.name,
502+
color: getAssetColor(asset, colors.green)
503+
}, {
504+
value: formatSize(asset.size),
505+
color: getAssetColor(asset, colors.normal)
506+
}, {
507+
value: asset.chunks.join(", "),
508+
color: colors.bold
509+
}, {
510+
value: asset.emitted ? "[emitted]" : "",
511+
color: colors.green
512+
}, {
513+
value: asset.isOverSizeLimit ? "[big]" : "",
514+
color: getAssetColor(asset, colors.normal)
515+
}, {
516+
value: asset.chunkNames.join(", "),
517+
color: colors.normal
518+
}]);
475519
});
476-
table(t, [colors.green, colors.normal, colors.bold, colors.green, colors.normal], "rrrll");
520+
table(t, "rrrlll");
477521
}
478522
if(obj.entrypoints) {
479523
Object.keys(obj.entrypoints).forEach(function(name) {

‎lib/WebpackOptionsApply.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
4242
var OccurrenceOrderPlugin = require("./optimize/OccurrenceOrderPlugin");
4343
var FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
4444
var FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
45+
var EmittedAssetSizeLimitPlugin = require("./performance/EmittedAssetSizeLimitPlugin");
4546

4647
var ResolverFactory = require("enhanced-resolve").ResolverFactory;
4748

@@ -69,6 +70,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
6970
var NodeSourcePlugin;
7071
var NodeTargetPlugin;
7172
var NodeTemplatePlugin;
73+
7274
switch(options.target) {
7375
case "web":
7476
JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
@@ -186,6 +188,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
186188
} else {
187189
throw new Error("Unsupported target '" + options.target + "'.");
188190
}
191+
189192
if(options.output.library || options.output.libraryTarget !== "var") {
190193
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
191194
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine, options.output.auxiliaryComment || ""));
@@ -264,6 +267,8 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
264267
new FlagDependencyUsagePlugin()
265268
);
266269

270+
compiler.apply(new EmittedAssetSizeLimitPlugin(options.performance));
271+
267272
compiler.apply(new TemplatedPathPlugin());
268273

269274
compiler.apply(new RecordIdsPlugin());

‎lib/WebpackOptionsDefaulter.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ function WebpackOptionsDefaulter() {
6969
this.set("node.__filename", "mock");
7070
this.set("node.__dirname", "mock");
7171

72+
this.set("performance.maxAssetSize", 250000);
73+
this.set("performance.maxInitialChunkSize", 250000);
74+
this.set("performance.errorOnHint", false);
75+
this.set("performance.hints", "make", function(options) {
76+
if(options.target === "web")
77+
return true;
78+
else
79+
return false;
80+
});
81+
7282
this.set("resolve", {});
7383
this.set("resolve.unsafeCache", true);
7484
this.set("resolve.modules", ["node_modules"]);
@@ -85,7 +95,6 @@ function WebpackOptionsDefaulter() {
8595
else
8696
return ["module", "main"];
8797
});
88-
8998
this.set("resolveLoader", {});
9099
this.set("resolveLoader.unsafeCache", true);
91100
this.set("resolveLoader.mainFields", ["loader", "main"]);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Sean Larkin @thelarkinn
4+
*/
5+
var SizeFormatHelpers = require('../SizeFormatHelpers');
6+
7+
function AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetLimit) {
8+
Error.call(this);
9+
Error.captureStackTrace(this, AssetsOverSizeLimitWarning);
10+
this.name = "AssetsOverSizeLimitWarning";
11+
this.assets = assetsOverSizeLimit;
12+
13+
var assetLists = this.assets.map(function(asset) {
14+
return "\n " + asset.name + " (" + SizeFormatHelpers.formatSize(asset.size) + ")";
15+
}).join("");
16+
17+
this.message = "asset size limit: The following asset(s) exceed the recommended size limit (" + SizeFormatHelpers.formatSize(assetLimit) + "). \n" +
18+
"This can impact web performance.\n" +
19+
"Assets: " + assetLists;
20+
}
21+
module.exports = AssetsOverSizeLimitWarning;
22+
23+
AssetsOverSizeLimitWarning.prototype = Object.create(Error.prototype);
24+
AssetsOverSizeLimitWarning.prototype.constructor = AssetsOverSizeLimitWarning;

0 commit comments

Comments
 (0)