Coder Social home page Coder Social logo

zguolee / vite-plugin-unocss-to-uni Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 0.0 127 KB

A Vite plugin that supports UnoCSS in uni-app

Home Page: https://www.npmjs.com/package/vite-plugin-unocss-to-uni

Vue 9.04% TypeScript 87.86% HTML 3.10%
miniprogram plugin uniapp unocss windicss

vite-plugin-unocss-to-uni's Introduction

vite-plugin-unocss-to-uni

Version Downloads

A Vite plugin that supports UnoCSS in uni-app, which can transform some class that mini-program can't use.

⚠️ It's recommended to use unocss-applet instead of this plugin.

class transform

form to sample
\. -point- p-0.5 -> p-0-point-5
\/ -div- p-1/2 -> p-1-div-2
\: -c- dark:text-green-500 -> dark-c-text-green-500
\% -pct opacity-10% -> opacity-10-pct
\! i- !bg-black -> i-bg-black
\# -h- bg-#121212 -> bg--h-121212
\( p- bg-[hsl(2.7,81.9%,69.6%)] -> bg-[hslp-2.7,81.9%,69.6%)]
\) -q bg-[hsl(2.7,81.9%,69.6%)] -> bg-[hsl(2.7,81.9%,69.6%-q]
\[ l- bg-[hsl(2.7,81.9%,69.6%)] -> bg-l-hsl(2.7,81.9%,69.6%)]
\] -r bg-[hsl(2.7,81.9%,69.6%)] -> bg-[hsl(2.7,81.9%,69.6%)-r
\, -comma- bg-[hsl(2.7,81.9%,69.6%)] -> bg-[hsl(2.7-comma-81.9%-comma-69.6%)]

Usage

Prepare

  • 🎨 UnoCSS - The instant on-demand atomic CSS engine.

Install

pnpm add -D vite-plugin-unocss-to-uni

Configure vite.config.ts

import { defineConfig } from 'vite'
...
import Unocss from 'unocss/vite'
import { UnocssToUni } from 'vite-plugin-unocss-to-uni'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),

    Unocss(),
    // Make sure it's behind Unocss
    UnocssToUni(),
  ],
})

Configure unocss.config.ts

import {
  defineConfig,
  presetIcons,
  presetUno,
  ...
} from 'unocss'
import { unocssToUniProcess } from 'vite-plugin-unocss-to-uni'

export default defineConfig({
  ...
  presets: [
    // https://github.com/unocss/unocss/blob/main/packages/preset-mini/src/preflights.ts
    // The miniprogram does not support the `*` selector.
    {
      ...presetUno(),
      preflights: [
        {
          layer: 'preflights',
          getCSS(ctx: PreflightContext<any>) {
            if (ctx.theme.preflightBase)
              return `page{${entriesToCss(Object.entries(ctx.theme.preflightBase))}}`
          },
        },
      ],
    },
    ...
  ],
  ...
  postprocess: (t) => {
    t.selector = unocssToUniProcess(t.selector)
    return t
  },
})

Example

ColorTimetable

License

MIT License © 2022-PRESENT zguolee

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.