Vite SVG Loader

5.1.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to configure `vite-svg-loader` in `vite.config.ts`, including SVGO customization, and how to import SVGs as both Vue components and URLs in a Vue SFC.

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>

view raw JSON →