Vite 2.x plugin to load SVG files as Vue components, using SVGO for optimization.
<template>
<MyIcon />
</template>
<script setup>
import MyIcon from './my-icon.svg'
</script>
npm install vite-svg-loader --save-dev
import svgLoader from 'vite-svg-loader'
export default {
plugins: [vue(), svgLoader()]
}
svgLoader({
svgoConfig: {
multipass: true
}
})
svgLoader({
svgo: false
})
If you use the loader in a Typescript project, you'll need to import your svg files with the ?component
param: import MyIcon from './my-icon.svg?component'
.
You'll also need to add the type definitions to src/vite-env.d.ts
:
/// <reference types="vite/client" />
+ /// <reference types="vite-svg-loader" />