chore: 🔨 本地 SVG 加载方式由 vite-plugin-svg-icons 切换为 @unocss/preset-icons
This commit is contained in:
@@ -10,6 +10,25 @@ import {
|
||||
transformerVariantGroup,
|
||||
} from "unocss";
|
||||
|
||||
import { FileSystemIconLoader } from "@iconify/utils/lib/loader/node-loaders";
|
||||
|
||||
import fs from "fs";
|
||||
|
||||
const iconsDir = "./src/assets/icons";
|
||||
|
||||
// 读取本地 SVG 目录,自动生成 safelist
|
||||
const generateSafeList = () => {
|
||||
try {
|
||||
return fs
|
||||
.readdirSync(iconsDir)
|
||||
.filter((file) => file.endsWith(".svg"))
|
||||
.map((file) => `i-svg:${file.replace(".svg", "")}`);
|
||||
} catch (error) {
|
||||
console.error("无法读取图标目录:", error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export default defineConfig({
|
||||
shortcuts: {
|
||||
"flex-center": "flex justify-center items-center",
|
||||
@@ -32,7 +51,22 @@ export default defineConfig({
|
||||
presets: [
|
||||
presetUno(),
|
||||
presetAttributify(),
|
||||
presetIcons(),
|
||||
presetIcons({
|
||||
extraProperties: {
|
||||
display: "inline-block",
|
||||
width: "1em",
|
||||
height: "1em",
|
||||
},
|
||||
collections: {
|
||||
svg: FileSystemIconLoader(iconsDir, (svg) => {
|
||||
// 如果 `fill` 没有定义,则添加 `fill="currentColor"`
|
||||
if (!svg.includes('fill="')) {
|
||||
return svg.replace(/^<svg /, '<svg fill="currentColor" ');
|
||||
}
|
||||
return svg;
|
||||
}),
|
||||
},
|
||||
}),
|
||||
presetTypography(),
|
||||
presetWebFonts({
|
||||
fonts: {
|
||||
@@ -40,5 +74,6 @@ export default defineConfig({
|
||||
},
|
||||
}),
|
||||
],
|
||||
safelist: generateSafeList(),
|
||||
transformers: [transformerDirectives(), transformerVariantGroup()],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user