Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Meta
create-react-app
Commits
7cf4de71
Commit
7cf4de71
authored
7 years ago
by
Dan Abramov
Committed by
GitHub
7 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Warn about large bundle sizes (#2648)
parent
a4197b61
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/react-dev-utils/FileSizeReporter.js
+33
-2
packages/react-dev-utils/FileSizeReporter.js
packages/react-scripts/scripts/build.js
+11
-1
packages/react-scripts/scripts/build.js
with
44 additions
and
3 deletions
+44
-3
packages/react-dev-utils/FileSizeReporter.js
+
33
-
2
View file @
7cf4de71
...
@@ -18,7 +18,13 @@ var stripAnsi = require('strip-ansi');
...
@@ -18,7 +18,13 @@ var stripAnsi = require('strip-ansi');
var
gzipSize
=
require
(
'
gzip-size
'
).
sync
;
var
gzipSize
=
require
(
'
gzip-size
'
).
sync
;
// Prints a detailed summary of build files.
// Prints a detailed summary of build files.
function
printFileSizesAfterBuild
(
webpackStats
,
previousSizeMap
,
buildFolder
)
{
function
printFileSizesAfterBuild
(
webpackStats
,
previousSizeMap
,
buildFolder
,
maxBundleGzipSize
,
maxChunkGzipSize
)
{
var
root
=
previousSizeMap
.
root
;
var
root
=
previousSizeMap
.
root
;
var
sizes
=
previousSizeMap
.
sizes
;
var
sizes
=
previousSizeMap
.
sizes
;
var
assets
=
webpackStats
var
assets
=
webpackStats
...
@@ -41,6 +47,7 @@ function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder) {
...
@@ -41,6 +47,7 @@ function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder) {
null
,
null
,
assets
.
map
(
a
=>
stripAnsi
(
a
.
sizeLabel
).
length
)
assets
.
map
(
a
=>
stripAnsi
(
a
.
sizeLabel
).
length
)
);
);
var
suggestBundleSplitting
=
false
;
assets
.
forEach
(
asset
=>
{
assets
.
forEach
(
asset
=>
{
var
sizeLabel
=
asset
.
sizeLabel
;
var
sizeLabel
=
asset
.
sizeLabel
;
var
sizeLength
=
stripAnsi
(
sizeLabel
).
length
;
var
sizeLength
=
stripAnsi
(
sizeLabel
).
length
;
...
@@ -48,14 +55,38 @@ function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder) {
...
@@ -48,14 +55,38 @@ function printFileSizesAfterBuild(webpackStats, previousSizeMap, buildFolder) {
var
rightPadding
=
'
'
.
repeat
(
longestSizeLabelLength
-
sizeLength
);
var
rightPadding
=
'
'
.
repeat
(
longestSizeLabelLength
-
sizeLength
);
sizeLabel
+=
rightPadding
;
sizeLabel
+=
rightPadding
;
}
}
var
isMainBundle
=
asset
.
name
.
indexOf
(
'
main.
'
)
===
0
;
var
maxRecommendedSize
=
isMainBundle
?
maxBundleGzipSize
:
maxChunkGzipSize
;
var
isLarge
=
maxRecommendedSize
&&
asset
.
size
>
maxRecommendedSize
;
if
(
isLarge
&&
path
.
extname
(
asset
.
name
)
===
'
.js
'
)
{
suggestBundleSplitting
=
true
;
}
console
.
log
(
console
.
log
(
'
'
+
'
'
+
sizeLabel
+
(
isLarge
?
chalk
.
yellow
(
sizeLabel
)
:
sizeLabel
)
+
'
'
+
'
'
+
chalk
.
dim
(
asset
.
folder
+
path
.
sep
)
+
chalk
.
dim
(
asset
.
folder
+
path
.
sep
)
+
chalk
.
cyan
(
asset
.
name
)
chalk
.
cyan
(
asset
.
name
)
);
);
});
});
if
(
suggestBundleSplitting
)
{
console
.
log
();
console
.
log
(
chalk
.
yellow
(
'
The bundle size is significantly larger than recommended.
'
)
);
console
.
log
(
chalk
.
yellow
(
'
Consider reducing it with code splitting: https://goo.gl/9VhYWB
'
)
);
console
.
log
(
chalk
.
yellow
(
'
You can also analyze the project dependencies: https://goo.gl/LeUzfb
'
)
);
}
}
}
function
removeFileNameHash
(
buildFolder
,
fileName
)
{
function
removeFileNameHash
(
buildFolder
,
fileName
)
{
...
...
This diff is collapsed.
Click to expand it.
packages/react-scripts/scripts/build.js
+
11
-
1
View file @
7cf4de71
...
@@ -39,6 +39,10 @@ const measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild
...
@@ -39,6 +39,10 @@ const measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild
const
printFileSizesAfterBuild
=
FileSizeReporter
.
printFileSizesAfterBuild
;
const
printFileSizesAfterBuild
=
FileSizeReporter
.
printFileSizesAfterBuild
;
const
useYarn
=
fs
.
existsSync
(
paths
.
yarnLockFile
);
const
useYarn
=
fs
.
existsSync
(
paths
.
yarnLockFile
);
// These sizes are pretty large. We'll warn for bundles exceeding them.
const
WARN_AFTER_BUNDLE_GZIP_SIZE
=
512
*
1024
;
const
WARN_AFTER_CHUNK_GZIP_SIZE
=
1024
*
1024
;
// Warn and crash if required files are missing
// Warn and crash if required files are missing
if
(
!
checkRequiredFiles
([
paths
.
appHtml
,
paths
.
appIndexJs
]))
{
if
(
!
checkRequiredFiles
([
paths
.
appHtml
,
paths
.
appIndexJs
]))
{
process
.
exit
(
1
);
process
.
exit
(
1
);
...
@@ -76,7 +80,13 @@ measureFileSizesBeforeBuild(paths.appBuild)
...
@@ -76,7 +80,13 @@ measureFileSizesBeforeBuild(paths.appBuild)
}
}
console
.
log
(
'
File sizes after gzip:
\n
'
);
console
.
log
(
'
File sizes after gzip:
\n
'
);
printFileSizesAfterBuild
(
stats
,
previousFileSizes
,
paths
.
appBuild
);
printFileSizesAfterBuild
(
stats
,
previousFileSizes
,
paths
.
appBuild
,
WARN_AFTER_BUNDLE_GZIP_SIZE
,
WARN_AFTER_CHUNK_GZIP_SIZE
);
console
.
log
();
console
.
log
();
const
appPackage
=
require
(
paths
.
appPackageJson
);
const
appPackage
=
require
(
paths
.
appPackageJson
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment