-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize bundle sizes 🚀 #40
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a ton. Looks sharp to me. I'd recommend adding 1-2 more comments (even though we have some already) with a reference to the article before merging.
rollup.config.js
Outdated
file: `dist/${pkg.module}`, | ||
format: 'esm', | ||
}, | ||
external: Object.keys(pkg.dependencies).concat(pkg.peerDependencies), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd dump a comment and a ref to the article on top. Otherwise it feels like natural to remove this in 1-2 years :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which article?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -1,7 +1,6 @@ | |||
import React from 'react'; | |||
import PropTypes from 'prop-types'; | |||
import has from 'lodash.has'; | |||
import uuid from 'uuid'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we remove it from the pkg.json too? I can't see that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't there in the first place 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I'd recommend using depcheck
on the package once. It's awesome and is how I managed to clean up the deps on the mc-fe without much manual work.
{ | ||
input: 'src/index.js', | ||
output: { | ||
file: `dist/${pkg.main}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever.
rollup.config.js
Outdated
}), | ||
resolve(), | ||
svgrPlugin({ | ||
include: ['**/*.react.svg'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a comment for others that this is our way of targeting svgs which mean to become react components?
rollup.config.js
Outdated
// this is used for importing both vendor css (from node_modules) and for | ||
// importing our global css which uses tokens that require postcss plugins | ||
// (such as color-mod) to be compiled properly. | ||
postcss({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah, the good times of inverse exclude/include rules :).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error messages during the build are gone as well, nice 💯
rollup.config.js
Outdated
}), | ||
execute('node scripts/bundle-copy.js'), | ||
...basePlugins, | ||
...(process.env.NODE_ENV === 'production' ? [uglify()] : []), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would let rollup always bundle for production only. If you want to debug, I'd make make the person debugging uncomment this.
I'd do this to avoid confusion like "why are we even checking the env" when reading this file.
But I'm also fine with keeping it the way it is and accepting the slight confusion for the convenience. Or maybe add a comment.
rollup.config.js
Outdated
file: `dist/${pkg.module}`, | ||
format: 'esm', | ||
}, | ||
external: Object.keys(pkg.dependencies).concat(pkg.peerDependencies), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which article?
This factory is better than random numbers as we can ensure the ids in tests, and it guarantees unique ids, which ids based on random numbers would not have.
// so that they do not end up in the bundle. | ||
// See also https://medium.com/@kelin2025/so-you-wanna-use-es6-modules-714f48b3a953 | ||
peerDepsExternal({ | ||
includeDependencies: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: I found out that this does exactly the same as manually doing
external: Object.keys(pkg.dependencies).concat(Object.keys(pkg.peerDependencies))
rollup.config.js
Outdated
@@ -36,72 +44,104 @@ const postcssPlugins = [ | |||
postcssReporter(), | |||
]; | |||
|
|||
export default [ | |||
// This list includes common plugins shared between each output format. | |||
// The list is sorted alphabetically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why sorted alphabetically? It's important that we have the correct order.
rollup.config.js
Outdated
// To automatically externalize `dependencies` and `peerDependencies` | ||
// so that they do not end up in the bundle. | ||
// See also https://medium.com/@kelin2025/so-you-wanna-use-es6-modules-714f48b3a953 | ||
peerDepsExternal({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
peer deps external should go first. ref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the idea of ordering plugins alphabetically...
Can you at least explain why you reordered the plugins? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Currently our bundle sizes are
~2M
. 🙃Those changes will reduce the bundle sizes by 75% 🎉
Notable changes:
dependencies
asexternal
(so they won't be bundled)react-select
apeerDependencies
(even though it was also defined asexternal
, it was injecting "a lot of crap" into the bundle)lodash.mapvalues
is supposed to be adependency
uglify
the CJS bundle (there is also an additional commandbuild:dev
to generated the bundles without uglify, useful for inspecting the bundle)uuid
and replaced it withMath.random