Stylesheet Generation
When to Use
Use this guide when understanding how UI Styles emits an optimized stylesheet — useful for performance tuning and debugging missing CSS.
Decision
| Goal | Action |
|---|---|
| Reduce CSS size in production | Rely on the generator — only used UI Styles classes ship |
| Keep all utility classes available regardless of UI Styles options | Skip the generator; load utilities via your normal library |
Pattern
The ui_styles.stylesheet_generator service (StylesheetGenerator):
- Collects all CSS files from theme and module libraries
- Parses each with Sabberworm PHP CSS Parser
- Filters out every CSS rule whose selector doesn't match a class declared in any active UI Styles option
- Outputs a single optimized stylesheet served via
StylesheetController - Caches the result; invalidates when style definitions or libraries change
The generated stylesheet is attached to every page via hook_page_top() as ui_styles/generated_styles.
Common Mistakes
- Editing CSS files but not seeing changes → Clear the
ui_stylescache discovery and the page cache; the generator caches output - Composer not having
php-css-parser→ The generator may fail on certain CSS edge cases. Confirmsabberworm/php-css-parser ^9.0is installed
See Also
- Installation
- Code Reference
- Reference:
src/Service/StylesheetGenerator.phpin the UI Styles module