コンパイルクラストランスフォーマー
クラスのグループを1つのクラスにコンパイルします。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
ts
import transformerCompileClass from '@unocss/transformer-compile-class'
import { defineConfig } from 'unocss'
export default defineConfig({
// ...
transformers: [
transformerCompileClass(),
],
})
ヒント
このプリセットはunocss
パッケージに含まれており、そこからインポートすることもできます。
ts
import { transformerCompileClass } from 'unocss'
使い方
コンパイル対象としてマークするために、クラス文字列の先頭に:uno:
を追加します。
例
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;
}
}
オプション
オプションで、コンパイルクラスのトリガー文字列とプレフィックスを設定できます。詳しくは、型定義を参照してください。
ツール
ESLint
プロジェクト全体でクラスコンパイルトランスフォーマーを強制するためのeslintルールがあります: @unocss/enforce-class-compile
使い方
json
{
"plugins": ["@unocss"],
"rules": {
"@unocss/enforce-class-compile": "warn"
}
}
ライセンス
- MIT License © 2021-PRESENT Anthony Fu