Vite plugin to load SVG files as Vue components
npm install vite-svg-loadervue
`
$3
`bash
npm install vite-svg-loader --save-dev
`
$3
#### vite.config.js
`js
import svgLoader from 'vite-svg-loader'
export default defineConfig({
plugins: [vue(), svgLoader()]
})
`
$3
$3
SVGs can be imported as URLs using the ?url suffix:
`js
import iconUrl from './my-icon.svg?url'
// 'data:image/svg+xml...'
`
$3
SVGs can be imported as strings using the ?raw suffix:
`js
import iconRaw from './my-icon.svg?raw'
// '...'
`
$3
SVGs can be explicitly imported as Vue components using the ?component suffix:
`js
import IconComponent from './my-icon.svg?component'
//
`
$3
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.
#### vite.config.js
`js
svgLoader({
defaultImport: 'url' // or 'raw'
})
`
$3
#### vite.config.js
`js
svgLoader({
svgoConfig: {
multipass: true
}
})
`
$3
#### vite.config.js
`js
svgLoader({
svgo: false
})
`
$3
SVGO can be explicitly disabled for one file by adding the ?skipsvgo suffix:
`js
import IconWithoutOptimizer from './my-icon.svg?skipsvgo'
//
`
$3
If you use the loader in a Typescript project, you'll need to reference the type definitions inside vite-env.d.ts:
`ts
///
///
``