pnpm i unocss @unocss/webpack -D
const UnoCSS = require("@unocss/webpack").default
module.exports = defineConfig({
configureWebpack: {
resolve: {
alias: {
"@": path.resolve(__dirname, "src/"),
},
},
//
plugins: [UnoCSS()],
optimization: {
// unocss
realContentHash: true,
},
},
})
如果报错Can't resolve uno.css
,卸载上面的依赖,再重新装试试
import "uno.css"
presetUno的预设是继承于@unocss/preset-wind
and @unocss/preset-mini
.支持流行的css框架如Tailwind CSS, Windi CSS, Bootstrap, Tachyons
的样式写法,如ml-3,ms-2,mt-10px,ma4
.ma4 { margin: 1rem; }
.ml-3 { margin-left: 0.75rem; }
.ms-2 { margin-inline-start: 0.5rem; }
.mt-10px { margin-top: 10px; }
import { defineConfig, presetUno,transformerVariantGroup } from "unocss"
export default defineConfig({
// ...UnoCSS options
presets: [presetUno()],
// hover:text-white等
transformers: [transformerVariantGroup()],
})
@unocss/transformer-directives
只在vite工程中起作用,在webpack包括vue-cli
在要使用@unocss/postcss
地址
@unocss/postcss
pnpm add -D @unocss/postcss
postcss.config.cjs
// postcss.config.cjs
module.exports = {
plugins: {
'@unocss/postcss': {},
},
}
@apply
指令的地方,引入@unocss;
<style>
@unocss;
.test {
@apply p-40px;
}
</style>
@@iconify/json
,可以使用iconfify
图标// uno.config.ts
import { defineConfig, presetUno, transformerVariantGroup, presetIcons } from "unocss"
import { FileSystemIconLoader } from "@iconify/utils/lib/loader/node-loaders"
export default defineConfig({
// ...UnoCSS options
presets: [
presetUno(),
presetIcons({
prefix: "i-",
// icon的样式
extraProperties: {
display: "inline-block",
"vertical-align": "middle",
width: "1em",
height: "1em",
},
// icon的集合,每次新增图标都需要重启项目
// 可着色图标,mask模式
// 原先svg图标,background-image模式
collections: {
icon: FileSystemIconLoader("./src/assets/svgs", svg =>
svg.replace(/fill="([^"]*)"/, 'fill="currentColor"'),
),
"icon-color": FileSystemIconLoader("./src/assets/svgs"),
},
// mode: "mask", // mask 可以使用color改变颜色,根据是否有currentColor属性决定使用mask,或者background-image模式
}),
],
transformers: [transformerVariantGroup()],
})