安装插件包
yarn add @iconify/iconify
yarn add @purge-icons/generated vite-plugin-purge-icons @iconify/json -D
在编译阶段按需加载本地离线图标(注意:字体库会导致打包体积变大)
- 抽取要离线加载的json图标数据文件
(在离线扫描失效时使用)
{
"ep:": ["add-location", "aim", "alarm-clock", "apple"],
"fa:": ["home", "address-book", "address-book-o", "address-card"],
"fa-solid:": ["abacus", "ad", "address-book"]
}
- 配置插件
import PurgeIcons from 'vite-plugin-purge-icons'
import iconData form './data/'
type ValueType<T> = [T, T[]]
const preloadOfflineIcons = (iconData): string[] => {
return Object.entries(iconData).reduce((a: string[], [k, v]: ValueType<string>) => {
a = a.concat(v.map((i) => k+i))
return a
}, [])
}
export default {
plugins: [
PurgeIcons({
included: preloadOfflineIcons()
})
]
}
- 加入 iconify 图标支持
import { createApp } from 'vue'
import App from './'
import '@purge-icons/generated'
createApp(App).mount('#app')
- 组件中使用
<span class="iconify" data-icon="fa:home"></span>