chore: unocss-Icons 支持加载本地 svg 作为图标

This commit is contained in:
Utopia
2025-11-04 10:18:09 +08:00
parent adffb24e4e
commit 2f93592e30
4 changed files with 58 additions and 11 deletions

View File

@@ -135,6 +135,7 @@
"@esbuild/darwin-arm64": "0.20.2",
"@esbuild/darwin-x64": "0.20.2",
"@iconify-json/carbon": "^1.2.4",
"@iconify/utils": "^3.0.2",
"@rollup/rollup-darwin-x64": "^4.28.0",
"@types/node": "^20.17.9",
"@uni-helper/eslint-config": "0.5.0",

44
pnpm-lock.yaml generated
View File

@@ -127,6 +127,9 @@ importers:
'@iconify-json/carbon':
specifier: ^1.2.4
version: 1.2.13
'@iconify/utils':
specifier: ^3.0.2
version: 3.0.2
'@rollup/rollup-darwin-x64':
specifier: ^4.28.0
version: 4.50.0
@@ -320,6 +323,9 @@ packages:
'@antfu/utils@8.1.1':
resolution: {integrity: sha512-Mex9nXf9vR6AhcXmMrlz/HVgYYZpVGJ6YlPgwl7UnaFpnshXs6EK/oa5Gpf3CzENMjkvEx2tQtntGnb7UtSTOQ==}
'@antfu/utils@9.3.0':
resolution: {integrity: sha512-9hFT4RauhcUzqOE4f1+frMKLZrgNog5b06I7VmZQV1BkvwvqrbC8EBZf3L1eEL2AKb6rNKjER0sEvJiSP1FXEA==}
'@babel/code-frame@7.27.1':
resolution: {integrity: sha512-cjQ7ZlQ0Mv3b47hABuTevyTuYN4i+loJKGeV9flcCgIK37cCXRh+L1bd3iBHlynerhQ7BhCkn2BPbQUL+rGqFg==}
engines: {node: '>=6.9.0'}
@@ -1393,6 +1399,9 @@ packages:
'@iconify/utils@2.3.0':
resolution: {integrity: sha512-GmQ78prtwYW6EtzXRU1rY+KwOKfz32PD7iJh6Iyqw68GiKuoZ2A6pRtzWONz5VQJbp50mEjXh/7NkumtrAgRKA==}
'@iconify/utils@3.0.2':
resolution: {integrity: sha512-EfJS0rLfVuRuJRn4psJHtK2A9TqVnkxPpHY6lYHiB9+8eSuudsxbwMiavocG45ujOo6FJ+CIRlRnlOGinzkaGQ==}
'@intlify/core-base@9.1.9':
resolution: {integrity: sha512-x5T0p/Ja0S8hs5xs+ImKyYckVkL4CzcEXykVYYV6rcbXxJTe2o58IquSqX9bdncVKbRZP7GlBU1EcRaQEEJ+vw==}
engines: {node: '>= 10'}
@@ -6343,6 +6352,8 @@ snapshots:
'@antfu/utils@8.1.1': {}
'@antfu/utils@9.3.0': {}
'@babel/code-frame@7.27.1':
dependencies:
'@babel/helper-validator-identifier': 7.27.1
@@ -6416,7 +6427,7 @@ snapshots:
'@babel/core': 7.28.3
'@babel/helper-compilation-targets': 7.27.2
'@babel/helper-plugin-utils': 7.27.1
debug: 4.4.1
debug: 4.4.3
lodash.debounce: 4.0.8
resolve: 1.22.10
transitivePeerDependencies:
@@ -7083,7 +7094,7 @@ snapshots:
'@babel/parser': 7.28.3
'@babel/template': 7.27.2
'@babel/types': 7.28.2
debug: 4.4.1
debug: 4.4.3
transitivePeerDependencies:
- supports-color
@@ -7288,7 +7299,7 @@ snapshots:
'@vue/compiler-sfc': 3.4.21
'@vue/consolidate': 1.0.0
'@vue/shared': 3.4.21
debug: 4.4.1
debug: 4.4.3
es-module-lexer: 1.7.0
estree-walker: 2.0.2
fast-glob: 3.3.3
@@ -7316,7 +7327,7 @@ snapshots:
'@vitejs/plugin-vue': 5.1.0(vite@5.2.8(@types/node@20.19.11)(sass@1.77.8)(terser@5.43.1))(vue@3.4.21(typescript@5.8.3))
'@vue/compiler-dom': 3.4.21
'@vue/compiler-sfc': 3.4.21
debug: 4.4.1
debug: 4.4.3
fs-extra: 10.1.0
picocolors: 1.1.1
transitivePeerDependencies:
@@ -7402,7 +7413,7 @@ snapshots:
base64url: 3.0.1
chokidar: 3.6.0
compare-versions: 3.6.0
debug: 4.4.1
debug: 4.4.3
es-module-lexer: 1.7.0
esbuild: 0.20.2
estree-walker: 2.0.2
@@ -7548,7 +7559,7 @@ snapshots:
'@vue/compiler-sfc': 3.4.21
'@vue/server-renderer': 3.4.21(vue@3.4.21(typescript@5.8.3))
'@vue/shared': 3.4.21
debug: 4.4.1
debug: 4.4.3
fs-extra: 10.1.0
mime: 3.0.0
module-alias: 2.2.3
@@ -7782,7 +7793,7 @@ snapshots:
'@vue/compiler-dom': 3.4.21
'@vue/compiler-sfc': 3.4.21
'@vue/shared': 3.4.21
debug: 4.4.1
debug: 4.4.3
transitivePeerDependencies:
- '@nuxt/kit'
- '@vueuse/core'
@@ -7885,7 +7896,7 @@ snapshots:
dependencies:
'@dcloudio/uni-cli-shared': 3.0.0-4070620250821001(postcss@8.5.6)(rollup@4.50.0)(vue@3.4.21(typescript@5.8.3))
'@dcloudio/uni-shared': 3.0.0-4070620250821001
debug: 4.4.1
debug: 4.4.3
transitivePeerDependencies:
- '@nuxt/kit'
- '@vueuse/core'
@@ -8157,6 +8168,19 @@ snapshots:
transitivePeerDependencies:
- supports-color
'@iconify/utils@3.0.2':
dependencies:
'@antfu/install-pkg': 1.1.0
'@antfu/utils': 9.3.0
'@iconify/types': 2.0.0
debug: 4.4.3
globals: 15.15.0
kolorist: 1.8.0
local-pkg: 1.1.2
mlly: 1.8.0
transitivePeerDependencies:
- supports-color
'@intlify/core-base@9.1.9':
dependencies:
'@intlify/devtools-if': 9.1.9
@@ -9015,7 +9039,7 @@ snapshots:
dependencies:
'@typescript-eslint/tsconfig-utils': 8.41.0(typescript@5.8.3)
'@typescript-eslint/types': 8.41.0
debug: 4.4.1
debug: 4.4.3
typescript: 5.8.3
transitivePeerDependencies:
- supports-color
@@ -9069,7 +9093,7 @@ snapshots:
'@typescript-eslint/tsconfig-utils': 8.41.0(typescript@5.8.3)
'@typescript-eslint/types': 8.41.0
'@typescript-eslint/visitor-keys': 8.41.0
debug: 4.4.1
debug: 4.4.3
fast-glob: 3.3.3
is-glob: 4.0.3
minimatch: 9.0.5

