Skip to content

Commit 93ae6bc

Browse files
TheLarkInnsokra
authored andcommitted
chore(performance) cleanup unused functionality, partial cleanup
1 parent 9894b58 commit 93ae6bc

11 files changed

Lines changed: 82 additions & 99 deletions

File tree

‎lib/Chunk.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,6 @@ Chunk.prototype.isEmpty = function() {
242242
return this.modules.length === 0;
243243
};
244244

245-
Chunk.prototype.isAsync = function() {
246-
return !this.isInitial() && !this.hasRuntime();
247-
};
248-
249245
Chunk.prototype.updateHash = function(hash) {
250246
hash.update(this.id + " ");
251247
hash.update(this.ids ? this.ids.join(",") : "");

‎lib/Compilation.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
529+
var entrypoint = self.entrypoints[chunk.name] = new Entrypoint(chunk.name, this);
530530
entrypoint.unshiftChunk(chunk);
531531

532532
chunk.addModule(module);
@@ -1031,7 +1031,3 @@ Compilation.prototype.checkConstraints = function() {
10311031
chunk.checkConstraints();
10321032
}.bind(this));
10331033
};
1034-
1035-
Compilation.prototype.isWebTarget = function() {
1036-
return this.options.target === "web";
1037-
};

‎lib/Entrypoint.js‎

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

@@ -22,3 +23,26 @@ Entrypoint.prototype.insertChunk = function(chunk, before) {
2223
}
2324
chunk.entrypoints.push(this);
2425
};
26+
27+
Entrypoint.prototype.getFiles = function() {
28+
var files = [];
29+
30+
for (var chunkIdx = 0; chunkIdx < this.chunks.length; chunkIdx++ ) {
31+
for (var fileIdx = 0; fileIdx < this.chunks[chunkIdx].files.length; fileIdx++) {
32+
if (files.indexOf(this.chunks[chunkIdx].files[fileIdx]) === -1) {
33+
files.push(this.chunks[chunkIdx].files[fileIdx]);
34+
}
35+
}
36+
}
37+
38+
return files;
39+
}
40+
41+
Entrypoint.prototype.getSize = function() {
42+
var files = this.getFiles();
43+
var compilation = this.compilation;
44+
45+
return files
46+
.map(function(file) { return compilation.assets[file].size() })
47+
.reduce(function(currentSize, nextSize) { return currentSize + nextSize}, 0);
48+
}

‎lib/Stats.js‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Stats.prototype.toJson = function toJson(options, forToString) {
2929
return v === undefined ? def : v;
3030
}
3131
var compilation = this.compilation;
32-
var isWebTarget = compilation.isWebTarget();
3332
var requestShortener = new RequestShortener(d(options.context, process.cwd()));
3433
var showPerformance = d(options.performance, true);
3534
var showHash = d(options.hash, true);
@@ -170,9 +169,13 @@ Stats.prototype.toJson = function toJson(options, forToString) {
170169
size: compilation.assets[asset].size(),
171170
chunks: [],
172171
chunkNames: [],
173-
emitted: compilation.assets[asset].emitted,
174-
isOverSizeLimit: compilation.assets[asset].isOverSizeLimit
172+
emitted: compilation.assets[asset].emitted
175173
};
174+
175+
if (showPerformance) {
176+
obj.isOverSizeLimit = compilation.assets[asset].isOverSizeLimit;
177+
}
178+
176179
assetsByFile[asset] = obj;
177180
return obj;
178181
}).filter(function(asset) {
@@ -327,9 +330,6 @@ Stats.prototype.toJson = function toJson(options, forToString) {
327330
});
328331
}
329332

330-
if(showPerformance) {
331-
obj.performance = compilation.performance;
332-
}
333333
return obj;
334334
};
335335

@@ -452,12 +452,11 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
452452
}
453453

