Vite plugin for creating SVG sprites and components
npm i vite-plugin-svg-sprite-component -D
yarn add vite-plugin-svg-sprite-component -D
// vite.config.js
import svgSpritePlugin from "vite-plugin-svg-sprite-component"
module.exports = {
plugins: [svgSpritePlugin({ symbolId: (name)=> "icon-" + name })],
}
// main.js
// globby import
const svgs = import.meta.globEager('/**your svg path*/*.svg')
// normal
import example from '/**your svg path*/example.svg'
// You should call it at least once so it will not be tree shaked
svgs
example
<template>
<svg>
<use xlink:href="#icon-example" />
</svg>
</template>
<template>
<IconExample />
</template>
<script>
import { IconExample } from '/@/icons/svg/example.svg'
export default {
components:{ IconExample }
}
</script>
When option defaultExport
be true
<template>
<Icon />
</template>
<script>
import Icon from '/@/icons/svg/example.svg'
export default {
components:{ Icon }
}
</script>
export type Options = {
/**
* How <symbol> id attribute should be named
* Default behavior: /@/icons/example.svg => example
*/
symbolId?: (name: string, path: string) => string,
/**
* Remove attributes also include members in style sttr
* @default ['width','height']
*/
removeAttrs?: string[],
component?: {
/** The export component type, Only Vue now */
type: 'vue',
/** export const [exportName] as VNode
* Default behavior: icon-example => IconExample
*/
exportName?: (name: string, path: string) => string,
/**
* export default [exportName] as VNode
* @default false
*/
defaultExport?: boolean,
}
}
MIT