Skip to content

PostCSS 插件

¥PostCSS Plugin

UnoCSS 的 PostCSS 插件。支持 @apply@screentheme() 指令。

¥PostCSS plugin for UnoCSS. Supports @apply, @screen and theme() directives.

源代码

¥Source Code

警告

该软件包目前处于实验状态。它不遵循 semver,并且可能会在补丁版本中引入重大更改。

¥This package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions.

安装

¥Install

bash
pnpm add -D @unocss/postcss
bash
yarn add -D @unocss/postcss
bash
npm install -D @unocss/postcss
ts
// postcss.config.cjs
module.exports = {
  plugins: {
    '@unocss/postcss': {},
  },
}
ts
// uno.config.ts
import { defineConfig, presetUno } from 'unocss'

export default defineConfig({
  content: {
    filesystem: [
      '**/*.{html,js,ts,jsx,tsx,vue,svelte,astro}',
    ],
  },
  presets: [
    presetUno(),
  ],
})
css
/* style.css */
@unocss;

用法

¥Usage

@unocss

@unocss at-rule 是一个占位符。它将被生成的 CSS 替换。

¥@unocss at-rule is a placeholder. It will be replaced by the generated CSS.

你还可以单独注入每一层:

¥You can also inject each layer individually:

css
/* style.css */
@unocss preflights;
@unocss default;

/*
  Fallback layer. It's always recommended to include.
  Only unused layers will be injected here.
*/
@unocss;

如果要包含所有图层,无论之前是否包含,可以使用 @unocss all。如果你想将生成的 CSS 包含在多个文件中,这非常有用。

¥If you want to include all layers no matter whether they are previously included or not, you can use @unocss all. This is useful if you want to include generated CSS in multiple files.

css
@unocss all;

@apply

css
.custom-div {
  @apply text-center my-0 font-medium;
}

将转变为:

¥Will be transformed to:

css
.custom-div {
  margin-top: 0rem;
  margin-bottom: 0rem;
  text-align: center;
  font-weight: 500;
}

@screen

@screen 指令允许你创建通过来自 theme.breakpoints 的名称引用断点的媒体查询。

¥The @screen directive allows you to create media queries that reference your breakpoints by name comes from theme.breakpoints.

css
.grid {
  @apply grid grid-cols-2;
}
@screen xs {
  .grid {
    @apply grid-cols-1;
  }
}
@screen sm {
  .grid {
    @apply grid-cols-3;
  }
}
/* ... */
...

将转变为:

¥Will be transformed to:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (min-width: 640px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
/* ... */

断点变体支持

¥Breakpoint Variant Support

@screen 还支持 ltat 变种

¥@screen also supports ltat variants

@screen lt
css
.grid {
  @apply grid grid-cols-2;
}
@screen lt-xs {
  .grid {
    @apply grid-cols-1;
  }
}
@screen lt-sm {
  .grid {
    @apply grid-cols-3;
  }
}
/* ... */

将转变为:

¥Will be transformed to:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 319.9px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (max-width: 639.9px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
/* ... */
@screen at
css
.grid {
  @apply grid grid-cols-2;
}
@screen at-xs {
  .grid {
    @apply grid-cols-1;
  }
}
@screen at-xl {
  .grid {
    @apply grid-cols-3;
  }
}
@screen at-xxl {
  .grid {
    @apply grid-cols-4;
  }
}
/* ... */

将转变为:

¥Will be transformed to:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) and (max-width: 639.9px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (min-width: 1280px) and (max-width: 1535.9px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
@media (min-width: 1536px) {
  .grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
/* ... */

theme()

使用 theme() 函数通过点表示法访问主题配置值。

¥Use the theme() function to access your theme config values using dot notation.

css
.btn-blue {
  background-color: theme('colors.blue.500');
}

将被编译为:

¥Will be compiled to:

css
.btn-blue {
  background-color: #3b82f6;
}