Skip to content

build(core): precise 'use client' boundaries via 1:1 file transpilation - #2472

Merged
imdreamrunner merged 1 commit into
mainfrom
build/babel-cli-esm-only
Jun 3, 2026
Merged

imdreamrunner merged 1 commit into
mainfrom
build/babel-cli-esm-only

Conversation

@imdreamrunner

@imdreamrunner imdreamrunner commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The current tsup/esbuild build bundles components into shared chunks. esbuild doesn't understand React's 'use client' directive — it treats it as a dead expression and drops it. Two postbuild scripts attempt to restore it, but they only fix barrel entry points, not the chunks where the actual component code lives.

This means every component is effectively marked as a client component at the barrel level, regardless of whether the component actually needs client-side features. Consumers can't import server-safe utilities or types from @xds/core without hitting a client boundary.

Solution

Replace tsup (esbuild bundling + code splitting) with Babel CLI for 1:1 file transpilation. Each source file maps to exactly one output file. 'use client' is preserved naturally on only the files that declare it — components that use hooks/state get the directive, pure utilities and types don't.

This gives consumers precise control over where client boundaries fall.

Changes

  • Babel CLI replaces tsup — StyleX is already a Babel plugin, so this removes a layer of indirection (esbuild → esbuild-plugin-babel → Babel → StyleX). Now it's just Babel → StyleX directly.
  • ESM-only output — CJS (require()) is not used by modern frameworks (Next.js, Vite, Remix all resolve ESM). Dropping it halves the JS build output and simplifies the pipeline.
  • Delete postbuild fixup scripts — postbuild-use-client.mjs and fix-use-client-dist.mjs existed solely to compensate for esbuild stripping directives. No longer needed.
  • @stylexjs/stylex moves to dependencies — previously bundled into every chunk via noExternal. Now it's a standard import that consumers' bundlers deduplicate naturally.

Verification

  • 447 files compiled in ~5s
  • 346 source files with 'use client' → 346 dist files with 'use client' (1:1 match)
  • 101 dist files correctly without 'use client' (types, stylex vars, pure utils)
  • Zero chunk files in dist
  • Next.js docsite build passes (RSC boundaries validated end-to-end)
  • StyleX compilation verified (class name objects in output)

Test plan

  • pnpm build succeeds
  • 'use client' count matches between src and dist
  • dist/Button/XDSButton.mjs has 'use client' (previously lost in a chunk)
  • No chunk-* files in dist
  • Next.js docsite pnpm build passes
  • Storybook renders components correctly
  • Full test suite passes

@vercel

vercel Bot commented Jun 1, 2026

Copy link
Copy Markdown

@imdreamrunner must be a member of the Meta Open Source team on Vercel to deploy.
- Click here to add @imdreamrunner to the team.
- If you initiated this build, request access.

Learn more about collaboration on Vercel and other options here.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 1, 2026
@vercel

vercel Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
xds-sandbox Error Error Jun 3, 2026 6:15pm

Request Review

@github-actions

github-actions Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Vercel Preview Deployment

Status ✅ Deployed
Preview Open Preview
Commit 200f1d0
Inspect Vercel Dashboard
Workflow View Logs

No authentication required — anyone with the link can view the preview.

@github-actions

github-actions Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core N/A 3.7KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@imdreamrunner
imdreamrunner marked this pull request as ready for review June 2, 2026 00:01
@imdreamrunner

Copy link
Copy Markdown
Contributor Author

Bundle Size Analysis: Before vs After

Per-component (top 15 by size)

Component Before (tsup) After (Babel)
Table 120 KB 223 KB
PowerSearch 113 KB 201 KB
Markdown 80.1 KB 151 KB
CommandPalette 76.2 KB 147 KB
Chat 65.7 KB 129 KB
DateTimeInput 63.7 KB 119 KB
DateInput 62.3 KB 118 KB
TopNav 57.4 KB 112 KB
SideNav 59.4 KB 111 KB
Pagination 56.1 KB 103 KB
Button 33.7 KB 64.2 KB
Dialog 46.5 KB 87.4 KB
Text 15.6 KB 23.5 KB
Badge 12.4 KB 18.1 KB
SizeContext 379B 914B

Scenarios (deduplicated)

