1.1.2 • Published 8 months ago
@endmvp/vite-plugin-svgsg v1.1.2
vite-plugin-svg-sprite
A Vite plugin to generate and manage SVG icon sprites. It scans a directory for SVG files, compiles them into a single sprite, and watches for changes to regenerate the sprite automatically.
Installation
To install the plugin, run one of the following command:
npm install vite-plugin-svg-sprite --save-devpnpm add vite-plugin-svg-sprite --save-devyarn add vite-plugin-svg-sprite --save-devUsage
In your vite.config.js (or vite.config.ts), add the plugin like this:
import { defineConfig } from 'vite';
import IconSpritePlugin from 'vite-plugin-svg-sprite';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
IconSpritePlugin('src/icons', 'dist') // Specify your icons directory and output directory
]
});- The plugin watches the specified folder for changes to
.svgfiles. It automatically rebuilds the sprite whenever any SVG files in the source directory are modified. - It generates a single SVG sprite file,
icon-sprite.svg, in the specified output directory. This sprite contains all icons as<symbol>elements, with each icon's ID derived from its folder structure and filename (#subfolder(optional)_filename). - In components, you can reference icons from the sprite using the
<use>tag:
import Icons from "./assets/icon-sprite.svg"
<svg width="18" height="18">
<use href={`${Icons}#footer_phone`}></use>
</svg>Options
iconsDir: The directory containing your SVG icons. (e.g.'src/icons')outDir: The output directory where theicon-sprite.svgwill be generated. (e.g.'dist')
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Vasyl Vovk (GitHub)