Skip to content

Commit

Permalink
Fix asset size comparison for multi build stats (#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 gaearon committed Jan 9, 2018
1 parent bef40ee commit d61a331
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

1 comment on commit d61a331

@matheenpasha
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My builds results in two sub-folders based on two different apps. How do send in the root folder or build folder so that it doesn't break?

My build folder structure is as below -

/dist
/app1
/app2

When i send the root/build folder to be just '/dist' it breaks.
Any help is appreciated.

Thanks.

Please sign in to comment.