Skip to content

Astro 集成

Astro 提供的 UnoCSS 集成:@unocss/astro。查看 示例

安装

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

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

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

ts
// uno.config.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

并更新你的 astro.config.ts

ts
// astro.config.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
ts
// astro.config.mjs
import UnoCSS from '@unocss/astro'

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

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

INFO

如果你正在构建基于 UnoCSS 的元框架,请参考 此文件,查看如何绑定默认预设的示例。

注意事项

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

Released under the MIT License.