微信原生小程序接入 unocss

难点:没整过

知识点:unocss,unocss-preset-weapp

1.还是先下载依赖,这里应该先构建npm(参考链接)

npm add -D unocss unocss-preset-weapp

 2.配置unocss.config.ts

这里有个坑,微信原生小程序,某些字符会变成空格,需要预设一下,不过不建议大家这么写,可读性和可维护性都不行,然后写的时候还得对照着写,个人感觉纯粹脱裤子放屁,还是单独的自定义一个class名写比较好,不过人各有所爱,我都列出来,看情况取自己需要的

2.1简略版

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

const include = [/\.wxml$/]

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

2.2转义版

比如【left-[calc(76%-64rpx)]】就不能直接写,需要转义,这个转义的预设可以自己设定,建议设置的复杂一点,我这个是从unocss-preset-weapp直接cv过来的转义成这样即可:

【left--f111l-calc-b111l-76-p111--64rpx-b111r--f111r-】

import { defineConfig } from "unocss";
import presetWeapp from 'unocss-preset-weapp';
import { extractorAttributify, transformerClass } from 'unocss-preset-weapp/transformer'
const transformRules = {
  '.': '-d111-',
  '/': '-s111-',
  ':': '-c111-',
  '%': '-p111-',
  '!': '-e111-',
  '#': '-w111-',
  '(': '-b111l-',
  ')': '-b111r-',
  '[': '-f111l-',
  ']': '-f111r-',
  '$': '-r111-',
  ',': '-r222-',
}
const { presetWeappAttributify, transformerAttributify } = extractorAttributify({
  transformRules
})
const include = [/\.wxml$/]

export default defineConfig({
  presets: [
    // https://github.com/MellowCo/unocss-preset-weapp
    presetWeapp({
      transformRules,
    }),
    // attributify autocomplete
    presetWeappAttributify()
  ],
  content:{
    pipeline:{
      include
    }
  },
  transformers: [
    // options 见https://github.com/MellowCo/unocss-preset-weapp/tree/main/src/transformer/transformerAttributify
    transformerAttributify(),

    // options 见https://github.com/MellowCo/unocss-preset-weapp/tree/main/src/transformer/transformerClass
    transformerClass({
      transformRules,
    }),
  ],
  separators:'__'
})

3.package.json中接入命令,监听wxml文件将你新写的class打包入一个新的wxss文件中

终端npm run unocss,一直运行的就行

{
  "name": "xiangmuming",
  "version": "1.0.0",
  "description": "项目说明",
  "main": "app.js",
  "scripts": {
    "unocss": "unocss */**/*.wxml -c unocss.config.js --watch -o unocss.wxss",
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "unocss": "^0.58.3",
    "unocss-preset-weapp": "^0.58.0"
  }
}

4.插入app.wxss中

@import '/unocss.wxss';

5.自定义组件中如果需要的话还得再配置一下,一定要记得,否则写了也不生效

Component({
  options: {
    addGlobalClass: true
  },
})

6.大功告成,默认兼容tailwindCss,这个很爽啊!更多玩法可以去官网看看

别的端可以也接入tailwindCSS,看我写的博文

react 接入 unocss 

vue3、react和taro/react配置tailwindCSS

hbx/uni3/vite/微信小程序 接入unocss

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸喵小宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值