--- name: unocss-extracting description: How UnoCSS extracts utilities from source code --- # Extracting UnoCSS searches for utility usages in your codebase and generates CSS on-demand. ## Content Sources ### Pipeline Extraction (Vite/Webpack) Most efficient - extracts from build tool pipeline. **Default file types:** `.jsx`, `.tsx`, `.vue`, `.md`, `.html`, `.svelte`, `.astro`, `.marko` **Not included by default:** `.js`, `.ts` ```ts export default defineConfig({ content: { pipeline: { include: [ /\.(vue|svelte|[jt]sx|mdx?|astro|html)($|\?)/, 'src/**/*.{js,ts}', // Add js/ts ], }, }, }) ``` ### Filesystem Extraction For files not in build pipeline: ```ts export default defineConfig({ content: { filesystem: [ 'src/**/*.php', 'public/*.html', ], }, }) ``` ### Inline Text Extraction ```ts export default defineConfig({ content: { inline: [ '
Extracted
NOT extracted
``` ## Limitations UnoCSS works at **build time** - dynamic classes don't work: ```html ``` ### Solutions **1. Safelist** - Pre-generate known values: ```ts safelist: ['p-1', 'p-2', 'p-3', 'p-4'] ``` **2. Static mapping** - List combinations statically: ```ts const colors = { red: 'text-red border-red', blue: 'text-blue border-blue', } ``` **3. Runtime** - Use `@unocss/runtime` for true runtime generation. ## Custom Extractors ```ts extractors: [ { name: 'my-extractor', extract({ code }) { return code.match(/class:[\w-]+/g) || [] }, }, ] ```