Skip to content

perf(css): merge top-level :root blocks in built component CSS#2262

Open
soulfeelings wants to merge 1 commit into
core-ds:masterfrom
soulfeelings:feat/postcss-merge-roots
Open

perf(css): merge top-level :root blocks in built component CSS#2262
soulfeelings wants to merge 1 commit into
core-ds:masterfrom
soulfeelings:feat/postcss-merge-roots

Conversation

@soulfeelings

Copy link
Copy Markdown

Problem

Each component inlines base tokens + its own aliases as separate :root
blocks
, so a consumer's bundled CSS ends up with hundreds of :root blocks.
They inherit into every node, and Chrome DevTools re-renders each one for every
selected/hovered node → the Styles panel freezes (same class of issue as
primefaces/primevue#8309). Production is usually fine only because the prod
minifier merges :root blocks.

Change

Add tools/postcss/postcss-merge-roots.cjs (a sibling of the existing
postcss-remove-empty-root.cjs) that collapses all top-level :root rules
in each emitted file into a single deduped block, preserving source order so
no resolved custom-property value changes
. Registered in the shared
tools/rollup/process-css.mjs chain (right before postcss-remove-empty-root),
so it applies to every build variant.

  • @media-scoped :root is intentionally left intact.
  • No-op when a file has fewer than two top-level :root blocks.

Effect

Fewer :root blocks per emitted file (e.g. button 21 → 9) → DevTools' Styles
pane stays responsive on consumer apps. Backward compatible, idempotent, no
public API change.

Verification

The transform was checked in isolation:

  • merges multiple :root into one, keeping other rules;
  • dedupe is last-value-wins in source order (cascade-faithful);
  • single-:root file is untouched;
  • :root inside @media is not merged.

Notes

  • Changeset: not included — this is a build-tooling change that affects every
    component's emitted CSS, so the right version bump depends on your release
    policy. Happy to add whatever set of bumps you prefer.
  • This is orthogonal to the existing moderncssm / noCommonVars build (it does
    not require or change it); it improves the default published CSS directly.

Each component inlines base tokens plus its own aliases as separate :root
blocks, so a consumer's bundled CSS ends up with hundreds of :root blocks.
They inherit into every node, and Chrome DevTools re-renders each one per
selected/hovered node, freezing the Styles panel (matches primevue#8309).

Add a postcss-merge-roots plugin (next to postcss-remove-empty-root) that
collapses all top-level :root rules in each emitted file into one deduped
block, preserving source order so no resolved value changes. Cuts per-file
:root blocks (e.g. button 21 -> 9). Top-level only; @media-scoped :root is
left intact. Applies to every build variant via the shared postcss chain.
@changeset-bot

changeset-bot Bot commented Jun 30, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 37dece8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Demo build (default)

https://soulfeelings.github.io/core-components/2262

@github-actions

Copy link
Copy Markdown
Contributor

Demo build (alfasans)

https://soulfeelings.github.io/core-components/2262-alfasans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant