Skip to content

⚡️(build) make the library tree-shakeable#258

Open
sylvinus wants to merge 1 commit into
mainfrom
tree_shaking
Open

⚡️(build) make the library tree-shakeable#258
sylvinus wants to merge 1 commit into
mainfrom
tree_shaking

Conversation

@sylvinus

@sylvinus sylvinus commented Jun 17, 2026

Copy link
Copy Markdown
Member

Right now, importing a single component pulls in the entire UI kit :/

Benchmarks (single import, bundled with esbuild, deps external, minified):

Import Before After Reduction
{ Badge } 547 KB 350 B ~99.9%
{ Icon } 547 KB 600 B ~99.9%
{ useResponsive } 547 KB 26 KB ~95%
{ Hero } 547 KB 30 KB ~95%
{ Footer } 547 KB 40 KB ~93%
one icon from /icons 349 KB 5.5 KB ~98%
whole barrel (import *) 547 KB 390 KB ~29%

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant