fix(shared): load css-tree from its self-contained bundle

Merged
SA
6d ago
+24-1

What and why

Main's Build CLI / Build linux-x64 smoke step has failed since #9970:

error: Cannot find module '../data/patch.json' from '/$bunfs/root/superset'

Cause. #9970 added css-tree@3 to @superset/shared for sanitizeThemeCss. The CLI imports @superset/shared/themes, so the parser loads when the CLI starts. css-tree's ESM entry reads its syntax data at run time with createRequire(import.meta.url) (../data/patch.json, mdn-data/css/*.json). bun build --compile does not embed files loaded that way, so the compiled binary throws on start, also for --version. Browser bundles (the desktop renderer) were not affected: css-tree's browser map sends lib/data.js to dist/data.js, which has the data inlined. Bun with target: "bun" does not use that map. #9970 is a fork PR and had no CI run of its own, so main was the first Build CLI run with it.

Fix. Import from css-tree/dist/csstree.esm. This is css-tree's own self-contained ESM bundle (public ./dist/* export), with the data inlined. A 3-line ambient declaration re-exports the @types/css-tree types for that subpath. Same version, same API, no behaviour change. No revert of #9970.

Regression test. sanitizeThemeCss.test.ts bundles the sanitizer with Bun.build({ target: "bun" }) and runs the output (~20 ms). It fails with the same patch.json error on the old import.

What breaks

Nothing. Released CLIs were not built from this main commit.

Not in this PR

The cli-framework table hyperlink test failures in recent PR runs have a different cause: they fail when the runner sets TERM=dumb (the tests force isTTY but inherit TERM). #9664 fixes that. It applies cleanly to main and passes under TERM=dumb, so this PR does not duplicate it.

How I verified

  • Built the CLI as CI does (bun run build:dist --target=linux-x64, Bun 1.4.3) and ran packages/cli/scripts/smoke-test.sh. On main it fails with the error above. On this branch it prints [smoke] all checks passed.
  • packages/shared: bun test passes (1762 pass, 0 fail). tsc --noEmit is clean for shared, cli and desktop. biome is clean.
  • The new test fails with the fix reverted and passes with it.

Evidence: https://app.superset.sh/page/mainfix-fix-compiled-cli-crash-on-css-tree-data-fi-2pnpef
Design: https://app.superset.sh/page/admin-cloud-workspace-cost-and-health-design-8z3d4d


Summary by cubic

Fixes the compiled CLI crashing on startup because css-tree's ESM entry reads its syntax data at runtime, which bun build --compile does not embed.

  • sanitizeThemeCss now imports css-tree/dist/csstree.esm, a self-contained bundle with the data inlined; no API or behavior change.
  • Adds a regression test that bundles sanitizeThemeCss with Bun.build so the failure surfaces in bun test.

Summary by CodeRabbit

  • Chores
    • Improved theme CSS build compatibility in Bun-based environments. No changes were made to how CSS is sanitized or displayed.