Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit a9c8b81

Browse files
gaearonwmonk
authored andcommitted
Warn about large bundle sizes (#2648)
1 parent 6f4c696 commit a9c8b81

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

packages/react-dev-utils/FileSizeReporter.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ var stripAnsi = require('strip-ansi');
1818
var gzipSize = require('gzip-size').sync;
1919

2020
// Prints a detailed summary of build files.
21-
function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder) {
21+
function printFileSizesAfterBuild(
22+
webpackStats,
23+
previousSizeMap,
24+
buildFolder,
25+
maxBundleGzipSize,
26+
maxChunkGzipSize
27+
) {
2228
var root = previousSizeMap.root;
2329
var sizes = previousSizeMap.sizes;
2430
var assets = webpackStats
@@ -41,21 +47,46 @@ function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder) {
4147
null,
4248
assets.map(a => stripAnsi(a.sizeLabel).length)
4349
);
50+
var suggestBundleSplitting = false;
4451
assets.forEach(asset => {
4552
var sizeLabel = asset.sizeLabel;
4653
var sizeLength = stripAnsi(sizeLabel).length;
4754
if (sizeLength < longestSizeLabelLength) {
4855
var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength);
4956
sizeLabel += rightPadding;
5057
}
58+
var isMainBundle = asset.name.indexOf('main.') === 0;
59+
var maxRecommendedSize = isMainBundle
60+
? maxBundleGzipSize
61+
: maxChunkGzipSize;
62+
var isLarge = maxRecommendedSize && asset.size > maxRecommendedSize;
63+
if (isLarge && path.extname(asset.name) === '.js') {
64+
suggestBundleSplitting = true;
65+
}
5166
console.log(
5267
' ' +
53-
sizeLabel +
68+
(isLarge ? chalk.yellow(sizeLabel) : sizeLabel) +
5469
' ' +
5570
chalk.dim(asset.folder + path.sep) +
5671
chalk.cyan(asset.name)
5772
);
5873
});
74+
if (suggestBundleSplitting) {
75+
console.log();
76+
console.log(
77+
chalk.yellow('The bundle size is significantly larger than recommended.')
78+
);
79+
console.log(
80+
chalk.yellow(
81+
'Consider reducing it with code splitting: https://goo.gl/9VhYWB'
82+
)
83+
);
84+
console.log(
85+
chalk.yellow(
86+
'You can also analyze the project dependencies: https://goo.gl/LeUzfb'
87+
)
88+
);
89+
}
5990
}
6091

6192
function removeFileNameHash(buildFolder, fileName) {

packages/react-scripts/scripts/build.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild
3939
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
4040
const useYarn = fs.existsSync(paths.yarnLockFile);
4141

42+
// These sizes are pretty large. We'll warn for bundles exceeding them.
43+
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
44+
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
45+
4246
// Warn and crash if required files are missing
4347
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
4448
process.exit(1);
@@ -76,7 +80,13 @@ measureFileSizesBeforeBuild(paths.appBuild)
7680
}
7781

7882
console.log('File sizes after gzip:\n');
79-
printFileSizesAfterBuild(stats, previousFileSizes, paths.appBuild);
83+
printFileSizesAfterBuild(
84+
stats,
85+
previousFileSizes,
86+
paths.appBuild,
87+
WARN_AFTER_BUNDLE_GZIP_SIZE,
88+
WARN_AFTER_CHUNK_GZIP_SIZE
89+
);
8090
console.log();
8191

8292
const appPackage = require(paths.appPackageJson);

0 commit comments

Comments
 (0)