⚡️(build) make the library tree-shakeable#258
Open
sylvinus wants to merge 1 commit into
Open
Conversation
Consumers (e.g. suitenumerique/messages) reported that importing a single
component pulled in the entire UI kit.
Benchmarks (single import, bundled with esbuild, deps external, minified):
import before after
---------------------- -------- --------
{ Badge } 547 KB 350 B
{ Icon } 547 KB 600 B
{ useResponsive } 547 KB 26 KB
{ Hero } 547 KB 30 KB
{ Footer } 547 KB 40 KB
one icon from /icons 349 KB 5.5 KB
whole barrel (import *) 547 KB 390 KB
Root causes
-----------
1. Mega-chunk. Vite's default lib build merged all ~500 source modules into
a single `index-<hash>.js`. A bundler resolving `{ Badge }` had to pull
that one file in its entirety — there were no module boundaries left to
shake against. This was the dominant cause.
2. Missing `sideEffects`. Without it in package.json, bundlers must assume
every module has side effects and cannot drop unused ones. Verified that
adding this *alone* changed nothing (the mega-chunk still defeated it) —
both fixes are required together.
3. Bundled dependencies. clsx, @dnd-kit/*, cmdk, react-arborist,
react-aria-components, react-stately, react-resizable-panels and others
were inlined into the output, shipping duplicate copies and preventing
consumer-side dedup.
Notes
-----
* CSS is unchanged: dist/style.css stays a single ~356 KB (~41 KB gzipped)
stylesheet, loaded in full on any import via index.ts -> library.scss.
JS now tree-shakes; per-component CSS splitting would be a separate effort.
* The published output is now many small files instead of two large ones;
package "exports", entry points (index, icons), types and assets are
unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Right now, importing a single component pulls in the entire UI kit :/
Benchmarks (single import, bundled with esbuild, deps external, minified):
{ Badge }{ Icon }{ useResponsive }{ Hero }{ Footer }/iconsimport *)Root causes
Mega-chunk. Vite's default lib build merged all ~500 source modules into a single
index-<hash>.js. A bundler resolving{ Badge }had to pull that one file in its entirety — there were no module boundaries left to shake against. This was the dominant cause.Missing
sideEffects. Without it in package.json, bundlers must assume every module has side effects and cannot drop unused ones. Verified that adding this alone changed nothing (the mega-chunk still defeated it) — both fixes are required together.Bundled dependencies. clsx, @dnd-kit/*, cmdk, react-arborist, react-aria-components, react-stately, react-resizable-panels and others were inlined into the output, shipping duplicate copies and preventing consumer-side dedup.
Notes