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

使用技巧

最后配置完就可以使用了

相关推荐
叫我阿柒啊3 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
叫我阿柒啊12 小时前
从Java全栈到云原生:一场技术深度对话
java·spring boot·docker·微服务·typescript·消息队列·vue3
叫我阿柒啊12 小时前
从Java全栈到Vue3实战:一次真实面试的深度复盘
java·spring boot·微服务·vue3·响应式编程·前后端分离·restful api
叫我阿柒啊1 天前
从Java全栈到前端框架:一次真实的面试对话
java·spring boot·微服务·前端框架·vue3·全栈开发
叫我阿柒啊1 天前
Java全栈开发工程师面试实战:从基础到微服务的完整技术演进
java·spring boot·微服务·前端框架·vue3·全栈开发·面试技巧
拜无忧1 天前
【教程】vue+vite+ts创建一个最新的高性能后台项目架构
vue.js·typescript·vite
wallflower20201 天前
🚀 从 Webpack 到 Vite:企业级前端构建、代码分割与懒加载优化完全指南
webpack·vite
Java陈序员2 天前
GitHub 星标太多管不过来?这款 AI 工具帮你一键整理、智能搜索!
react.js·openai·vite
Lsx-codeShare2 天前
前端数据可视化:基于Vue3封装 ECharts 的最佳实践
前端·javascript·echarts·vue3·数据可视化
叫我阿柒啊2 天前
从全栈开发到微服务架构:一位Java工程师的实战经验分享
java·ci/cd·kafka·mybatis·vue3·springboot·fullstack