Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Configure and use UnoCSS atomic CSS engine with presets like Wind (Tailwind-compatible), Icons, and Attributify.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/transformer-compile-class.md
1---2name: transformer-compile-class3description: Compile multiple classes into one hashed class4---56# Transformer Compile Class78Compiles multiple utility classes into a single hashed class for smaller HTML.910## Installation1112```ts13import { defineConfig, transformerCompileClass } from 'unocss'1415export default defineConfig({16transformers: [17transformerCompileClass(),18],19})20```2122## Usage2324Add `:uno:` prefix to mark classes for compilation:2526```html27<!-- Before -->28<div class=":uno: text-center sm:text-left">29<div class=":uno: text-sm font-bold hover:text-red" />30</div>3132<!-- After -->33<div class="uno-qlmcrp">34<div class="uno-0qw2gr" />35</div>36```3738## Generated CSS3940```css41.uno-qlmcrp {42text-align: center;43}44.uno-0qw2gr {45font-size: 0.875rem;46line-height: 1.25rem;47font-weight: 700;48}49.uno-0qw2gr:hover {50--un-text-opacity: 1;51color: rgb(248 113 113 / var(--un-text-opacity));52}53@media (min-width: 640px) {54.uno-qlmcrp {55text-align: left;56}57}58```5960## Options6162```ts63transformerCompileClass({64// Custom trigger string (default: ':uno:')65trigger: ':uno:',6667// Custom class prefix (default: 'uno-')68classPrefix: 'uno-',6970// Hash function for class names71hashFn: (str) => /* custom hash */,7273// Keep original classes alongside compiled74keepOriginal: false,75})76```7778## Use Cases7980- **Smaller HTML** - Reduce repetitive class strings81- **Obfuscation** - Hide utility class names in production82- **Performance** - Fewer class attributes to parse8384## ESLint Integration8586Enforce compile class usage across project:8788```json89{90"rules": {91"@unocss/enforce-class-compile": "warn"92}93}94```9596This rule:97- Warns when class attribute doesn't start with `:uno:`98- Auto-fixes by adding the prefix99100Options:101102```json103{104"rules": {105"@unocss/enforce-class-compile": ["warn", {106"prefix": ":uno:",107"enableFix": true108}]109}110}111```112113## Combining with Other Transformers114115```ts116export default defineConfig({117transformers: [118transformerVariantGroup(), // Process variant groups first119transformerDirectives(), // Then directives120transformerCompileClass(), // Compile last121],122})123```124125<!--126Source references:127- https://unocss.dev/transformers/compile-class128-->129