Skip to content

Commit e949aa1

Browse files
TheLarkInnsokra
authored andcommitted
feat(perfbudgets): fixed issues with bad asset checking, and formatting
1 parent 0084071 commit e949aa1

23 files changed

Lines changed: 96 additions & 392 deletions

File tree

lib/Stats.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,11 @@ Stats.prototype.toJson = function toJson(options, forToString) {
324324
return obj;
325325
});
326326
}
327-
if(showPerformance) {
328-
obj.performance = compilation.options.performance;
327+
328+
debugger;
329+
330+
if(showPerformance && compilation.compiler.options.target === "web") {
331+
obj.performance = compilation.compiler.options.performance;
329332
}
330333
return obj;
331334
};
@@ -455,7 +458,9 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
455458
}
456459

457460
function getAssetColor(asset, defaultColor) {
458-
if(isHugeBundle(asset.size)) {
461+
var jsRegex = /\.js($|\?)/i;
462+
463+
if(isHugeBundle(asset.size) && jsRegex.test(asset.name)) {
459464
return colors.yellow;
460465
}
461466
return defaultColor;

lib/performance/EmittedAssetSizeLimitPlugin.js

Lines changed: 76 additions & 3 deletions
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 EmittedAssetSizeWarningPlugin = require('./EmittedAssetSizeWarningPlugin.js');
5+
var path = require('path');
66

77
function EmittedAssetSizeLimitPlugin(performanceOptions) {
88
this.maxAssetSize = performanceOptions.maxAssetSize;
@@ -11,6 +11,21 @@ function EmittedAssetSizeLimitPlugin(performanceOptions) {
1111

1212
module.exports = EmittedAssetSizeLimitPlugin;
1313

14+
function uniques(array) {
15+
var result = [],
16+
val, ridx;
17+
outer:
18+
for(var i = 0, length = array.length; i < length; i++) {
19+
val = array[i];
20+
ridx = result.length;
21+
while(ridx--) {
22+
if(val === result[ridx]) continue outer;
23+
}
24+
result.push(val);
25+
}
26+
return result;
27+
}
28+
1429
function normalizeAndCompare(sizeLimit, assetSize) {
1530
// sizeLimit=maxAssetSize is always expressed in kB
1631
// assetSize is expressed in byte size
@@ -24,7 +39,7 @@ function getJSWarnings(noOfAssets, sizeLimit, assetSize) {
2439
if(noOfAssets === 1) {
2540
warnings.push(new Error("EmmittedAssetSizeWarning: " + "This asset exceeds " + sizeLimit + "kB. Consider reducing the size for optimal web performance."));
2641
} else {
27-
warnings.push(new Error("EmmittedAssetSizeWarning: " + "Highlighted chunks are large and are likely to impact web performance. Consider keeping total chunks of page < " + sizeLimit + "kB"));
42+
warnings.push(new Error("EmmittedAssetSizeWarning: " + "Highlighted chunks are large and are likely to impact web performance. Consider keeping total chunks of page < " + sizeLimit + "kB"));
2843
}
2944
}
3045
return warnings;
@@ -41,14 +56,72 @@ EmittedAssetSizeLimitPlugin.prototype.apply = function(compiler) {
4156
var jsRegex = /\.js($|\?)/i;
4257

4358
compiler.plugin("after-emit", function(compilation, callback) {
59+
console.log("Plugin Init");
4460
var assets = Object.keys(compilation.assets);
4561
var noOfAssets = assets.length;
4662
assets.forEach(function(file) {
4763
var assetSize = compilation.assets[file].size();
4864
var warnings = jsRegex.test(file) && getJSWarnings(noOfAssets, sizeLimit, assetSize);
49-
Array.prototype.push.apply(compilation.warnings, warnings);
65+
if(warnings.length > 0) {
66+
Array.prototype.push.apply(compilation.warnings, warnings);
67+
}
5068
});
5169
callback();
5270
});
5371

5472
};
73+
74+
//TODO# This should probably be moved to compilation
75+
EmittedAssetSizeLimitPlugin.prototype.getAssets = function(compilation, chunks) {
76+
var self = this;
77+
var webpackStatsJson = compilation.getStats().toJson();
78+
79+
var assets = {
80+
// Will contain all js & css files by chunk
81+
chunks: {},
82+
// Will contain all js files
83+
js: [],
84+
// Will contain all css files
85+
css: [],
86+
// Will contain the html5 appcache manifest files if it exists
87+
manifest: Object.keys(compilation.assets).filter(function(assetFile) {
88+
return path.extname(assetFile) === '.appcache';
89+
})[0]
90+
};
91+
92+
// Append a hash for cache busting
93+
for(var i = 0; i < chunks.length; i++) {
94+
var chunk = chunks[i];
95+
var chunkName = chunk.names[0];
96+
97+
assets.chunks[chunkName] = {};
98+
99+
// Prepend the public path to all chunk files
100+
var chunkFiles = [].concat(chunk.files).map(function(chunkFile) {
101+
return chunkFile;
102+
});
103+
104+
// Webpack outputs an array for each chunk when using sourcemaps
105+
// But we need only the entry file
106+
var entry = chunkFiles[0];
107+
assets.chunks[chunkName].size = chunk.size;
108+
assets.chunks[chunkName].entry = entry;
109+
assets.chunks[chunkName].hash = chunk.hash;
110+
assets.js.push(entry);
111+
112+
// Gather all css files
113+
var css = chunkFiles.filter(function(chunkFile) {
114+
// Some chunks may contain content hash in their names, for ex. 'main.css?1e7cac4e4d8b52fd5ccd2541146ef03f'.
115+
// We must proper handle such cases, so we use regexp testing here
116+
return /.css($|\?)/.test(chunkFile);
117+
});
118+
assets.chunks[chunkName].css = css;
119+
assets.css = assets.css.concat(css);
120+
}
121+
122+
// Duplicate css assets can occur on occasion if more than one chunk
123+
// requires the same css.
124+
assets.css = uniques(assets.css);
125+
126+
return assets;
127+
};

lib/performance/EmittedAssetSizeWarningPlugin.js

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

test/Stats.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ describe("Stats", function() {
144144
getPublicPath: function() {
145145
return 'path';
146146
}
147+
},
148+
compiler: {
149+
options: {
150+
performance: {
151+
hints: true,
152+
maxAssetSize: 250
153+
},
154+
target: "web"
155+
}
147156
}
148157
});
149158
var obj = mockStats.toJson();

test/statsCases/aggressive-splitting-entry/actual.txt

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

test/statsCases/aggressive-splitting-on-demand/actual.txt

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

test/statsCases/chunks/actual.txt

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

test/statsCases/color-disabled/actual.txt

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

test/statsCases/color-enabled-custom/actual.txt

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

test/statsCases/color-enabled/actual.txt

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

0 commit comments

Comments
 (0)