Skip to content

postcss-root-to-host work in progress ​

A PostCSS plugin from @frontpack/widget-build-tools that rewrites :root selectors to :host.

Do you need this? ​

  • ✅ Your widget imports stylesheets that define custom properties on :root (e.g. tokens from @nessie/web-fundamentals).
  • ✅ Your widget uses ViewEncapsulation.ShadowDom. Required for encapsulated widgets.

What problem does it solve? ​

:root selectors never match inside a shadow tree. Design-token stylesheets that widgets import, define their custom properties on :root, which works fine in a normal page but is silently dropped once that same stylesheet is injected into a component using ViewEncapsulation.ShadowDom. The widget then renders without its design tokens and/or the tokens will conflict with the hosting app.

How to install it ​

bash
npm install --save @frontpack/widget-build-tools

How to use it ​

Add it to postcss.config.json, scoped to the entry stylesheet(s) of the shadow-DOM components that @import design tokens:

json
// postcss.config.json
{
  "plugins": {
    "@frontpack/widget-build-tools/postcss-root-to-host": {
      "entryPattern": "src/app/components/.*\\.css$"
    }
  }
}

The plugin rewrites :root to :host in any entry stylesheet whose path matches entryPattern (a regular expression, as a string), so the custom properties become available inside the shadow root. Omit entryPattern to rewrite every stylesheet processed by PostCSS instead.

WARNING

Scope entryPattern deliberately. A widget's global stylesheet (e.g. styles.css) usually still relies on :root targeting the real document root (used for local development and app webviews), and should not be rewritten.