454454
function getAssetColor(asset, defaultColor) {
455-
if("performance" in obj) {
456-
if(asset.isOverSizeLimit) {
457-
return colors.yellow;
458-
}
455+
if(asset.isOverSizeLimit) {
456+
return colors.yellow;
459457
}
460458

459+
461460
return defaultColor;
462461
}
463462

‎lib/performance/EmittedAssetSizeLimitPlugin.js‎

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var path = require('path');
66

77
var EntrypointsOverSizeLimitWarning = require('./EntrypointsOverSizeLimitWarning');
88
var AssetsOverSizeLimitWarning = require('./AssetsOverSizeLimitWarning');
9+
var NoAsyncChunksWarning = require('./NoAsyncChunksWarning');
910

1011
function EmittedAssetSizeLimitPlugin(performanceOptions) {
1112
this.maxAssetSize = performanceOptions.maxAssetSize;
@@ -53,7 +54,6 @@ EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
5354
compiler.plugin("emit", function(compilation, callback) {
5455
var warnings = [];
5556
var assetsByFile = {};
56-
var assetsByChunkName = {};
5757
var assetsOverSizeLimit = [];
5858
var entrypointsOverSizeLimit = [];
5959

@@ -86,81 +86,62 @@ EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
8686
});
8787
if(chunk.name) {
8888
assetsByFile[asset].chunkNames.push(chunk.name);
89-
if(assetsByChunkName[chunk.name]) {
90-
assetsByChunkName[chunk.name] = [].concat(assetsByChunkName[chunk.name]).concat([asset]);
91-
} else {
92-
assetsByChunkName[chunk.name] = asset;
93-
}
9489
}
9590
}
9691
});
9792
});
9893

9994
var hasAsyncChunks = compilation.chunks.filter(function(chunk) {
100-
return chunk.isAsync();
95+
return !chunk.isInitial();
10196
}).length > 0;
10297

103-
var entrypoints = Object.keys(compilation.entrypoints).map(function(ep) {
104-
var files = [];
105-
var entrypoint = compilation.entrypoints[ep];
98+
var entrypointsOverLimit = Object.keys(compilation.entrypoints)
99+
.map(function(key) {
100+
return compilation.entrypoints[key]
101+
})
102+
.filter(function(entry) {
103+
return doesExceedLimit(entrypointSizeLimit, entry.getSize())
104+
})
106105

107-
entrypoint.assets = {};
108-
entrypoint.size = 0;
109-
110-
// TODO: Need to use keys or even better Set for distinct values
111-
entrypoint.chunks.forEach(function(chunk) {
112-
chunk.files.forEach(function(file) {
113-
files.push(file);
114-
});
115-
});
116-
117-
files.forEach(function(file) {
118-
if(!isAssetJsFile(file)) {
119-
return;
120-
}
121-
entrypoint.assets[file] = assetsByFile[file];
122-
entrypoint.size = entrypoint.size + assetsByFile[file].size;
123-
entrypoint.isOverSizeLimit = false;
124-
if(entrypoint.assets[file].isOverSizeLimit) {
125-
entrypoint.isOverSizeLimit = true;
126-
}
127-
});
128-
129-
if(!entrypoint.isOverSizeLimit) {
130-
entrypoint.isOverSizeLimit = doesExceedLimit(entrypointSizeLimit, formatSize(entrypoint.size).number);
131-
}
132-
133-
return entrypoint;
134-
});
135-
136-
entrypointsOverSizeLimit = entrypoints.filter(function(ep) {
137-
return ep.isOverSizeLimit;
138-
})
139-
140-
// 1. Individual Chunk: Size < 200kb
141-
// 2. Collective Initial Chunks (Each Set?): Size < 200kb
106+
// 1. Individual Chunk: Size < 250kb
107+
// 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
142108
// 3. No Async Chunks
143109
// if !1, then 2, if !2 return
144-
// if 1, then 2, then 3,
145110
if(assetsOverSizeLimit.length) {
146-
warnings.push(new AssetsOverSizeLimitWarning(assetsOverSizeLimit, compilation, formatSize, sizeLimit));
147-
warnings.push(new EntrypointsOverSizeLimitWarning(entrypointsOverSizeLimit, compilation, formatSize, entrypointSizeLimit));
111+
warnings.push(
112+
new AssetsOverSizeLimitWarning(
113+
assetsOverSizeLimit,
114+
compilation,
115+
formatSize,
116+
sizeLimit
117+
)
118+
);
119+
120+
warnings.push(
121+
new EntrypointsOverSizeLimitWarning(
122+
entrypointsOverLimit,
123+
compilation,
124+
formatSize,
125+
entrypointSizeLimit
126+
)
127+
);
148128

149129
if(!hasAsyncChunks) {
150-
warnings.push(new Error("webpack performance recommendations: \n" +
151-
"You can limit the size of your bundles by using System.import() or require.ensure to lazy load some parts of your application.\n" +
152-
"For more info visit https://webpack.github.io/docs/code-splitting.html"
153-
));
130+
warnings.push(new NoAsyncChunksWarning());
154131
}
155132
} else {
156133
if(entrypointsOverSizeLimit.legnth) {
157-
warnings.push(new EntrypointsOverSizeLimitWarning(entrypointsOverSizeLimit, compilation, formatSize, entrypointSizeLimit));
134+
warnings.push(
135+
new EntrypointsOverSizeLimitWarning(
136+
entrypointsOverSizeLimit,
137+
compilation,
138+
formatSize,
139+
entrypointSizeLimit
140+
)
141+
);
158142

159143
if(!hasAsyncChunks) {
160-
warnings.push(new Error("webpack performance recommendations: \n" +
161-
"You can limit the size of your bundles by using System.import() or require.ensure to lazy load some parts of your application.\n" +
162-
"For more info visit https://webpack.github.io/docs/code-splitting.html"
163-
));
144+
warnings.push(new NoAsyncChunksWarning());
164145
}
165146
}
166147
}

