Skip to content

Commit 37b7474

Browse files
committed
Refactoring, make options simpler
breaking change: performance options changed
1 parent 5da9d8c commit 37b7474

10 files changed

Lines changed: 168 additions & 159 deletions

lib/SizeFormatHelpers.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,3 @@ SizeFormatHelpers.formatSize = function(size) {
1616
.toPrecision(3) + " " + abbreviations[index];
1717
}
1818

19-
SizeFormatHelpers.getEntrypointSize = function(entrypoint, compilation) {
20-
var files = entrypoint.getFiles();
21-
22-
return files
23-
.filter(function(asset) {
24-
return !(/\.map$/.test(asset))
25-
})
26-
.map(function(file) {
27-
return compilation.assets[file].size()
28-
})
29-
.reduce(function(currentSize, nextSize) {
30-
return currentSize + nextSize
31-
}, 0);
32-
}

lib/Stats.js

Lines changed: 4 additions & 4 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;
@@ -501,7 +501,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
501501
value: asset.name,
502502
color: getAssetColor(asset, colors.green)
503503
}, {
504-
value: formatSize(asset.size),
504+
value: SizeFormatHelpers.formatSize(asset.size),
505505
color: getAssetColor(asset, colors.normal)
506506
}, {
507507
value: asset.chunks.join(", "),
@@ -548,7 +548,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
548548

549549
function processModuleAttributes(module) {
550550
colors.normal(" ");
551-
colors.normal(formatSize(module.size));
551+
colors.normal(SizeFormatHelpers.formatSize(module.size));
552552
if(module.chunks) {
553553
module.chunks.forEach(function(chunk) {
554554
colors.normal(" {");
@@ -658,7 +658,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
658658
colors.normal(")");
659659
}
660660
colors.normal(" ");
661-
colors.normal(formatSize(chunk.size));
661+
colors.normal(SizeFormatHelpers.formatSize(chunk.size));
662662
chunk.parents.forEach(function(id) {
663663
colors.normal(" {");
664664
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 & 106 deletions
This file was deleted.

lib/performance/EntrypointsOverSizeLimitWarning.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +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(SizeFormatHelpers.getEntrypointSize(entrypoint, entrypointCompilation)) + ")\n" +
16-
entrypoint.getFiles()
17-
.filter(function(asset) {
18-
return !(/\.map$/.test(asset))
19-
})
20-
.map(function(filename, index) {
21-
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";
2218
}).join("");
2319
}).join("");
2420

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)