欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > unocss 添加支持使用本地 svg 预设图标,并支持更改大小

unocss 添加支持使用本地 svg 预设图标,并支持更改大小

2025/4/19 14:24:34 来源:https://blog.csdn.net/weixin_39786582/article/details/143424045  浏览:    关键词:unocss 添加支持使用本地 svg 预设图标,并支持更改大小

安装 pnpm install @iconify/utils

在配置文件 unocss.config.tspresets > presetIcons 选项中

通过 FileSystemIconLoader 加载本地图标,并指定目录。

import presetWeapp from 'unocss-preset-weapp'
import { extractorAttributify, transformerClass } from 'unocss-preset-weapp/transformer'
import { defineConfig, presetIcons } from 'unocss'const { presetWeappAttributify, transformerAttributify } = extractorAttributify()import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders' // 支持通过 iconify 加载本地 svg 图标
export default defineConfig({presets: [// https://github.com/MellowCo/unocss-preset-weapppresetWeapp({isH5: process.env.TARO_ENV === 'h5',platform: 'taro',// 通过设置 taroWebpack 版本,指定 rem 策略taroWebpack: 'webpack5',// designWidth: 375,designWidth: 750,deviceRatio: {640: 2.34 / 2,750: 1,828: 1.81 / 2,375: 2 / 1},},),presetWeappAttributify(),// 预设图标presetIcons({// 添加 '$1"1em" ,支持动态改变图标大小collections: {// 不同模块,自定义图标前缀tender: FileSystemIconLoader('./src/assets/svg-icon/tenderDetail', svg => svg.replace(/(<svg.*?width=)"(.*?)"/, '$1"1em"').replace(/(<svg.*?height=)"(.*?)"/, '$1"1em"')),// appSvg: FileSystemIconLoader('./src/assets/svg-icon', svg => svg//   .replace(/(<svg.*?width=)"(.*?)"/, '$1"1em"').replace(/(<svg.*?height=)"(.*?)"/, '$1"1em"')),// ...}}),],shortcuts: [{'border-base': 'border border-gray-500/10','center': 'flex justify-center items-center',},],transformers: [// https://github.com/MellowCo/unocss-preset-weapp/tree/main/src/transformer/transformerAttributify// 支持在小程序中使用 attributify modetransformerAttributify(),// https://github.com/MellowCo/unocss-preset-weapp/tree/main/src/transformer/transformerClass// 用于转换 微信小程序 不支持的 \\,\: \[ \$ \. 等转义类名, 实现在小程序中使用原子化csstransformerClass(),],theme: {color: {bg: 'rgba(22, 33, 255, 1)',},},
})

使用:
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词