vite配置unocss

vue3+vite+ts+eslint+prettier+stylelint+husky+lint-staged+commitlint+commitizen+cz-git介绍了关于vite+vue工程化搭建,现在在这个基础上,我们增加一下unocss

unocss官方文档

具体开发中使用遇到的问题可以参考不喜欢原子化CSS得我,还是在新项目中使用了Unocss - 掘金 (juejin.cn)

为什么要使用unocss,有必要使用吗?

UnoCSS 是即时原子 CSS 引擎,其设计灵活且可扩展。核心是不固定的,所有 CSS 工具都是通过预设提供的。

这个因人而异,最直接的就是可以应付面试,如果面试官问,

txt 复制代码
面试官:你了解(用过)原子化css吗?
我:内心os,什么是原子化css?
面试官:你都用过哪些原子化css库
我:???

安装

shell 复制代码
pnpm i -D unocss
# 样式重置,也可以不用
pnpm i @unocss/reset

vite.config.ts配置

ts 复制代码
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import UnoCss from "unocss/vite";

export default defineConfig({
  plugins: [vue(), UnoCss()],
});

新建unocss.config.ts

ts 复制代码
import {
  defineConfig,
  presetAttributify,
  presetIcons,
  presetTypography,
  presetUno,
  transformerDirectives,
  transformerVariantGroup,
} from "unocss";

export default defineConfig({
  shortcuts: [
    ["flex-center", "flex items-center justify-center"],
    ["flex-between", "flex items-center justify-between"],
    ["flex-start", "flex items-center justify-start"],
    ["flex-end", "flex items-center justify-end"],
  ],
  rules: [
      // 配置了eslint可能会有报错,在eslintrc.cjs 里面rules里面把规则关闭即可 "linebreak-style": "off" 
    [
      /^clamp-(\d+)$/,
      ([, d]) =>
        d === "1"
          ? {
              overflow: "hidden",
              "text-overflow": "ellipsis",
              "white-space": "nowrap",
            }
          : {
              "-webkit-line-clamp": d,
              overflow: "hidden",
              "text-overflow": "ellipsis",
              display: "-webkit-box",
              "white-space": "normal",
              "-webkit-box-orient": "vertical",
            },
    ],
  ],
  presets: [
    presetUno(),
    presetAttributify(),
    presetIcons(),
    presetTypography(),
  ],
  transformers: [transformerDirectives(), transformerVariantGroup()],
});
ts 复制代码
import {
  defineConfig,
  presetAttributify,
  presetIcons,
  presetTypography,
  presetUno,
  transformerDirectives,
  transformerVariantGroup,
} from "unocss";

export default defineConfig({
  shortcuts: [
    ["flex-center", "flex items-center justify-center"],
    ["flex-between", "flex items-center justify-between"],
    ["flex-start", "flex items-center justify-start"],
    ["flex-end", "flex items-center justify-end"],
  ],
  rules: [
      // 配置了eslint可能会有报错,在eslintrc.cjs 里面rules里面把规则关闭即可 "linebreak-style": "off" 
    [
      /^clamp-(\d+)$/,
      ([, d]) =>
        d === "1"
          ? {
              overflow: "hidden",
              "text-overflow": "ellipsis",
              "white-space": "nowrap",
            }
          : {
              "-webkit-line-clamp": d,
              overflow: "hidden",
              "text-overflow": "ellipsis",
              display: "-webkit-box",
              "white-space": "normal",
              "-webkit-box-orient": "vertical",
            },
    ],
  ],
  presets: [
    presetUno(),
    presetAttributify(),
    presetIcons(),
    presetTypography(),
  ],
  transformers: [transformerDirectives(), transformerVariantGroup()],
});

vscode安装插件,有助于开发的时候有提示,以及显示编译后的css

  • UnoCSS
  • WindiCSS IntelliSense

使用技巧

最后配置完就可以使用了

相关推荐
minko15 小时前
这给我干哪儿来了,这还是react-router吗
react.js·vite
西门吹雪~2 天前
【前端面试】在Vue3中,beforeMount和mounted钩子函数有什么区别?
前端·javascript·vue.js·前端框架·vue3
西门吹雪~3 天前
Vue3 从入门到精通:全面掌握前端框架的进阶之路
vue.js·前端框架·vue3
西门吹雪~3 天前
【前端框架】Vue3 中 `setup` 函数的作用和使用方式
前端·javascript·vue.js·前端框架·vue3
西门吹雪~3 天前
【前端框架】Vue3 面试题深度解析
前端·前端框架·vue·vue3·前端面试
西门吹雪~4 天前
【前端框架】vue2和vue3的区别详细介绍
前端·javascript·vue.js·前端框架·vue3·vue2
西门吹雪~4 天前
【前端框架】深入Vue 3组件开发:构建高效灵活的前端应用
前端·vue.js·前端框架·vue3
cs_dn_Jie5 天前
uniapp + vite + 使用多个 ui 库
vue.js·ui·uni-app·vite
患得患失9496 天前
【前端】【面试】ref与reactive的区别
前端·面试·vue3
sakuraxiaoyu9 天前
MHTML文件如何在前端页面展示
前端·html·vue3·js·mhtml