Giter Site home page Giter Site logo

unocss-wechat's Introduction

unocss-wechat

English | 简体中文

miniprogram,unocss example


related links


set unocss preset

two methods to choose from

method one: use common configuration

this method uses the built-in presets in unocss and solves the following configuration

  1. solve the problem that the small program does not support the * selector
  2. rem unit to rpx
  1. use npm in miniprogram,install 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`
      })
    },
  })

method two: use unocss-preset-weapp

this method uses the unocss-preset-weapp preset, which solves the following configuration

because the miniprogram does not support escape class, like \ \: \[ \$ \., so need transform bg-#81ecec/50 to bg-hex-81ecec_50

  • use 'hex' instead of '#' , _ instead of : /

    • for example, bg-#81ecec/50 can be converted to bg-hex-81ecec_50
  • for 'hover: and active:, separators can be set to specify the separator

    • for example, setting separators to __, hover:bg-red-500 can be converted to hover__bg-red-500
  1. use npm in miniprogram,install 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:'__'
})

generate unocss.wxss

  1. package.json,setting script

use @unocss/cli to listen to file content,documents

{
  "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. run npm run unocss

wxml content changes, trigger the generation of new unocss.wxss


  1. import unocss.wxss

in app.wxss, impoort generated 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%;
}


use vscode unocss plugin

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


transformer

using transformer in mini programs will change the original file and is not recommended

  • unocss.config.js

add transformerClass,setting include 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
    })
  ]
})

Additional Notes

  • At this time, the box-shadow shorthand notations like , and [ aren't compatible with native mini-programs. This is due to certain restrictions on the platform that prevent the transformer from processing these notations. Therefore, a straightforward solution isn't available. We suggest using the pre-configured settings instead. If these don't suffice, feel free to write your own CSS. You can find more discussion on this topic in this thread.

unocss-wechat's People

Contributors

mellowco avatar lawted avatar charleewa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.