Skip to content

Commit 324d309

Browse files
authored
Merge pull request webpack#3471 from webpack/feature/perf_budgets_filter_sourcemap
feat(performance): don't include .map files in perf warnings
2 parents fdc781e + a68439d commit 324d309

24 files changed

Lines changed: 252 additions & 160 deletions

‎lib/Entrypoint.js‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,3 @@ Entrypoint.prototype.getFiles = function() {
3636

3737
return files;
3838
}
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/Stats.js‎

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

88
function Stats(compilation) {
99
this.compilation = compilation;
@@ -212,6 +212,9 @@ Stats.prototype.toJson = function toJson(options, forToString) {
212212
assets: ep.chunks.reduce(function(array, c) {
213213
return array.concat(c.files || []);
214214
}, [])
215+
};
216+
if(showPerformance) {
217+
obj.entrypoints[name].isOverSizeLimit = ep.isOverSizeLimit;
215218
}
216219
});
217220
}
@@ -501,7 +504,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
501504
value: asset.name,
502505
color: getAssetColor(asset, colors.green)
503506
}, {
504-
value: formatSize(asset.size),
507+
value: SizeFormatHelpers.formatSize(asset.size),
505508
color: getAssetColor(asset, colors.normal)
506509
}, {
507510
value: asset.chunks.join(", "),
@@ -521,10 +524,15 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
521524
}
522525
if(obj.entrypoints) {
523526
Object.keys(obj.entrypoints).forEach(function(name) {
527+
var ep = obj.entrypoints[name];
524528
colors.normal("Entrypoint ");
525529
colors.bold(name);
530+
if(ep.isOverSizeLimit) {
531+
colors.normal(" ");
532+
colors.yellow("[big]");
533+
}
526534
colors.normal(" =");
527-
obj.entrypoints[name].assets.forEach(function(asset) {
535+
ep.assets.forEach(function(asset) {
528536
colors.normal(" ");
529537
colors.green(asset);
530538
});
@@ -548,7 +556,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
548556

549557
function processModuleAttributes(module) {
550558
colors.normal(" ");
551-
colors.normal(formatSize(module.size));
559+
colors.normal(SizeFormatHelpers.formatSize(module.size));
552560
if(module.chunks) {
553561
module.chunks.forEach(function(chunk) {
554562
colors.normal(" {");
@@ -658,7 +666,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
658666
colors.normal(")");
659667
}
660668
colors.normal(" ");
661-
colors.normal(formatSize(chunk.size));
669+
colors.normal(SizeFormatHelpers.formatSize(chunk.size));
662670
chunk.parents.forEach(function(id) {
663671
colors.normal(" {");
664672
colors.yellow(id);

‎lib/WebpackOptionsApply.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +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");
45+
var SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
4646

4747
var ResolverFactory = require("enhanced-resolve").ResolverFactory;
4848

@@ -267,7 +267,9 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
267267
new FlagDependencyUsagePlugin()
268268
);
269269

270-
compiler.apply(new EmittedAssetSizeLimitPlugin(options.performance));
270+
if(options.performance) {
271+
compiler.apply(new SizeLimitsPlugin(options.performance));
272+
}
271273

272274
compiler.apply(new TemplatedPathPlugin());
273275

‎lib/WebpackOptionsDefaulter.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ function WebpackOptionsDefaulter() {
7070
this.set("node.__dirname", "mock");
7171

7272
this.set("performance.maxAssetSize", 250000);
73-
this.set("performance.maxInitialChunkSize", 250000);
73+
this.set("performance.maxEntrypointSize", 250000);
7474
this.set("performance.errorOnHint", false);
7575
this.set("performance.hints", "make", function(options) {
76-
if(options.target === "web")
77-
return true;
76+
if(options.target === "web" || options.target === "webworker")
77+
return "warning";
7878
else
7979
return false;
8080
});

‎lib/performance/AssetsOverSizeLimitWarning.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Sean Larkin @thelarkinn
44
*/
5-
var SizeFormatHelpers = require('../SizeFormatHelpers');
5+
var SizeFormatHelpers = require("../SizeFormatHelpers");
66

77
function AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetLimit) {
88
Error.call(this);

‎lib/performance/EmittedAssetSizeLimitPlugin.js‎

Lines changed: 0 additions & 101 deletions
This file was deleted.

‎lib/performance/EntrypointsOverSizeLimitWarning.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Sean Larkin @thelarkinn
44
*/
5-
var SizeFormatHelpers = require('../SizeFormatHelpers');
5+
var SizeFormatHelpers = require("../SizeFormatHelpers");
66

7-
function EntrypointsOverSizeLimitWarning(entrypoints, compilation, entrypointLimit) {
7+
function EntrypointsOverSizeLimitWarning(entrypoints, entrypointLimit) {
88
Error.call(this);
99
Error.captureStackTrace(this, EntrypointsOverSizeLimitWarning);
1010
this.name = "EntrypointsOverSizeLimitWarning";
1111
this.entrypoints = entrypoints;
1212

13-
var entrypointCompilation = compilation;
1413
var entrypointList = this.entrypoints.map(function(entrypoint) {
15-
return "\n " + entrypoint.name + " (" + SizeFormatHelpers.formatSize(entrypoint.getSize(entrypointCompilation)) + ")\n" +
16-
entrypoint.getFiles().map(function(filename, index) {
17-
return " " + entrypoint.getFiles()[index] + "\n";
14+
return "\n " + entrypoint.name + " (" + SizeFormatHelpers.formatSize(entrypoint.size) + ")\n" +
15+
entrypoint.files
16+
.map(function(asset) {
17+
return " " + asset + "\n";
1818
}).join("");
1919
}).join("");
2020

‎lib/performance/NoAsyncChunksWarning.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function NoAsyncChunksWarning() {
88
this.name = "NoAsyncChunksWarning";
99

1010
this.message = "webpack performance recommendations: \n" +
11-
"You can limit the size of your bundles by using System.import() or require.ensure to lazy load some parts of your application.\n" +
11+
"You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.\n" +
1212
"For more info visit https://webpack.js.org/guides/code-splitting/";
1313
}
1414
module.exports = NoAsyncChunksWarning;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Sean Larkin @thelarkinn
4+
*/
5+
var path = require("path");
6+
7+
var EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
8+
var AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
9+
var NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
10+
11+
function SizeLimitsPlugin(options) {
12+
this.hints = options.hints;
13+
this.maxAssetSize = options.maxAssetSize;
14+
this.maxEntrypointSize = options.maxEntrypointSize;
15+
this.assetFilter = options.assetFilter;
16+
}
17+
18+
module.exports = SizeLimitsPlugin;
19+
20+
SizeLimitsPlugin.prototype.apply = function(compiler) {
21+
var entrypointSizeLimit = this.maxEntrypointSize;
22+
var assetSizeLimit = this.maxAssetSize;
23+
var hints = this.hints;
24+
var assetFilter = this.assetFilter || function(asset) {
25+
return !(/\.map$/.test(asset))
26+
};
27+
28+
compiler.plugin("after-emit", function(compilation, callback) {
29+
var warnings = [];
30+
31+
var getEntrypointSize = function(entrypoint) {
32+
var files = entrypoint.getFiles();
33+
34+
return files
35+
.filter(assetFilter)
36+
.map(function(file) {
37+
return compilation.assets[file].size()
38+
})
39+
.reduce(function(currentSize, nextSize) {
40+
return currentSize + nextSize
41+
}, 0);
42+
};
43+
44+
var assetsOverSizeLimit = [];
45+
Object.keys(compilation.assets)
46+
.filter(assetFilter)
47+
.forEach(function(assetName) {
48+
var asset = compilation.assets[assetName];
49+
var size = asset.size();
50+
51+
if(size > assetSizeLimit) {
52+
assetsOverSizeLimit.push({
53+
name: assetName,
54+
size: size
55+
});
56+
asset.isOverSizeLimit = true;
57+
}
58+
});
59+
60+
var entrypointsOverLimit = [];
61+
Object.keys(compilation.entrypoints)
62+
.forEach(function(key) {
63+
var entry = compilation.entrypoints[key];
64+
var size = getEntrypointSize(entry, compilation);
65+
66+
if(size > entrypointSizeLimit) {
67+
entrypointsOverLimit.push({
68+
name: key,
69+
size: size,
70+
files: entry.getFiles().filter(assetFilter)
71+
});
72+
entry.isOverSizeLimit = true;
73+
}
74+
});
75+
76+
if(hints) {
77+
// 1. Individual Chunk: Size < 250kb
78+
// 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
79+
// 3. No Async Chunks
80+
// if !1, then 2, if !2 return
81+
if(assetsOverSizeLimit.length > 0) {
82+
warnings.push(
83+
new AssetsOverSizeLimitWarning(
84+
assetsOverSizeLimit,
85+
assetSizeLimit
86+
)
87+
);
88+
}
89+
if(entrypointsOverLimit.length > 0) {
90+
warnings.push(
91+
new EntrypointsOverSizeLimitWarning(
92+
entrypointsOverLimit,
93+
entrypointSizeLimit
94+
)
95+
);
96+
}
97+
98+
if(warnings.length > 0) {
99+
var hasAsyncChunks = compilation.chunks.filter(function(chunk) {
100+
return !chunk.isInitial();
101+
}).length > 0;
102+
103+
if(!hasAsyncChunks) {
104+
warnings.push(new NoAsyncChunksWarning());
105+
}
106+
107+
if(hints === "error") {
108+
Array.prototype.push.apply(compilation.errors, warnings);
109+
} else {
110+
Array.prototype.push.apply(compilation.warnings, warnings);
111+
}
112+
}
113+
}
114+
115+
callback();
116+
});
117+
118+
};

0 commit comments

Comments
 (0)