Skip to content

Compile class transformer

คอมไพล์กลุ่มของคลาสเป็นคลาสเดียว ได้รับแรงบันดาลใจจาก compilation mode ของ Windi CSS และ issue #948 โดย @UltraCakeBakery

การติดตั้ง

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

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

TIP

preset นี้รวมอยู่ในแพ็กเกจ unocss คุณสามารถ import จากที่นั่นได้เช่นกัน:

ts
import { transformerCompileClass } from 'unocss'

การใช้งาน

เพิ่ม :uno: ที่จุดเริ่มต้นของ class strings เพื่อทำเครื่องหมายสำหรับการคอมไพล์

ตัวอย่างเช่น:

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

จะถูกคอมไพล์เป็น:

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;
  }
}

ตัวเลือก

คุณสามารถกำหนดค่า trigger string และ prefix สำหรับ compile class ด้วยตัวเลือก ดูรายละเอียดได้ที่ the types

เครื่องมือ

ESLint

มี eslint rule สำหรับบังคับใช้ class compile transformer ทั่วทั้งโปรเจกต์: @unocss/enforce-class-compile

การใช้งาน:

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

License

Released under the MIT License.