Skip to content

编译类转换器

¥Compile class transformer

将一组类编译为一个类。灵感来自 Windi CSS 的 编译模式@UltraCakeBakery问题 #948

¥Compile group of classes into one class. Inspired by the compilation mode of Windi CSS and issue #948 by @UltraCakeBakery.

安装

¥Installation

bash
pnpm add -D @unocss/transformer-compile-class
bash
yarn add -D @unocss/transformer-compile-class
bash
npm install -D @unocss/transformer-compile-class
uno.config.ts
ts
import transformerCompileClass from '@unocss/transformer-compile-class'
import { defineConfig } from 'unocss'

export default defineConfig({
  // ...
  transformers: [
    transformerCompileClass(),
  ],
})

提示

该预设包含在 unocss 包中,你也可以从那里导入它:

¥This preset is included in the unocss package, you can also import it from there:

ts
import { transformerCompileClass } from 'unocss'

用法

¥Usage

在类字符串的开头添加 :uno: 以将其标记为要编译。

¥Add :uno: at the beginning of the class strings to mark them for compilation.

例如:

¥For example:

html
<div class=":uno: text-center sm:text-left">
  <div class=":uno: text-sm font-bold hover:text-red"/>
</div>

将被编译为:

¥Will be compiled to:

html
<div class="uno-qlmcrp">
  <div class="uno-0qw2gr"/>
</div>
css
.uno-qlmcrp {
  text-align: center;
}
.uno-0qw2gr {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
}
.uno-0qw2gr:hover {
  --un-text-opacity: 1;
  color: rgb(248 113 113 / var(--un-text-opacity));
}
@media (min-width: 640px) {
  .uno-qlmcrp {
    text-align: left;
  }
}

选项

¥Options

你可以使用选项配置编译类的触发字符串和前缀。详情请参阅 类型

¥You can config the trigger string and prefix for compile class with the options. Refer to the types for details.

工具

¥Tooling

ESLint

有一个 eslint 规则用于在整个项目中强制执行类编译转换器:@unocss/enforce-class-compile

¥There is an eslint rule for enforcing the class compile transformer across the whole project: @unocss/enforce-class-compile

用法:

¥Usage:

json
{
  "plugins": ["@unocss"],
  "rules": {
    "@unocss/enforce-class-compile": "warn"
  }
}

许可

¥License