Scenario Before After Delta
Small app (5 components) 75.9 KB 127 KB +67%
Medium app (15 components) 164 KB 287 KB +75%
Full library (96 components) 367 KB 624 KB +70%

Why the numbers are larger

The raw numbers are larger because the two builds measure different things:

Before (tsup/esbuild): esbuild deduplicated shared code into chunks and gzipped each chunk individually. Shared utilities, hooks, and theme code existed once in a chunk file, so they were counted once.

After (Babel 1:1): Each source file is its own output file. When measuring a component's "solo" size, we sum all files in its dependency tree — including shared dependencies like theme, hooks, and Layout that are also used by other components. These shared files get counted in every component that imports them.

This does not affect the consumer's actual bundle size. The consumer's bundler (Next.js, Vite, webpack) deduplicates at build time regardless — a file imported by 10 components is included once in the final app bundle. The "dedup" just moved from library build time to consumer build time.

Why this is actually better for consumers

  1. Better tree-shaking. With 1:1 files, the consumer's bundler can drop unused exports at the file level. With chunks, unrelated code could be bundled together, making it harder to eliminate.

  2. Precise 'use client' boundaries. Each component file carries its own 'use client' directive (or not). Previously, the directive was only on barrel re-exports while the actual component code lived in chunks without it.

  3. No duplicate runtime. @stylexjs/stylex was previously bundled into the dist (~5KB). Now it's an external dependency — consumers who also use StyleX directly share one copy instead of getting a duplicate.

…ndaries

Switch from tsup/esbuild bundling (which produced shared chunks that
lost 'use client' directives) to Babel CLI 1:1 file transpilation.
Each source file maps to exactly one output file, so 'use client'
is preserved on the actual component files — not just barrel re-exports.

- ESM-only output with "type": "module" and .js extension
- Update sync-exports.js for ESM-only export conditions
- Move @stylexjs/stylex to dependencies (no longer bundled in)
- Delete tsup.config.ts, postbuild-use-client.mjs, fix-use-client-dist.mjs
- Fix babel.config.json: add runtime: "automatic" for JSX transform
@imdreamrunner
imdreamrunner merged commit 8b48555 into main Jun 3, 2026
20 of 21 checks passed
@kentonquatman

Copy link
Copy Markdown
Contributor

Hey @imdreamrunner, it looks like this PR broke how typography is rendering.

imdreamrunner added a commit that referenced this pull request Jun 4, 2026
#2472 made @xds/core ESM-only ("type": "module"), which broke the CLI's
build-theme.mjs. require() fails on ESM-only packages, and the empty
catch {} silently swallowed the error, causing generateThemeRulesSplit
to be null. Theme generation fell back to a buggy code path that emitted
prose heading resets into @layer xds-theme instead of @layer reset,
breaking display-1 headings (42px → 24px).

Fixes:
- Add babel-plugin-add-extensions.cjs to rewrite extensionless relative
  imports with .js extensions during Babel compilation. Handles both
  file imports ('./XDSButton' → './XDSButton.js') and directory imports
  ('./domainTokens' → './domainTokens/index.js'). This replaces the
  postbuild-add-extensions.mjs script with a proper Babel plugin.
- Switch build-theme.mjs from require() to dynamic import() and
  surface errors instead of silently swallowing them.
imdreamrunner added a commit that referenced this pull request Jun 4, 2026
#2472 made @xds/core ESM-only ("type": "module"), which broke the CLI's
build-theme.mjs. require() fails on ESM-only packages, and the empty
catch {} silently swallowed the error, causing generateThemeRulesSplit
to be null. Theme generation fell back to a buggy code path that emitted
prose heading resets into @layer xds-theme instead of @layer reset,
breaking display-1 headings (42px → 24px).

Fixes:
- Add babel-plugin-add-extensions.cjs to rewrite extensionless relative
  imports with .js extensions during Babel compilation. Handles both
  file imports ('./XDSButton' → './XDSButton.js') and directory imports
  ('./domainTokens' → './domainTokens/index.js'). This replaces the
  postbuild-add-extensions.mjs script with a proper Babel plugin.
- Switch build-theme.mjs from require() to dynamic import() and
  surface errors instead of silently swallowing them.
@imdreamrunner

Copy link
Copy Markdown
Contributor Author

Hey @imdreamrunner, it looks like this PR broke how typography is rendering.

thanks for flagging.

