Skip to content

fix(core,cli): fix Node.js ESM resolution for CLI theme build - #2512

Merged
imdreamrunner merged 1 commit into
mainfrom
fix/esm-cli-compat
Jun 4, 2026
Merged

imdreamrunner merged 1 commit into
mainfrom
fix/esm-cli-compat

Conversation

@imdreamrunner

@imdreamrunner imdreamrunner commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

#2472 made @xds/core ESM-only ("type": "module"), which broke the CLI's theme build command. The causal chain:

  1. build-theme.mjs used require('@xds/core/theme') to load theme generation utilities
  2. require() fails on ESM-only packages → ERR_MODULE_NOT_FOUND
  3. The catch {} silently swallowed the error → generateThemeRulesSplit = null
  4. Theme generation fell back to a buggy code path that emits prose heading resets into @layer xds-theme instead of @layer reset
  5. @layer xds-theme outranks @layer xds-base, so the bare h1 { font-size: 24px } rule overrides StyleX's display-1 class → headings collapse to 24px

Fix

  1. Switch require() to await import() in build-theme.mjs — the file is already .mjs, so dynamic import() is the natural ESM approach
  2. Surface import errors instead of silently swallowing them — catch (e) { console.warn(...) } instead of catch {}
  3. Add postbuild-add-extensions.mjs — Babel CLI preserves extensionless imports from TypeScript source (e.g. './XDSButton'). Bundlers resolve these fine, but Node.js native ESM requires explicit extensions for "type": "module" packages. This script rewrites them to './XDSButton.js' after the build.

Test plan

  • pnpm build succeeds with extension rewriting (340 of 447 files)
  • xds theme build loads @xds/core/theme without warnings
  • Generated astryx.css has prose resets in @layer reset (not @layer xds-theme)
  • No h1 rules in @layer xds-theme (confirmed via awk grep)
  • Visual verification (main vs fix branch):
    • Main (broken): docsite hero heading "An open source design system..." renders at 24px — display-1 StyleX class overridden by h1 reset in wrong @layer xds-theme
    • Fix branch: same heading renders at 42px — prose resets correctly in @layer reset, StyleX display-1 class wins

@vercel

vercel Bot commented Jun 4, 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 4, 2026
@github-actions

github-actions Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Vercel Preview Deployment

Status ✅ Deployed
Preview Open Preview
Commit 5218e42
Inspect Vercel Dashboard
Workflow View Logs

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

@github-actions

github-actions Bot commented Jun 4, 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 4.5KB 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 draft June 4, 2026 00:22
@vercel

vercel Bot commented Jun 4, 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 4, 2026 1:33am

Request Review

#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
imdreamrunner marked this pull request as ready for review June 4, 2026 03:48
@imdreamrunner
imdreamrunner merged commit e46e1bd into main Jun 4, 2026
20 of 21 checks passed
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
#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
cixzhang deleted the fix/esm-cli-compat branch June 21, 2026 16:55

This branch had an error being deployed

1 failed deployment
Preview — 5218e423 Deployed Jun 4, 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.

2 participants