Repository navigation
build(core): precise 'use client' boundaries via 1:1 file transpilation - #2472
Conversation
|
@imdreamrunner must be a member of the Meta Open Source team on Vercel to deploy. Learn more about collaboration on Vercel and other options here. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🚀 Vercel Preview Deployment
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR No new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
dc73b58 to
e2d54e9
Compare
e2d54e9 to
8ba4e12
Compare
8ba4e12 to
3d57ad5
Compare
3d57ad5 to
68b05d4
Compare
68b05d4 to
228b863
Compare
228b863 to
8361e40
Compare
8361e40 to
1eb5953
Compare
Bundle Size Analysis: Before vs AfterPer-component (top 15 by size)
Scenarios (deduplicated)
Why the numbers are largerThe 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
|
…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
1eb5953 to
200f1d0
Compare
|
Hey @imdreamrunner, it looks like this PR broke how typography is rendering. |
#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.
#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.
thanks for flagging. fix in #2512 |
#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.
…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
#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.
…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
#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.
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/corewithout 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
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.postbuild-use-client.mjsandfix-use-client-dist.mjsexisted solely to compensate for esbuild stripping directives. No longer needed.@stylexjs/stylexmoves todependencies— previously bundled into every chunk vianoExternal. Now it's a standard import that consumers' bundlers deduplicate naturally.Verification
'use client'→ 346 dist files with'use client'(1:1 match)'use client'(types, stylex vars, pure utils)Test plan
pnpm buildsucceeds'use client'count matches between src and distdist/Button/XDSButton.mjshas'use client'(previously lost in a chunk)chunk-*files in distpnpm buildpasses