astro-uno
astro-uno copied to clipboard
Astro UnoCSS integration
Astro UnoCSS Integration
:warning: Archive Notice
This repository is archived because UnoCSS comes with its own Astro integration (unocss/astro) and this third-party integration is no longer needed.
Usage
- Install this package and UnoCSS
npm install --save-dev astro-uno unocss
- Add to the integrations in
astro.config.mjs
import uno from 'astro-uno'
import { presetUno } from 'unocss'
export default defineConfig({
integrations: [
uno({
presets: [presetUno()]
})
]
}
- Change the scripts to allow experimental integrations
{
"scripts": {
"dev": "astro dev --experimental-integrations",
"start": "astro dev --experimental-integrations",
"build": "astro build --experimental-integrations",
}
}
- Import UnoCSS in your
.astrofiles
import 'unocss-hmr-fix'
See examples/ for detail.
What it does
-
Creates an alias of
uno.cssto avoid HMR issues.unocss-hmr-fixis just an alias ofuno.css. But the renaming fixes some HMR issues, since Astro treats everything ends with '.css' as a normal CSS file. -
Force UnoCSS to run at the SSR phase.
UnoCSS skips running when in the SSR mode, because it assumes that there is another client build. But that's not true for Astro, where the client build doesn't include all the sources and the styles are generated in the SSR phase. So we need to force UnoCSS to run at the SSR phase.
-
Fix regular expression matching for attributify mode.
Astro pre-processes the
.astrofiles and transform them into something like:return $$render`<img${$$addAttribute(src, "src")} w-full>`;UnoCSS expected a space after
<imgbut Astro gives$$addAttributepart instead, making attributify mode not working well. Here we pass modified code with an extra space to UnoCSS for extracting the tokens.
Extra configs
interface UnoIntegrationConfig extends VitePluginConfig {
astro?: {
/**
* Whether to auto import UnoCSS
* @default false
*/
autoImport?: boolean
}
}