Skip to content

fix!: externalize @base-ui/react so packages share one instance - #305

Merged
adamfratino merged 1 commit into
mainfrom
chore/externalize-base-ui
Aug 20, 2026
Merged

adamfratino merged 1 commit into
mainfrom
chore/externalize-base-ui

Conversation

@adamfratino

@adamfratino adamfratino commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

@base-ui/react was missing from baseExternal in vite.config.ts, so rollup followed into it and inlined it — along with @base-ui/utils, @floating-ui/core, @floating-ui/react-dom, reselect, and use-sync-external-store — into every consuming package's dist.

Closes #304

The defect, not just the bloat

Each package shipped its own module instance of Base UI's context modules. Two copies of a module mean two React.createContext() calls and two distinct context objects, so a provider rendered from one package's copy could not be read by a component resolving another's — composing across @uiid/* packages could silently fail to share state.

After this change, all eight packages resolve to a single physical copy under node_modules/.pnpm:

Context module before after
TooltipRootContext 2 copies 0 duplicates
TooltipProviderContext 2 copies 0 duplicates
DirectionContext 3 copies 0 duplicates
FloatingTree 3 copies 0 duplicates

Measured, from turbo run build --force on a cleared dist

Package before after delta
forms 2968K 1808K −1160K
overlays 1540K 624K −916K
buttons 712K 160K −552K
indicators 572K 464K −108K
layout 360K 312K −48K
total 7388K 4604K −2784K (416 files)

Emitted output now carries a bare external import:

import { DrawerPreview as t } from "@base-ui/react/drawer";

Changes

  • Add /^@base-ui\/react(\/|$)/ to baseExternal — a regex, not a bare string, so deep subpath imports like @base-ui/react/drawer match
  • Move @base-ui/react from dependencies to peerDependencies (^1.2.0) across the eight consuming packages, with an exact 1.2.0 devDependency so in-repo builds, types, and tests still resolve

The baseExternal change is the half that does the work — moving a dependency to peerDependencies on its own does not shrink anything. The manifest change just keeps installs correct alongside it.

Deviation from the issue

The issue proposed peerDependenciesMeta. I skipped it deliberately: the repo's existing precedent for react/react-dom is peer + dev with no meta, and marking @base-ui/react optional would be false — these components cannot render without it.

The version pin stays at 1.2.0 so this stays orthogonal to #303.

Two checklist items from the issue that cannot pass as written

1. "No node_modules/ directories remain under any packages/*/dist." Not achievable here. interactive still emits dist/node_modules containing @dnd-kit/core, @dnd-kit/modifiers, @dnd-kit/sortable, @dnd-kit/utilities, @dnd-kit/accessibility, and react-resizable-panels. Same class of problem, different dependency set — out of scope for this change, and tracked separately in #306 (where the case is notably weaker: single consumer, so no duplicated contexts). Zero @base-ui/react remains bundled anywhere, which is the goal that matters here.

