Skip to content

Commit c6850b3

Browse files
TheLarkInnsokra
authored andcommitted
chore(performance): cleaned up unused variables, removed compilation from constructor, added stats additional messages
1 parent 72f049c commit c6850b3

7 files changed

Lines changed: 42 additions & 66 deletions

lib/Compilation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function Compilation(compiler) {
3232
this.bail = options && options.bail;
3333
this.profile = options && options.profile;
3434
this.performance = options && options.performance;
35-
35+
3636
this.mainTemplate = new MainTemplate(this.outputOptions);
3737
this.chunkTemplate = new ChunkTemplate(this.outputOptions, this.mainTemplate);
3838
this.hotUpdateChunkTemplate = new HotUpdateChunkTemplate(this.outputOptions);
@@ -526,7 +526,7 @@ Compilation.prototype.seal = function seal(callback) {
526526
self.preparedChunks.forEach(function(preparedChunk) {
527527
var module = preparedChunk.module;
528528
var chunk = self.addChunk(preparedChunk.name, module);
529-
var entrypoint = self.entrypoints[chunk.name] = new Entrypoint(chunk.name, this);
529+
var entrypoint = self.entrypoints[chunk.name] = new Entrypoint(chunk.name);
530530
entrypoint.unshiftChunk(chunk);
531531

532532
chunk.addModule(module);

lib/Entrypoint.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function Entrypoint(name, compilation) {
5+
function Entrypoint(name) {
66
this.name = name;
77
this.chunks = [];
8-
this.compilation = compilation;
98
}
109
module.exports = Entrypoint;
1110

@@ -25,8 +24,8 @@ Entrypoint.prototype.insertChunk = function(chunk, before) {
2524
};
2625

2726
Entrypoint.prototype.getFiles = function() {
28-
var files = [];
29-
27+
var files = [];
28+
3029
for (var chunkIdx = 0; chunkIdx < this.chunks.length; chunkIdx++ ) {
3130
for (var fileIdx = 0; fileIdx < this.chunks[chunkIdx].files.length; fileIdx++) {
3231
if (files.indexOf(this.chunks[chunkIdx].files[fileIdx]) === -1) {
@@ -38,9 +37,8 @@ Entrypoint.prototype.getFiles = function() {
3837
return files;
3938
}
4039

41-
Entrypoint.prototype.getSize = function() {
40+
Entrypoint.prototype.getSize = function(compilation) {
4241
var files = this.getFiles();
43-
var compilation = this.compilation;
4442

4543
return files
4644
.map(function(file) { return compilation.assets[file].size() })

lib/Stats.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,19 +479,21 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
479479
{value: "Size", color: colors.bold},
480480
{value: "Chunks", color: colors.bold},
481481
{value: "", color: colors.bold},
482+
{value: "", color: colors.bold},
482483
{value: "Chunk Names", color: colors.bold}
483484
]
484485
];
485486
obj.assets.forEach(function(asset) {
486487
t.push([
487-
{value: asset.name, color: getAssetColor(asset, colors.green)},
488-
{value: formatSize(asset.size), color: getAssetColor(asset, colors.normal)},
489-
{value: asset.chunks.join(", "), color: colors.bold},
490-
{value: asset.emitted ? "[emitted]" : "", color: colors.green},
488+
{value: asset.name, color: getAssetColor(asset, colors.green)},
489+
{value: formatSize(asset.size), color: getAssetColor(asset, colors.normal)},
490+
{value: asset.chunks.join(", "), color: colors.bold},
491+
{value: asset.emitted ? "[emitted]" : "", color: colors.green},
492+
{value: asset.isOverSizeLimit ? "[big]" : "", color: colors.yellow},
491493
{value: asset.chunkNames.join(", "), color: colors.normal}
492494
]);
493495
});
494-
table(t, "rrrll");
496+
table(t, "rrrlll");
495497
}
496498
if(obj.entrypoints) {
497499
Object.keys(obj.entrypoints).forEach(function(name) {

lib/WebpackOptionsApply.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,19 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
7474
case "web":
7575
JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
7676
NodeSourcePlugin = require("./node/NodeSourcePlugin");
77-
EmittedAssetSizeLimitPlugin = require("./performance/EmittedAssetSizeLimitPlugin");
77+
7878
compiler.apply(
7979
new JsonpTemplatePlugin(options.output),
8080
new FunctionModulePlugin(options.output),
8181
new NodeSourcePlugin(options.node),
82-
new LoaderTargetPlugin("web"),
83-
new EmittedAssetSizeLimitPlugin(options.performance)
82+
new LoaderTargetPlugin("web")
8483
);
84+
85+
if (options.performance.hints) {
86+
EmittedAssetSizeLimitPlugin = require("./performance/EmittedAssetSizeLimitPlugin");
87+
compiler.apply(new EmittedAssetSizeLimitPlugin(options.performance));
88+
}
89+
8590
break;
8691
case "webworker":
8792
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");

lib/performance/AssetsOverSizeLimitWarning.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
*/
55
var SizeFormatHelpers = require('../SizeFormatHelpers');
66

7-
function AssetsOverSizeLimitWarning(assetsOverSizeLimit, compilation, assetLimit) {
7+
function AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetLimit) {
88
Error.call(this);
99
Error.captureStackTrace(this, AssetsOverSizeLimitWarning);
1010
this.name = "AssetsOverSizeLimitWarning";
1111
this.assets = assetsOverSizeLimit;
1212

1313
var assetLists = this.assets.map(function(asset) {
14-
return "\n " + asset.name;
14+
return "\n " + asset.name + " (" + SizeFormatHelpers.formatSize(asset.size) +")";
1515
}).join("");
1616

17-
this.message = "asset size limit: The following assets exceed the recommended size limit (" + SizeFormatHelpers.formatSize(assetLimit) + "). \n" +
17+
this.message = "asset size limit: The following asset(s) exceed the recommended size limit (" + SizeFormatHelpers.formatSize(assetLimit) + "). \n" +
1818
"This can impact web performance.\n" +
1919
"Assets: " + assetLists;
2020
}

lib/performance/EmittedAssetSizeLimitPlugin.js

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ function doesExceedLimit(limit, actualSize) {
2323
return limit < actualSize;
2424
}
2525

26-
function isAssetJsFile(assetFilename) {
27-
var jsRegex = /\.js($|\?)/i;
28-
29-
return jsRegex.test(assetFilename);
30-
}
31-
3226
EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
3327
if(!this.hints) {
3428
return;
@@ -38,67 +32,41 @@ EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
3832
var hints = this.hints;
3933
var shouldErrorOnHint = this.errorOnHint;
4034

41-
compiler.plugin("emit", function(compilation, callback) {
35+
compiler.plugin("after-emit", function(compilation, callback) {
4236
var warnings = [];
43-
var assetsByFile = {};
4437
var assetsOverSizeLimit = [];
45-
var entrypointsOverSizeLimit = [];
4638

47-
var assets = Object.keys(compilation.assets).map(function(asset) {
39+
Object.keys(compilation.assets).forEach(function(asset) {
4840
var obj = {
4941
name: asset,
50-
size: compilation.assets[asset].size(),
51-
chunks: [],
52-
chunkNames: [],
53-
emitted: compilation.assets[asset].emitted
42+
size: compilation.assets[asset].size()
5443
};
5544

5645
if(doesExceedLimit(sizeLimit, obj.size)) {
5746
obj.isOverSizeLimit = true;
5847
assetsOverSizeLimit.push(obj);
5948
compilation.assets[asset].isOverSizeLimit = true;
6049
}
61-
62-
assetsByFile[asset] = obj;
63-
return obj;
64-
}).filter(function(asset) {
65-
return asset.emitted;
66-
});
67-
68-
compilation.chunks.forEach(function(chunk) {
69-
chunk.files.forEach(function(asset) {
70-
if(assetsByFile[asset]) {
71-
chunk.ids.forEach(function(id) {
72-
assetsByFile[asset].chunks.push(id);
73-
});
74-
if(chunk.name) {
75-
assetsByFile[asset].chunkNames.push(chunk.name);
76-
}
77-
}
78-
});
7950
});
8051

8152
var hasAsyncChunks = compilation.chunks.filter(function(chunk) {
8253
return !chunk.isInitial();
8354
}).length > 0;
8455

8556
var entrypointsOverLimit = Object.keys(compilation.entrypoints)
86-
.map(function(key) {
87-
return compilation.entrypoints[key]
88-
})
57+
.map(function(key) { return compilation.entrypoints[key] })
8958
.filter(function(entry) {
90-
return doesExceedLimit(entrypointSizeLimit, entry.getSize())
91-
})
59+
return doesExceedLimit(entrypointSizeLimit, entry.getSize(compilation))
60+
});
9261

9362
// 1. Individual Chunk: Size < 250kb
9463
// 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
9564
// 3. No Async Chunks
9665
// if !1, then 2, if !2 return
97-
if(assetsOverSizeLimit.length) {
66+
if(assetsOverSizeLimit.length > 0) {
9867
warnings.push(
9968
new AssetsOverSizeLimitWarning(
10069
assetsOverSizeLimit,
101-
compilation,
10270
sizeLimit
10371
)
10472
);
@@ -115,10 +83,10 @@ EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
11583
warnings.push(new NoAsyncChunksWarning());
11684
}
11785
} else {
118-
if(entrypointsOverSizeLimit.legnth) {
86+
if(entrypointsOverLimit.legnth > 0) {
11987
warnings.push(
12088
new EntrypointsOverSizeLimitWarning(
121-
entrypointsOverSizeLimit,
89+
entrypointsOverLimit,
12290
compilation,
12391
entrypointSizeLimit
12492
)
@@ -130,7 +98,7 @@ EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
13098
}
13199
}
132100

133-
if(warnings.length) {
101+
if(warnings.length > 0) {
134102
if(shouldErrorOnHint) {
135103
Array.prototype.push.apply(compilation.errors, warnings);
136104
} else {

lib/performance/EntrypointsOverSizeLimitWarning.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ function EntrypointsOverSizeLimitWarning(entrypoints, compilation, entrypointLim
1010
this.name = "EntrypointsOverSizeLimitWarning";
1111
this.entrypoints = entrypoints;
1212

13+
14+
var entrypointCompilation = compilation;
1315
var entrypointList = this.entrypoints.map(function(entrypoint) {
14-
return "\n " + entrypoint.name + ": " + SizeFormatHelpers.formatSize(entrypoint.getSize()) + "\n" +
15-
Object.keys(entrypoint.getFiles()).map(function(filename) {
16-
return " " + entrypoint.getFiles()[filename] + "\n";
16+
return "\n " + entrypoint.name + " (" + SizeFormatHelpers.formatSize(entrypoint.getSize(entrypointCompilation)) + ")\n" +
17+
entrypoint.getFiles().map(function(filename, index) {
18+
return " " + entrypoint.getFiles()[index] + "\n";
1719
}).join("");
1820
}).join("");
1921

22+
debugger;
23+
2024
this.message = "entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (" + SizeFormatHelpers.formatSize(entrypointLimit) + "). " +
2125
"This can impact web performance.\n" +
22-
"Entrypoints: \n" +
23-
entrypointList;
26+
"Entrypoints:" + entrypointList;
2427
}
2528
module.exports = EntrypointsOverSizeLimitWarning;
2629

0 commit comments

Comments
 (0)