<template>
<svg aria-hidden="true" class="svg-icon">
<use :xlink:href="symbolId" :fill="color" ></use>
</svg>
</template>
<script setup lang="ts">
import { defineProps,computed } from "vue";
const props = defineProps({
prefix: { type: String, default: "icon" },
iconClass: { type: String, required: true },
color: { type: String, default: "" },
});
const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`);
</script>
<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
overflow: hidden;
fill: currentColor;
}
</style>
import { createApp } from 'vue'
import App from './App.vue'
import svgIcon from '@/components/svgIcon/index.vue'
createApp(App).component('svg-icon', svgIcon).mount('#app');
import { readFileSync, readdirSync } from 'fs'
let idPerfix = ''
const svgTitle = //
const clearHeightWidth = /(width|height)="([^>+].*?)"/g
const hasViewBox = /(viewBox="[^>+].*?")/g
const clearReturn = /(\r)|(\n)/g
// 查找svg文件
function svgFind(e: any) {
const arr = []
const dirents = readdirSync(e, { withFileTypes: true })
for (const dirent of dirents) {
if (dirent.isDirectory()) arr.push(...svgFind(e + dirent.name + '/'))
else {
const svg = readFileSync(e + dirent.name)
.toString()
.replace(clearReturn, '')
.replace(svgTitle, ($1: any, $2: any) => {
let width = 0,
height = 0,
content = $2.replace(clearHeightWidth, (s1: any, s2: any, s3: any) => {
if (s2 === 'width') width = s3
else if (s2 === 'height') height = s3
return ''
})
if (!hasViewBox.test($2)) content += `viewBox="0 0 ${width} ${height}"`
return `${idPerfix} -${dirent.name.replace('.svg', '')}" ${content}>`
}).replace('', '')
arr.push(svg)
}
}
return arr
}
// 生成svg
export const createSvg = (path: any, perfix = 'icon') => {
if (path === '') return
idPerfix = perfix
const res = svgFind(path)
return {
name: 'svg-transform',
transformIndexHtml(dom: String) {
return dom.replace(
'',
`${res.join('')}`
)
}
}
}
如果是js文件就写下面代码:
import { readFileSync, readdirSync } from 'fs'
let idPerfix = ''
const svgTitle = //
const clearHeightWidth = /(width|height)="([^>+].*?)"/g
const hasViewBox = /(viewBox="[^>+].*?")/g
const clearReturn = /(\r)|(\n)/g
function findSvgFile(dir) {
const svgRes = []
const dirents = readdirSync(dir, {
withFileTypes: true
})
for (const dirent of dirents) {
if (dirent.isDirectory()) {
svgRes.push(...findSvgFile(dir + dirent.name + '/'))
} else {
const svg = readFileSync(dir + dirent.name)
.toString()
.replace(clearReturn, '')
.replace(svgTitle, ($1, $2) => {
// console.log(++i)
// console.log(dirent.name)
let width = 0
let height = 0
let content = $2.replace(
clearHeightWidth,
(s1, s2, s3) => {
if (s2 === 'width') {
width = s3
} else if (s2 === 'height') {
height = s3
}
return ''
}
)
if (!hasViewBox.test($2)) {
content += `viewBox="0 0 ${width} ${height}"`
}
return `${idPerfix} -${dirent.name.replace(
'.svg',
''
)}" ${content}>`
})
.replace('', '')
svgRes.push(svg)
}
}
return svgRes
}
export const createSvg = (path, perfix = 'icon') => {
if (path === '') return
idPerfix = perfix
const res = findSvgFile(path)
// console.log(res.length)
// const res = []
return {
name: 'svg-transform',
transformIndexHtml(html) {
return html.replace(
'',
`
${res.join('')}
`
)
}
}
}
iimport { createSvg } from './src/utils/svg'
export default defineConfig({
createSvg('./src/icons/')
})
<svg-icon icon-class="next"></svg-icon>
<svg-icon icon-class="magnify" ></svg-icon>
icon-class里面的是对应 icons目录下的文件名