unplugin-attributify-to-class

English | 简体中文

[![Version](https://img.shields.io/npm/v/unplugin-attributify-to-class.svg?style=flat-square&logo=npm) ![Downloads](https://img.shields.io/npm/dm/unplugin-unocss-attributify-wechat.svg?style=flat-square&logo=npm)](https://www.npmjs.com/package/unplugin-attributify-to-class) collect and add atomized css [attributify mode](https://github.com/unocss/unocss/tree/main/packages/preset-attributify#attributify-mode) to class , support the use of attributify mode in miniprogram to fit the functional semantics,`unplugin-unocss-attributify-wechat` rename to `unplugin-attributify-to-class` --- ## Why use it use `@unocss/preset-attributify` ```html ``` generated css ```css [bg~="blue-400"] { --un-bg-opacity: 1; background-color: rgba(96,165,250,var(--un-bg-opacity)); } ``` miniprogram does not support property selectors [bg~="blue-400"] ,[wechat documents](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#%E7%BB%84%E4%BB%B6%E6%A0%B7%E5%BC%8F) --- use this plugin ```html ``` transform ```html ``` --- ## Installation ```shell npm i -D unplugin-attributify-to-class ```
vite
```ts import { defineConfig } from 'vite' import { attributifyToClass, defaultAttributes, defaultIgnoreNonValuedAttributes } from 'unplugin-attributify-to-class/vite' export default defineConfig({ plugins: [ // https://github.com/MellowCo/unplugin-attributify-to-class attributifyToClass(), ], }) ```
webpack
```js const { defaultAttributes, defaultIgnoreNonValuedAttributes, attributifyToClass } = require('unplugin-attributify-to-class/webpack') module.exports = { configureWebpack: { plugins: [ // https://github.com/MellowCo/unplugin-attributify-to-class attributifyToClass(), ], }, } ```
## Usage ### Options ```ts export interface Options { /** * @default 'un-' */ prefix?: string /** * Only match for prefixed attributes * @default false */ prefixedOnly?: boolean /** * A list of attributes to transform class * @default ['bg', 'flex', 'grid', 'border', 'text', 'font', 'class', 'className', 'p', 'm', 'animate'] */ attributes?: string[] /** * A list of non-valued attributes to be ignored from extracting * @default ['class'] */ ignoreNonValuedAttributes?: string[] /** * Support matching non-valued attributes * * For example * ```html *
* ``` * * @default true */ nonValuedAttribute?: boolean /** * Transform escape char like [ # $ . , * @default true */ transformEscape?: boolean /** * Custom transform Rules for escape char * @default https://github.com/MellowCo/unplugin-transform-class#options */ transformRules?: Record /** * Rules to exclude transforming target * @default [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/] */ exclude?: FilterPattern /** * Rules to include transforming target * @default [/\.vue$/, /\.vue\?vue/] */ include?: FilterPattern /** * Add prefix for class * @default '' */ classPrefix?: string } ``` ### Attributify Mode See [attributify-mode](https://github.com/unocss/unocss/tree/main/packages/preset-attributify#attributify-mode) ```html ``` Default transfrom attributes list ['bg', 'flex', 'grid', 'border', 'text', 'font', 'class', 'className', 'p', 'm', 'animate'] ```html ``` Add new `attributes`, like `my-attr` ```ts import { attributifyToClass, defaultAttributes } from 'unplugin-attributify-to-class/vite' // import { attributifyToClass, defaultAttributes } from 'unplugin-attributify-to-class/webpack' attributifyToClass({ attributes: [...defaultAttributes, 'my-attr'] }) ``` ```html ``` ### Prefix Self-referencing For utilities like flex, grid, border, that have the utilities same as the prefix, a special ~ value is provided. ```html ``` Can be written as ```html ``` ### Valueless Attributify ```html ``` Setting classPrefix with `li-` ```ts attributifyToClass({ nonValuedAttribute: true, classPrefix: 'li-', }) ``` After transform , bg-green => class="li-bg-green" ```html ``` related links * [unocss-preset-weapp](https://github.com/MellowCo/unocss-preset-weapp) - Uthe unocss preset for wechat miniprogram. --- ### utils ```ts import { extractorAttributify } from 'unplugin-attributify-to-class/utils' const options = { // ... } const extractor = extractorAttributify(options) const code = 'xxxxx' const newCode = extractor(code) ```