Skip to content

Commit

Permalink
Fix asset size comparison for multi build stats (facebook#3514)
Browse files Browse the repository at this point in the history
If create-react-app project is ejected and webpack configuration is
modified to multi build setup FileSizeReporter would fail.

In those situations `webpackStats` parameter would contain stats array
for each build. This fix will try to access stats and then falls back
to using plaing webpackStats object.
  • Loading branch information
iiska authored and Pavel Zhytko committed Jul 10, 2018
1 parent ac579ab commit e804b61
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/react-dev-utils/FileSizeReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,29 @@ function printFileSizesAfterBuild(
) {
var root = previousSizeMap.root;
var sizes = previousSizeMap.sizes;
var assets = webpackStats
.toJson()
.assets.filter(asset => /\.(js|css)$/.test(asset.name))
.map(asset => {
var fileContents = fs.readFileSync(path.join(root, asset.name));
var size = gzipSize(fileContents);
var previousSize = sizes[removeFileNameHash(root, asset.name)];
var difference = getDifferenceLabel(size, previousSize);
return {
folder: path.join(path.basename(buildFolder), path.dirname(asset.name)),
name: path.basename(asset.name),
size: size,
sizeLabel: filesize(size) + (difference ? ' (' + difference + ')' : ''),
};
});
var assets = (webpackStats.stats || [webpackStats])
.map(stats =>
stats
.toJson()
.assets.filter(asset => /\.(js|css)$/.test(asset.name))
.map(asset => {
var fileContents = fs.readFileSync(path.join(root, asset.name));
var size = gzipSize(fileContents);
var previousSize = sizes[removeFileNameHash(root, asset.name)];
var difference = getDifferenceLabel(size, previousSize);
return {
folder: path.join(
path.basename(buildFolder),
path.dirname(asset.name)
),
name: path.basename(asset.name),
size: size,
sizeLabel:
filesize(size) + (difference ? ' (' + difference + ')' : '')
};
})
)
.reduce((single, all) => all.concat(single), []);
assets.sort((a, b) => b.size - a.size);
var longestSizeLabelLength = Math.max.apply(
null,
Expand Down

0 comments on commit e804b61

Please sign in to comment.