fix in #2512

imdreamrunner added a commit that referenced this pull request Jun 4, 2026
#2472 made @xds/core ESM-only ("type": "module"), which broke the CLI's
build-theme.mjs. require() fails on ESM-only packages, and the empty
catch {} silently swallowed the error, causing generateThemeRulesSplit
to be null. Theme generation fell back to a buggy code path that emitted
prose heading resets into @layer xds-theme instead of @layer reset,
breaking display-1 headings (42px → 24px).

Fixes:
- Add babel-plugin-add-extensions.cjs to rewrite extensionless relative
  imports with .js extensions during Babel compilation. Handles both
  file imports ('./XDSButton' → './XDSButton.js') and directory imports
  ('./domainTokens' → './domainTokens/index.js'). This replaces the
  postbuild-add-extensions.mjs script with a proper Babel plugin.
- Switch build-theme.mjs from require() to dynamic import() and
  surface errors instead of silently swallowing them.
cixzhang pushed a commit that referenced this pull request Jun 21, 2026
…ndaries (#2472)

Switch from tsup/esbuild bundling (which produced shared chunks that
lost 'use client' directives) to Babel CLI 1:1 file transpilation.
Each source file maps to exactly one output file, so 'use client'
is preserved on the actual component files — not just barrel re-exports.

- ESM-only output with "type": "module" and .js extension
- Update sync-exports.js for ESM-only export conditions
- Move @stylexjs/stylex to dependencies (no longer bundled in)
- Delete tsup.config.ts, postbuild-use-client.mjs, fix-use-client-dist.mjs
- Fix babel.config.json: add runtime: "automatic" for JSX transform
cixzhang pushed a commit that referenced this pull request Jun 21, 2026
#2472 made @xds/core ESM-only ("type": "module"), which broke the CLI's
build-theme.mjs. require() fails on ESM-only packages, and the empty
catch {} silently swallowed the error, causing generateThemeRulesSplit
to be null. Theme generation fell back to a buggy code path that emitted
prose heading resets into @layer xds-theme instead of @layer reset,
breaking display-1 headings (42px → 24px).

Fixes:
- Add babel-plugin-add-extensions.cjs to rewrite extensionless relative
  imports with .js extensions during Babel compilation. Handles both
  file imports ('./XDSButton' → './XDSButton.js') and directory imports
  ('./domainTokens' → './domainTokens/index.js'). This replaces the
  postbuild-add-extensions.mjs script with a proper Babel plugin.
- Switch build-theme.mjs from require() to dynamic import() and
  surface errors instead of silently swallowing them.
cixzhang pushed a commit that referenced this pull request Jun 21, 2026
…ndaries (#2472)

Switch from tsup/esbuild bundling (which produced shared chunks that
lost 'use client' directives) to Babel CLI 1:1 file transpilation.
Each source file maps to exactly one output file, so 'use client'
is preserved on the actual component files — not just barrel re-exports.

- ESM-only output with "type": "module" and .js extension
- Update sync-exports.js for ESM-only export conditions
- Move @stylexjs/stylex to dependencies (no longer bundled in)
- Delete tsup.config.ts, postbuild-use-client.mjs, fix-use-client-dist.mjs
- Fix babel.config.json: add runtime: "automatic" for JSX transform
cixzhang pushed a commit that referenced this pull request Jun 21, 2026
#2472 made @xds/core ESM-only ("type": "module"), which broke the CLI's
build-theme.mjs. require() fails on ESM-only packages, and the empty
catch {} silently swallowed the error, causing generateThemeRulesSplit
to be null. Theme generation fell back to a buggy code path that emitted
prose heading resets into @layer xds-theme instead of @layer reset,
breaking display-1 headings (42px → 24px).

Fixes:
- Add babel-plugin-add-extensions.cjs to rewrite extensionless relative
  imports with .js extensions during Babel compilation. Handles both
  file imports ('./XDSButton' → './XDSButton.js') and directory imports
  ('./domainTokens' → './domainTokens/index.js'). This replaces the
  postbuild-add-extensions.mjs script with a proper Babel plugin.
- Switch build-theme.mjs from require() to dynamic import() and
  surface errors instead of silently swallowing them.

This branch had an error being deployed

1 failed deployment
Preview — 200f1d00 Deployed Jun 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants