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

使用技巧

最后配置完就可以使用了

相关推荐
RuoyiOffice1 天前
2026最推荐基于SpringBoot+Vue3的项目管理经营一体化:从商机、合同、任务与工时走到回款和利润
spring boot·项目管理·vue3·crm·合同管理·ruoyi office·项目经营
RuoyiOffice2 天前
SpringBoot+Vue3 节日主题换肤实战:一条参数换全站配色,节后自动还原
spring boot·vue3·spring boot 3·vben admin·ruoyi office·节日主题·换肤方案
RuoyiOffice4 天前
SpringBoot3+Vue3 流程抄送已读:打开详情消红点,待办与知会怎么分开
spring boot·vue3·flowable·spring boot 3·ruoyi office·流程抄送·已读回执
志尊宝4 天前
Vue3 零基础每日笔记(038):亲手封装 useDebounceFn 与 useThrottleFn——高频事件的流量阀
前端·javascript·vue·html·vue3
志尊宝5 天前
Vue3 零基础每日笔记(043):defineModel 与 defineExpose 的 TS 用法——3.4+ 新 API 进阶
前端·javascript·vue·html·vue3
向北丶6 天前
vite项目中配置alias别名
前端·vite
PedroQue996 天前
修复 .uvue 页面路径残留问题
前端·vite
RuoyiOffice6 天前
SpringBoot3+Vue3+Flowable 企业审批平台全链路:从表单设计、节点权限到移动审批怎么落地
spring boot·vue3·uniapp·springboot3·flowable·审批系统·ruoyi office
梦曦i6 天前
修复 .uvue 页面路径残留问题
前端·vite
RuoyiOffice6 天前
SpringBoot3+Vue3 企业级管理平台架构:多租户、RBAC、数据权限、工作流与微服务一次讲透
spring boot·vue3·springboot3·rbac·多租户·数据权限·ruoyi office