Setting up UnoCSS in a Vite Project
- Published
- Tags
- #unocss#vue#web dev
bash
pnpm create vite unocssVite
Select Vue + Typescript
bash
pnpm i @unocss/reset cva@npm:class-variance-authority
bash
pnpm i unocss @iconify/json unplugin-icons -D
Create a unocss.config.ts in your main directory
typescript
import {
defineConfig,
presetAttributify,
presetIcons,
presetTypography,
presetUno,
presetWebFonts,
transformerDirectives,
transformerVariantGroup,
} from 'unocss'
export default defineConfig({
shortcuts: [
],
presets: [
presetUno(),
presetAttributify(),
presetIcons({
scale: 1.2,
warn: true,
}),
presetTypography(),
presetWebFonts({
fonts: {
sans: 'Poppins',
serif: 'Poppins',
mono: 'JetBrains Mono',
},
}),
],
transformers: [
transformerDirectives(),
transformerVariantGroup(),
],
})
In Main.ts change:
typescript
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
createApp(App).mount('#app')
To:
typescript
import { createApp } from 'vue'
import '@unocss/reset/tailwind.css'
import './assets/styles/main.css'
import 'uno.css'
import App from './App.vue'
createApp(App).mount('#app')
In vite.config.ts change:
typescript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
})
To:
typescript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Unocss from 'unocss/vite'
import Icons from 'unplugin-icons/vite'
export default defineConfig({
plugins: [
vue(),
Icons({
autoInstall: true,
}),
Unocss(),
],
})
html
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
</script>
<template>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
</div>
<HelloWorld msg="Vite + Vue" />
</template>
<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
</style>