This is a fork of https://github.com/jpkleemans/vite-svg-loader with the sole difference that it replaces component
with inline
to make it backwards compatible with components using the webpack svg loader.
Vite 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 defineConfig({
plugins: [vue(), svgLoader()]
})
SVGs can be imported as URLs using the ?url
suffix:
import iconUrl from './my-icon.svg?url'
// '/assets/my-icon.2d8efhg.svg'
SVGs can be imported as strings using the ?raw
suffix:
import iconRaw from './my-icon.svg?raw'
// '<?xml version="1.0"?>...'
SVGs can be explicitly imported as Vue components using the ?inline
suffix:
import IconComponent from './my-icon.svg?inline'
// <IconComponent />
When no explicit params are provided SVGs will be imported as Vue components by default.
This can be changed using the defaultImport
config setting,
such that SVGs without params will be imported as URLs (or raw strings) instead.
svgLoader({
defaultImport: 'url' // or 'raw'
})
svgLoader({
svgoConfig: {
multipass: true
}
})
svgLoader({
svgo: false
})
SVGO can be explicitly disabled for one file by adding the ?skipsvgo
suffix:
import IconWithoutOptimizer from './my-icon.svg?skipsvgo'
// <IconWithoutOptimizer />
If you use the loader in a Typescript project, you'll need to import your svg files with the ?inline
param: import MyIcon from './my-icon.svg?inline'
.
You'll also need to reference the type definitions:
/// <reference types="vite-svg-loader" />