‎lib/performance/EntrypointsOverSizeLimitWarning.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ function EntrypointsOverSizeLimitWarning(entrypoints, compilation, formatSizeFn,
1010
var formatSize = formatSizeFn;
1111

1212
var entrypointList = this.entrypoints.map(function(entrypoint) {
13-
return "\n " + entrypoint.name + ": " + formatSize(entrypoint.size).string + "\n" +
14-
Object.keys(entrypoint.assets).map(function(assetKey) {
15-
var asset = entrypoint.assets[assetKey];
16-
return " " + asset.name + ": " + formatSize(asset.size).string + "\n";
13+
return "\n " + entrypoint.name + ": " + formatSize(entrypoint.getSize()).string + "\n" +
14+
Object.keys(entrypoint.getFiles()).map(function(filename) {
15+
return " " + entrypoint.getFiles()[filename] + "\n";
1716
}).join("");
1817
}).join("");
1918

‎test/Stats.test.js‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,10 @@ describe("Stats", function() {
140140
modules: [],
141141
children: [],
142142
hash: '1234',
143-
isWebTarget: function() {
144-
return "web";
145-
},
146143
mainTemplate: {
147144
getPublicPath: function() {
148145
return 'path';
149146
}
150-
},
151-
compiler: {
152-
options: {
153-
performance: {
154-
hints: true,
155-
maxAssetSize: 250
156-
},
157-
target: "web"
158-
}
159147
}
160148
});
161149
var obj = mockStats.toJson();

‎test/statsCases/performance-error/expected.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ chunk {<CLR=33,BOLD>3</CLR>} <CLR=32,BOLD>main.js</CLR> (main) 540 kB<CLR=33,
1616
[1] <CLR=BOLD>(webpack)/buildin/module.js</CLR> 528 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
1717
[2] <CLR=BOLD>(webpack)/~/lodash/lodash.js</CLR> 539 kB {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
1818
[3] <CLR=BOLD>(webpack)/test/statsCases/performance-error/a.js</CLR> 22 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
19-
[8] <CLR=BOLD>(webpack)/test/statsCases/performance-error/index.js</CLR> 93 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
19+
[8] <CLR=BOLD>(webpack)/test/statsCases/performance-error/index.js</CLR> 71 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
2020

2121
<CLR=31,BOLD>ERROR in asset size limit: The following assets exceed the recommended size limit (250 kB).
2222
This can impact web performance.
@@ -27,5 +27,5 @@ Assets:
2727
Entrypoints:
2828

2929
main: 546 kB
30-
main.js: 546 kB
30+
main.js
3131
</CLR>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
require("./a");
22
require(["./b"]);
33
require(["./c"]);
4-
require("../../../node_modules/lodash");
4+
require("lodash");

‎test/statsCases/preset-normal-performance/expected.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ chunk {<CLR=33,BOLD>3</CLR>} <CLR=32,BOLD>main.js</CLR> (main) 540 kB<CLR=33,
1616
[1] <CLR=BOLD>(webpack)/buildin/module.js</CLR> 528 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
1717
[2] <CLR=BOLD>(webpack)/~/lodash/lodash.js</CLR> 539 kB {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
1818
[3] <CLR=BOLD>(webpack)/test/statsCases/preset-normal-performance/a.js</CLR> 22 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
19-
[8] <CLR=BOLD>(webpack)/test/statsCases/preset-normal-performance/index.js</CLR> 93 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
19+
[8] <CLR=BOLD>(webpack)/test/statsCases/preset-normal-performance/index.js</CLR> 71 bytes {<CLR=33,BOLD>3</CLR>}<CLR=32,BOLD> [built]</CLR>
2020

2121
<CLR=33,BOLD>WARNING in asset size limit: The following assets exceed the recommended size limit (250 kB).
2222
This can impact web performance.
@@ -27,5 +27,5 @@ Assets:
2727
Entrypoints:
2828

2929
main: 546 kB
30-
main.js: 546 kB
30+
main.js
3131
</CLR>

0 commit comments

Comments
 (0)