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

使用技巧

最后配置完就可以使用了

相关推荐
我才是银古12 小时前
当 360° 全景遇上真实地图:Vue 3 实现全景-地图双向联动的工程实践
3d·vue3·360影像
XLYcmy2 天前
HTML/CSS/JS 基础与 Vue 技术栈深度解析
java·前端·css·html·vue3·vue2·api
深念Y3 天前
07-SSR水合问题实战排查与修复记录
前端·vue·vite·nuxt·ssr·csr·水合
进哥AI研习社4 天前
构建工具链深度配置——Vite/SWC 与 Tree Shaking 调优
rollup·vite·构建工具·swc·代码分割·esbuild·tree shaking
PedroQue994 天前
v1.4.0:新增 defineUniPage 宏声明页面配置,架构全面重构
前端·vite
码视野5 天前
基于 Spring Boot + Vue3 的【大学英语四六级 (CET-4/6) 作文智能评分与句式润色系统】设计与实现(含PRD/三端高保真源码/大屏)
java·前端·人工智能·spring boot·后端·vue3
xiaohe06015 天前
🤔 v-mortal 是什么?!我只知道 v-model 啊!
vue.js·vite·前端工程化
码视野5 天前
基于 Spring Boot + Vue3 的【高校化学实验室安全准入考试与危化品配伍排查系统】设计与实现(含PRD/三端高保真源码/大屏)
前端·人工智能·spring boot·后端·安全·vue3
fxshy5 天前
WebGIS 游戏化实践:基于 Cesium + Vue3 实现全球飞行模拟系统:从球体坐标、飞行动力学到地形碰撞实战
游戏·vue3·cesium·webgis·飞行模拟
jonyleek6 天前
技术实践:基于 Vue3 + Spring Cloud 微服务实现私有化文档系统的类 SaaS 协同体验
微服务·私有化部署·vue3·springcloud·协同编辑·jvs企业文档·无忧企业文档