微信小程序如何使用 unocss

4,702 阅读1分钟

unocss-wechat

原生小程序,unocss示例

68747470733a2f2f666173746c792e6a7364656c6976722e6e65742f67682f4d656c6c6f77436f2f696d6167652d686f73742f323032322f3230323230393134313335343336332e676966.gif


相关链接


设置 unocss 预设

两种方法任选一种

方法一: 使用通用配置

此方法使用 unocss 内置预设,通过以下配置解决

  1. 解决小程序不支持 * 选择器
  2. rem单位 转 rpx
  1. 小程序中使用npm,安装 unocss
npm -D unocss
  1. unocss.config
import { defineConfig, presetUno } from "unocss";

const remRE = /^-?[\.\d]+rem$/

export default defineConfig(
  {
    presets: [
      presetUno(),
    ],
    theme:{
      // 解决小程序不支持 * 选择器
      preflightRoot: ["page,::before,::after"]
    },
    postprocess(util) {
      // 自定义rem 转 rpx
      util.entries.forEach((i) => {
        const value = i[1]
        if (value && typeof value === 'string' && remRE.test(value))
          i[1] = `${value.slice(0, -3) * 16 * 2}rpx`
      })
    },
  }
)

方法二:使用 unocss-preset-weapp 预设

unocss-preset-weapp 内部已经解决小程序不兼容的相关问题

由于小程序不支持 \ \: \[ \$ \. 等转义类名

  1. 使用 hex 代替 # , _ 代替 : /

    • 例如 bg-#81ecec/50 可以使用 bg-hex-81ecec_50 表示
  2. 针对 hover:avtive:, 可以设置 separators 指定分隔符

    • 例如设置 separators__hover:bg-red-500 可以使用 hover__bg-red-500 表示

小程序中使用npm,安装 unocss unocss-preset-weapp

npm -D unocss unocss-preset-weapp

  1. unocss.config
import { defineConfig } from "unocss";
import presetWeapp from 'unocss-preset-weapp'

const include = [/\.wxml$/]

export default defineConfig({
    content:{
        pipeline:{
          include
        }
    },
    presets: [
      presetWeapp(),
    ],
    separators:'__'
})

生成wxss文件

package.json,设置 script

使用 @unocss/cli 监听文件内容,参考文档

{
  "scripts": {
     "unocss": "unocss pages/**/*.wxml -c unocss.config.js --watch -o unocss.wxss",
     "unocss:build": "unocss pages/**/*.wxml -c unocss.config.js -o  unocss.wxss"
  }
}

  1. 运行 npm run unocss

wxml 内容变化,触发生成新的 unocss.wxss

image.png


  1. 导入 unocss.wxss

app.wxss 导入生成的 unocss.wxss

/**app.wxss**/
@import "./unocss.wxss";

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding: 200rpx 0;
  height: 100%;
}

68747470733a2f2f666173746c792e6a7364656c6976722e6e65742f67682f4d656c6c6f77436f2f696d6167652d686f73742f323032322f3230323230393134313335343336332e676966.gif


unocss插件

  • vscode settings.json
  // 文件类型
"files.associations": {
  "*.wxml": "html",
},

68747470733a2f2f666173746c792e6a7364656c6976722e6e65742f67682f4d656c6c6f77436f2f696d6167652d686f73742f323032322f3230323230393231323033363834302e676966.gif


使用 transformer

transformer 可以将 小程序不支持 \\ \\: \\[ \\$ \\. 等转义类名,根据规则替换

原生小程序使用 transformer 会改变原文件,不推荐使用

  • unocss.confit.js

添加 transformerClass,设置转换 wxml 文件

import { defineConfig } from "unocss";
import presetWeapp from 'unocss-preset-weapp'
import { transformerClass } from 'unocss-preset-weapp/transformer';

const include = [/\.wxml$/]

export default defineConfig({
  content:{
    pipeline:{
      include
    }
  },
  presets: [
    presetWeapp(),
  ],
  
  transformers:[
    transformerClass({
      include,
      classTags: false
    })
  ]
})

68747470733a2f2f666173746c792e6a7364656c6976722e6e65742f67682f4d656c6c6f77436f2f696d6167652d686f73742f323032322f3230323230393231323031393332302e676966.gif