vite-plugin-svg-sprite
raw JSON → 0.7.0 verified Mon Apr 27 auth: no javascript
A Vite plugin that transforms SVG files into SVG sprite symbols or framework-specific components. Current stable version is 0.7.0, released with support for custom adapter types. Compatible with Vite 2–7, React 17–19, and Vue 3. Unlike svgr, this plugin generates an inline sprite and provides better render performance for large icon sets. Exports SVG as symbol IDs (vanilla), React components, or Vue components. Ships TypeScript definitions and includes SVGO optimization.
Common errors
error Cannot find module 'vite-plugin-svg-sprite' or its corresponding type declarations. ↓
cause TypeScript cannot resolve the module types because the type declarations are not automatically included.
fix
Add one of the following to your tsconfig.json "types" array: "vite-plugin-svg-sprite/typings/vanilla", "vite-plugin-svg-sprite/typings/react", or "vite-plugin-svg-sprite/typings/vue".
error TypeError: createSvgSpritePlugin is not a function ↓
cause Incorrect import syntax: the plugin is a default export, not a named import.
fix
Use 'import createSvgSpritePlugin from 'vite-plugin-svg-sprite'' instead of 'import { createSvgSpritePlugin } from 'vite-plugin-svg-sprite''.
error Unresolved variable or type: Attributes ↓
cause When using vanilla exportType, trying to destructure 'size' instead of 'attributes' from the SVG module.
fix
Use 'import { attributes } from './icon.svg'' (since v0.6.0) instead of 'import { size } from './icon.svg''.
Warnings
breaking In v0.6.0, the named export 'size' was renamed to 'attributes' for vanilla SVG imports. ↓
fix Replace import { size } from './icon.svg' with import { attributes } from './icon.svg'.
breaking In v0.6.0, the internal dependency 'svg-baker' was removed. ↓
fix If your code relied on svg-baker exports, migrate to use this plugin's output directly.
deprecated Using require() to import this plugin is deprecated; it is ESM-only and does not export a CommonJS module. ↓
fix Change const createSvgSpritePlugin = require('vite-plugin-svg-sprite') to import createSvgSpritePlugin from 'vite-plugin-svg-sprite'.
gotcha By default, the plugin only processes SVGs matching '**/icons/*.svg'. SVGs outside that pattern are not turned into sprite symbols. ↓
fix Set the 'include' option to a broader micromatch pattern (e.g., '**/*.svg') to include all SVG files.
gotcha The plugin re-exports a function as default; it does not export a named function like 'svgSpritePlugin'. ↓
fix Use a default import, e.g., import createSvgSpritePlugin from 'vite-plugin-svg-sprite'.
Install
npm install vite-plugin-svg-sprite yarn add vite-plugin-svg-sprite pnpm add vite-plugin-svg-sprite Imports
- default wrong
const createSvgSpritePlugin = require('vite-plugin-svg-sprite')correctimport createSvgSpritePlugin from 'vite-plugin-svg-sprite' - IconFoo (from SVG import as React component) wrong
import { IconFoo } from './icons/foo.svg';correctimport IconFoo from './icons/foo.svg'; <IconFoo /> - attributes (from SVG import, vanilla) wrong
import { size } from './icons/foo.svg';correctimport { attributes } from './icons/foo.svg';
Quickstart
import createSvgSpritePlugin from 'vite-plugin-svg-sprite';
export default {
plugins: [
createSvgSpritePlugin({
exportType: 'react',
include: 'src/icons/*.svg',
symbolId: 'icon-[name]',
svgo: {
plugins: [
{ name: 'removeViewBox', active: false }
]
}
})
]
};