2. "A cross-package composition case works." Neither the test suite nor Storybook can verify this: vitest.config.ts and apps/storybook/.storybook/main.ts both alias @uiid/* to source, where only one base-ui instance ever existed. The duplicate-context bug was exclusively a property of the built dist artifacts, so no unit test could have reproduced it and none can prove the fix.

apps/docs is the only surface that consumes built output (no aliases, dependsOn: ["^build"]). It builds and prerenders /overlays/dialog, /overlays/drawer, /overlays/popover, /overlays/toast, and /overlays/tooltip as static pages — real SSG execution against the externalized output. That is meaningful partial evidence, but a deliberate composition repro against built packages would still be worth doing by hand.

Verification

  • /^@base-ui\/react(\/|$)/ present in baseExternal — one line in vite.config.ts
  • All 8 manifests moved base-ui to peerDependencies + devDependencies; diff is 1 removal / 2 additions each, no reformatting
  • No @base-ui/react bundled in any packages/*/dist
  • Emitted dist carries bare @base-ui/react/* imports
  • pnpm install reports no unmet @base-ui/react peer
  • pnpm test:run — 480 passed, 2 skipped, 42 files
  • pnpm run build — 17/17 tasks, including docs:build
  • pnpm size — all packages under budget
  • pnpm run format:check clean; pnpm run lint reports only pre-existing warnings in untouched files
  • Storybook exercised by hand — overlays and forms
  • Cross-package composition repro against built packages (not Storybook — see above)

Note on pnpm size

Numbers are largely unchanged, and should be. size-limit measures "with all dependencies", so it resolves and walks into base-ui either way. The win is in the published artifacts: consumers bundle base-ui once instead of once per @uiid/* package they install.

@base-ui/react was missing from `baseExternal` in vite.config.ts, so rollup
followed into it and inlined it — along with @base-ui/utils, @floating-ui/core,
@floating-ui/react-dom, reselect, and use-sync-external-store — into every
consuming package's dist.

Beyond the size cost, each package shipped its own module instance of Base UI's
context modules (TooltipRootContext, TooltipProviderContext, DirectionContext,
FloatingTree). Two copies of a module mean two React.createContext() calls and
two distinct context objects, so a provider rendered from one package's copy
could not be read by a component resolving another's — composing across @uiid/*
packages could silently fail to share state.

Externalizing @base-ui/react is the half that does the work; the manifest change
only keeps installs correct alongside it.

Measured, from a forced clean rebuild:

  buttons      712K -> 160K
  forms       2968K -> 1808K
  indicators   572K -> 464K
  layout       360K -> 312K
  overlays    1540K -> 624K
  total       7388K -> 4604K   (-2784K, 416 files)

All four duplicated context modules now resolve to a single physical copy under
node_modules/.pnpm, shared by all eight packages.

BREAKING CHANGE: @base-ui/react moves from dependencies to peerDependencies on
@uiid/buttons, forms, indicators, interactive, layout, lists, overlays, and
tables. Consumers must install @base-ui/react themselves. This is deliberate —
it is what guarantees a single deduplicated instance.

Refs #304
@github-actions github-actions Bot added buttons Changes to buttons forms Changes to forms indicators Changes to indicators interactive Changes to interactive layout Changes to layout lists Changes to lists overlays Changes to overlays tables Changes to tables labels Aug 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ Auto-labeled this PR: buttons, forms, indicators, interactive, layout, lists, overlays, tables

@github-actions

Copy link
Copy Markdown
Contributor

Size report (affected packages)

Package Size Limit
✅ @uiid/buttons 45.10 kB —
✅ @uiid/cards 15.66 kB —
✅ @uiid/code 156.04 kB —
✅ @uiid/forms 105.75 kB —
✅ @uiid/indicators 19.50 kB —
✅ @uiid/interactive 93.51 kB —
✅ @uiid/layout 13.63 kB —
✅ @uiid/lists 13.77 kB —
✅ @uiid/navigation 45.39 kB —
✅ @uiid/overlays 71.96 kB —
✅ @uiid/tables 72.08 kB —

Checked 11 affected package(s). Unchanged packages were not rebuilt.

@adamfratino
adamfratino merged commit 512affd into main Aug 20, 2026
6 checks passed
@adamfratino
adamfratino deleted the chore/externalize-base-ui branch August 20, 2026 03:52
adamfratino added a commit that referenced this pull request Aug 20, 2026
Five minor releases (1.3.0 -> 1.7.0, Feb to Aug 2026) across the 27
@base-ui/react modules this repo uses.

Of the three breaking changes declared upstream, only one reaches us:
`Drawer` was unmarked as preview in 1.3.0, renaming the namespace export
`DrawerPreview` -> `Drawer`. 1.7.0 ships no deprecated alias, so the rename is
mandatory — the 11 imports under packages/overlays/src/drawer/subcomponents all
alias to `BaseDrawer`, so only the import specifier changes. The other two
(`sanitizeValue()` -> `normalizeValue()`, `OTPFieldPreview` -> `OTPField`) touch
APIs this repo does not use.

Two undeclared behaviour changes did reach our tests:

- Accordion lost roving focus in 1.6.0 (mui/base-ui#4965), following the APG
  which no longer recommends it (w3c/aria-practices#3434). Arrow keys no longer
  move focus between triggers; Tab does. The test now covers both.
- 1.7.0 appends an invisible Word Joiner (U+2060) to Combobox/Autocomplete empty
  state text for ~200ms so Safari VoiceOver announces the polite live region.
  Exact-string matching broke on the marker; the assertion now matches loosely.

Neither is a defect in this repo — both are upstream corrections our tests were
pinning to the old behaviour.

Verified: 481 tests pass, 17/17 build tasks including docs prerender, all size
budgets green (aggregate 291.27 -> 295.71 kB), format and lint clean. The
externalization from #305 still holds — no base-ui bundled in any dist.

BREAKING CHANGE: the @base-ui/react peer range moves from ^1.2.0 to ^1.7.0.
Consumers on 1.2.x-1.6.x must upgrade. Accordion triggers are no longer a roving
focus group — arrow keys no longer move focus between them, per current APG
guidance.

Closes #303
adamfratino added a commit that referenced this pull request Aug 24, 2026
#305 promoted /^@base-ui\/react(\/|$)/ into baseExternal, which covers
both the bare specifier and subpath imports. The per-package entries in
indicators, interactive, layout, lists, overlays, and tables have been
dead config ever since.

buttons and forms were further out of date, listing
/^@base-ui-components\// — the pre-rename scope, which is not installed
in any manifest and imported by no source file.

Verified as a no-op: after deleting every dist and rebuilding all 17
tasks from scratch, no package emits dist/node_modules, base-ui remains
a bare import wherever it is used, and every pnpm size figure is
unchanged.
adamfratino added a commit that referenced this pull request Aug 24, 2026
…tive (#308)

* fix(ui): externalize @dnd-kit and react-resizable-panels from interactive

@uiid/interactive was the last package emitting a dist/node_modules
directory, vendoring six modules (144K unminified) that a consumer
cannot dedupe, patch, or security-update.

These are externalized but stay ordinary `dependencies` rather than
becoming `peerDependencies`. @base-ui/react needed a peer because eight
packages inlined it and produced duplicate React contexts; nothing here
holds context shared across packages, and @dnd-kit/* and
react-resizable-panels have exactly one consumer. Keeping them as
regular dependencies means consumers install nothing extra — notably
@uiid/tables, which depends on @uiid/interactive only for Menu and
would otherwise inherit five unmet drag-and-drop peers.

Also drops two dead entries: the react-medium-image-zoom dependency
(imported nowhere in the repo) and the /^embla-carousel/ external
(no package depends on embla).

* chore(repo): drop redundant per-package base-ui externals

#305 promoted /^@base-ui\/react(\/|$)/ into baseExternal, which covers
both the bare specifier and subpath imports. The per-package entries in
indicators, interactive, layout, lists, overlays, and tables have been
dead config ever since.

buttons and forms were further out of date, listing
/^@base-ui-components\// — the pre-rename scope, which is not installed
in any manifest and imported by no source file.

Verified as a no-op: after deleting every dist and rebuilding all 17
tasks from scratch, no package emits dist/node_modules, base-ui remains
a bare import wherever it is used, and every pnpm size figure is
unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buttons Changes to buttons forms Changes to forms indicators Changes to indicators interactive Changes to interactive layout Changes to layout lists Changes to lists overlays Changes to overlays tables Changes to tables

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Externalize @base-ui/react — it is bundled into every package's dist

1 participant