Vite SVG Loader
Vite SVG Loader is a Vite plugin designed to seamlessly load SVG files as Vue 3 components within a Vite project. It automatically optimizes SVGs using SVGO, which can be configured or entirely disabled on a per-file or global basis. The package is currently stable at version 5.1.1 and receives active maintenance, including compatibility updates for new Vite and Vue versions, as well as security patches for its dependencies. Its release cadence is moderate, with several updates per year, indicating ongoing development. Key differentiators include its flexible import options, allowing SVGs to be loaded as Vue components (default), raw strings (`?raw`), or URL data (`?url`), and the ability to skip SVGO for specific files (`?skipsvgo`). This provides developers with granular control over how SVG assets are handled in their Vue applications.
Common errors
-
Cannot find module './my-icon.svg' or its corresponding type declarations.
cause The TypeScript compiler does not recognize `.svg` files as modules that can be imported, usually due to a missing type declaration for `vite-svg-loader`.fixAdd `/// <reference types="vite-svg-loader" />` to your `vite-env.d.ts` file. -
Error: [vite] The plugin 'vite-svg-loader' was not found. Have you installed it?
cause The plugin is not installed or not correctly added to the `plugins` array in `vite.config.js/ts`.fixRun `npm install --save-dev vite-svg-loader` (or `yarn add -D vite-svg-loader`) and ensure `svgLoader()` is included in `plugins: []` in your `vite.config.js/ts`. -
[plugin:vite-svg-loader] [SVGO] Error: Error: No config file found in...
cause An issue occurred during SVGO processing, potentially due to invalid `svgoConfig` options, especially after upgrading to SVGO v3 in `vite-svg-loader` v4.0.0.fixReview your `svgoConfig` in `vite.config.js/ts` and ensure it adheres to the SVGO v3 configuration format. You can also temporarily set `svgo: false` to isolate if the problem is with SVGO configuration.
Warnings
- breaking Version 5.0.0 introduced breaking changes for compatibility with Vite v5. Users upgrading their Vite projects should ensure they also upgrade `vite-svg-loader` to v5.0.0 or higher.
- breaking Version 4.0.0 upgraded the underlying SVGO library to SVGO v3. This change might introduce new optimization behaviors or require adjustments to existing `svgoConfig` options.
- breaking Version 5.0.1 changed the package type to 'module', which could have caused issues for CommonJS environments. While version 5.1.0 reverted some internal ESM import changes to improve compatibility, users on 5.0.1 might have encountered CJS import errors.
- breaking A high-severity vulnerability (GHSA-xpqw-6gx7-v673 / CVE-2026-29074) in the `svgo` dependency was fixed in version 5.1.1. This vulnerability could potentially affect the security of your build process if unpatched.
- gotcha TypeScript users must include a type reference for SVG imports to be correctly recognized by the TypeScript compiler. Without it, you may get 'Cannot find module' errors for SVG files.
Install
-
npm install vite-svg-loader -
yarn add vite-svg-loader -
pnpm add vite-svg-loader
Imports
- svgLoader
const svgLoader = require('vite-svg-loader')import svgLoader from 'vite-svg-loader'
- MyIcon
import { MyIcon } from './assets/my-icon.svg'import MyIcon from './assets/my-icon.svg'
- iconUrl
import iconUrl from './assets/my-icon.svg'
import iconUrl from './assets/my-icon.svg?url'
- SvgRawString
import { SvgRawString } from './assets/my-icon.svg?raw'import SvgRawString from './assets/my-icon.svg?raw'
- Type Reference
/// <reference types="vite-svg-loader" />
Quickstart
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import svgLoader from 'vite-svg-loader';
export default defineConfig({
plugins: [
vue(),
svgLoader({
svgoConfig: { // Example: customize SVGO options
multipass: true,
plugins: ['preset-default', { name: 'removeViewBox', active: false }]
},
defaultImport: 'component' // Default, but explicitly set for clarity
})
]
});
// src/App.vue
// <template>
// <h1>My App</h1>
// <MyLogo aria-label="Company Logo" />
// <img :src="imageUrl" alt="SVG as Image" />
// </template>
// <script setup>
// import MyLogo from './assets/logo.svg'; // Imported as Vue component
// import imageUrl from './assets/background.svg?url'; // Imported as URL
// </script>