Processors
Processors are hooks that transform generated CSS. Unlike transformers, which modify source code before extraction, processors run after UnoCSS has generated its CSS layers.
Define a processor
A processor receives the CSS for one layer and returns the CSS that should replace it. Both synchronous and asynchronous results are supported.
import type { CSSProcessor } from '@unocss/core'
import { defineConfig } from 'unocss'
const banner: CSSProcessor = {
name: 'add-banner',
order: 10,
process(css, { layer, envMode }) {
if (envMode !== 'build')
return css
return `/* generated layer: ${layer} */\n${css}`
},
}
export default defineConfig({
processors: [banner],
})Processing flow
For every non-empty CSS layer, UnoCSS performs these steps:
- Generate the raw layer CSS, including preflights and any enabled CSS layer wrapper or layer marker.
- Sort processors by
orderin ascending order. - Pass the layer through each processor sequentially. The output of one processor becomes the input of the next.
- Cache the processed layer and expose it through
getLayer(),getLayers(), andcss.
Different layers may be processed concurrently. A processor should avoid relying on mutable state shared between layers.
When setLayer() changes a layer, its callback receives the raw, unprocessed CSS. UnoCSS then runs the updated CSS through the complete processor chain again. This prevents processors from being applied repeatedly to their own previous output.
generated layer
-> processor 1
-> processor 2
-> processed layer outputIf a processor throws an error, generation fails and the error is passed to the caller.
Context
The second argument passed to process() is a CSSProcessorContext:
interface CSSProcessorContext<Theme extends object = object> {
layer: string
theme: Theme
envMode: 'dev' | 'build'
}layeris the name of the current generated layer.themeis the resolved UnoCSS theme.envModeindicates whether UnoCSS is generating CSS for development or production builds.
Processor order
Processors with a lower order run first. Processors without an explicit order use 0.
processors: [
{ name: 'minify', order: 20, process: minify },
{ name: 'prefix', order: 10, process: addPrefixes },
]In this example, prefix runs before minify.
Processors declared by presets and the user configuration are merged. The processor name identifies it when duplicate processors are removed.