-
Notifications
You must be signed in to change notification settings - Fork 147
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
refactor: optimize bundle size with isolated modules #1839
Conversation
🦋 Changeset detectedLatest commit: 07deaa3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ PR title follows Conventional Commits specification. |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 07deaa3:
|
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.
Can you check carousel story once? seems like its broken.
https://61c19ee8d3d282003ac1d81c-tenvvkemkx.chromatic.com/?path=/story/components-carousel--default-carousel
Might have to take a look at components that are using a bit complex lodash methods. I'll try testing few from my end
Checked. Imports were breaking. |
BundleMonFiles removed (6)
Unchanged files (8)
Total files change -603.34KB -94.34% Final result: ✅ View report in BundleMon website ➡️ |
packages/blade/scripts/buildBlade.sh
Outdated
# This script is used to build the blade package. | ||
set -e | ||
|
||
yarn build:clean |
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.
We can also use it without glob pattern like run-s build:clean build:generate-types build:react-prod build:react-dev build:react-native
, right?
yarn add @razorpay/blade [email protected] @floating-ui/react | ||
yarn add @razorpay/blade [email protected] |
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.
@floating-ui/react
is no longer a peer dependency?
Co-authored-by: saurabhdaware <[email protected]>
The problem:
Currently blade is bundled into a single
index.js
file and gets shipped to consumers, which consumers then import and treeshake.This works for treeshaking the whole bundle as a single module but this creates an issue where lazy loading components / codesplitting components doesn't work, if consumer tries to codesplit a component from blade it will always remain in the main bundle instead of in a new chunk.
react-router also has the similar problem, read more here.
The Solution:
Instead of bundling into a single index.js file, this PR changes the bundled output to preserve the original file structure via rollup's preserveModules flag and separate each module on it's own file. This way webpack will be able to individually codesplit/treeshake modules.
The bundled output now looks like this:
Further Bundle Optimizations
We've removed lodash as a dependency from blade and instead use alternative lightweight version of the utilities that we use internally.
Previously we used to bundle dependencies like
@use-gesture/react
,tinycolor2
with blade, which means even if you are not usingBottomSheet
orcreateTheme
you'll get the bundle of these libraries.And finally, with this change consumers can now properly do lazy loading of components and webpack will be able to create chunks.
sideEffects: false
Another problem which occurred because of Table, since Table uses @react-table/table-library & @emotion/react, emotion is causing side effects and both of these libraries gets included in the bundle, to fix these issues we marked blade as
sideEffects:false
in package.json.Unfortunately, there is still an issue with emotion which causes around 5kb of bundle to still get into the main bundle even with
sideEffects:false
. This we can explore later on how to solve.Impact:
After rendering a vite app with 1 button:
Before (v10.19.1): 104kb gzip
After: 78kb gzip
Reduction: 26kb
emotion-js/emotion#1279