View File

@@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1762219859937" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8816" id="mx_n_1762219859938" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64z m0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z" p-id="8817"></path><path d="M517.6 351.3c53 0 89 33.8 93 83.4 0.3 4.2 3.8 7.4 8 7.4h56.7c2.6 0 4.7-2.1 4.7-4.7 0-86.7-68.4-147.4-162.7-147.4C407.4 290 344 364.2 344 486.8v52.3C344 660.8 407.4 734 517.3 734c94 0 162.7-58.8 162.7-141.4 0-2.6-2.1-4.7-4.7-4.7h-56.8c-4.2 0-7.6 3.2-8 7.3-4.2 46.1-40.1 77.8-93 77.8-65.3 0-102.1-47.9-102.1-133.6v-52.6c0.1-87 37-135.5 102.2-135.5z" p-id="8818"></path></svg>

After

Width:  |  Height:  |  Size: 934 B

View File

@@ -1,9 +1,10 @@
import type {
Preset,
} from 'unocss'
import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders'
// https://www.npmjs.com/package/@uni-helper/unocss-preset-uni
import { presetUni } from '@uni-helper/unocss-preset-uni'
// @see https://unocss.dev/presets/legacy-compat
import { presetLegacyCompat } from '@unocss/preset-legacy-compat'
import {
@@ -26,6 +27,26 @@ export default defineConfig({
'display': 'inline-block',
'vertical-align': 'middle',
},
collections: {
// 注册本地 SVG 图标集合, 从本地文件系统加载图标
// 在 './src/static/my-icons' 目录下的所有 svg 文件将被注册为图标,
// my-icons 是图标集合名称,使用 `i-my-icons-图标名` 调用
'my-icons': FileSystemIconLoader(
'./src/static/my-icons',
// 可选的,你可以提供一个 transform 回调来更改每个图标
(svg) => {
let svgStr = svg
// 如果 SVG 文件未定义 `fill` 属性,则默认填充 `currentColor`, 这样图标颜色会继承文本颜色,方便在不同场景下适配
svgStr = svgStr.includes('fill="') ? svgStr : svgStr.replace(/^<svg /, '<svg fill="currentColor" ')
// 如果 svg 有 width, 和 height 属性,将这些属性改为 1em否则无法显示图标
svgStr = svgStr.replace(/(<svg.*?width=)"(.*?)"/, '$1"1em"').replace(/(<svg.*?height=)"(.*?)"/, '$1"1em"')
return svgStr
},
),
},
}),
// 支持css class属性化
presetAttributify(),