Skip to content
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

Merged
merged 5 commits into from
Sep 12, 2018
Merged

Optimize bundle sizes 🚀 #40

merged 5 commits into from
Sep 12, 2018

Conversation

emmenko
Copy link
Member

@emmenko emmenko commented Sep 12, 2018

Currently our bundle sizes are ~2M. 🙃

Those changes will reduce the bundle sizes by 75% 🎉

$ ll dist/ui-kit*
-rw-r--r--  1 emmenko  staff   469K 12 Sep 10:07 dist/ui-kit.cjs.js
-rw-r--r--  1 emmenko  staff   560K 12 Sep 10:07 dist/ui-kit.esm.js

Notable changes:

  • define dependencies as external (so they won't be bundled)
  • make react-select a peerDependencies (even though it was also defined as external, it was injecting "a lot of crap" into the bundle)
  • lodash.mapvalues is supposed to be a dependency
  • uglify the CJS bundle (there is also an additional command build:dev to generated the bundles without uglify, useful for inspecting the bundle)
  • removed uuid and replaced it with Math.random

Copy link
Contributor

@tdeekens tdeekens left a 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),
Copy link
Contributor

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 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Which article?

Copy link
Member Author

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';
Copy link
Contributor

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.

Copy link
Member Author

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 🤷‍♂️

Copy link
Contributor

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}`,
Copy link
Contributor

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'],
Copy link
Contributor

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({
Copy link
Contributor

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 :).

Copy link
Contributor

@dferber90 dferber90 left a 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()] : []),
Copy link
Contributor

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),
Copy link
Contributor

Choose a reason for hiding this comment

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

Which article?

emmenko and others added 2 commits September 12, 2018 11:24
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,
Copy link
Member Author

@emmenko emmenko Sep 12, 2018

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.
Copy link
Contributor

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({
Copy link
Contributor

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

Copy link
Contributor

@montezume montezume left a 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...

@montezume
Copy link
Contributor

Can you at least explain why you reordered the plugins?

@emmenko emmenko mentioned this pull request Sep 12, 2018
Copy link
Contributor

@montezume montezume left a comment

Choose a reason for hiding this comment

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

Thanks!

@emmenko emmenko merged commit d4b169c into master Sep 12, 2018
@emmenko emmenko deleted the nm-optimize-bundle-sizes branch September 12, 2018 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants