Skip to content

Astro 集成

UnoCSS 在 Astro 中的集成:@unocss/astro。查看 示例

安装

bash
pnpm add -D unocss
bash
yarn add -D unocss
bash
npm install -D unocss
bash
bun add -D unocss
astro.config.ts
ts
import { defineConfig } from 'astro/config'
import UnoCSS from 'unocss/astro'

export default defineConfig({
  integrations: [
    UnoCSS(),
  ],
})

创建一个 uno.config.ts 文件:

uno.config.ts
ts
import { defineConfig } from 'unocss'

export default defineConfig({
  // ...UnoCSS 选项
})

样式重置

默认情况下,浏览器样式重置 不会被注入。要启用它,请安装 @unocss/reset 包:

bash
pnpm add -D @unocss/reset
bash
yarn add -D @unocss/reset
bash
npm install -D @unocss/reset
bash
bun add -D @unocss/reset

并更新您的 astro.config.ts

astro.config.ts
ts
import { defineConfig } from 'astro/config'
import UnoCSS from 'unocss/astro'

export default defineConfig({
  integrations: [
    UnoCSS({
      injectReset: true // 或重置文件的路径
    }),
  ],
})

无预设的使用

该插件不带任何默认的预设。

bash
pnpm add -D @unocss/astro
bash
yarn add -D @unocss/astro
bash
npm install -D @unocss/astro
bash
bun add -D @unocss/astro
astro.config.mjs
ts
import UnoCSS from '@unocss/astro'

export default {
  integrations: [
    UnoCSS(),
  ],
}

有关更多详细信息,请参阅 Vite 插件

INFO

如果您正在 UnoCSS 上构建一个元框架,请查看 这个文件 以获取如何绑定默认预设的示例。

注意事项

client:only 组件必须放在 components 文件夹中或添加到 UnoCSS 的 content 配置中,以便进行处理。