A plugin for Vite, Webpack & Rsbuild that generates optimized SVG sprite files from directories of SVG icons. Supports multiple input/output configurations, TypeScript declarations, and is ideal for scalable icon management in modern front-end applications.
- Generate one or more SVG sprite files from multiple icon directories
- Supports color replacement with any valid CSS color format
- Automatically watches for changes during development
- Works well with inlined
<use>references - Available for Vite, Webpack, and Rsbuild projects
- Generate TypeScript declarations for your icons
npm install @prof-dev/svg-sprite-plugin --save-devor
yarn add -D @prof-dev/svg-sprite-plugin// vite.config.ts
import { defineConfig } from "vite";
import { ViteSvgSpritePlugin } from "@prof-dev/svg-sprite-plugin";
export default defineConfig({
plugins: [
new ViteSvgSpritePlugin([
{
input: [
{
path: "public/static/icons/plain",
color: "currentColor", // Dynamic color based on text color
},
{
path: "public/static/icons/colored",
// No color specified - preserves original colors
},
{
path: "public/static/icons/themed",
color: "var(--primary-color)", // Using CSS variable
},
{
path: "public/static/icons/red",
color: "#FF0000", // Using hex color
},
],
output: "public/static/icons.svg",
declaration: {
path: "src/icons.d.ts",
namespace: "Icons",
export: false,
},
},
{
input: [
{
path: "public/static/logos",
color: "rgb(0, 128, 255)", // Using RGB color
},
],
output: "public/static/logos.svg",
},
]),
],
});// webpack.config.js
const { WebpackSvgSpritePlugin } = require("@prof-dev/svg-sprite-plugin");
module.exports = {
// ... other webpack configuration
plugins: [
new WebpackSvgSpritePlugin([
{
input: [
{
path: "public/static/icons/plain",
color: "currentColor", // Dynamic color based on text color
},
{
path: "public/static/icons/colored",
// No color specified - preserves original colors
},
{
path: "public/static/icons/themed",
color: "hsl(210, 100%, 50%)", // Using HSL color
},
],
output: "static/icons.svg",
declaration: {
path: "src/icons.d.ts",
namespace: "Icons",
export: false,
},
},
{
input: [
{
path: "public/static/logos",
color: "rgba(0, 128, 255, 0.8)", // Using RGBA with opacity
},
],
output: "static/logos.svg",
},
]),
],
};// rsbuild.config.ts
import { defineConfig } from "@rsbuild/core";
import { rsbuildSvgSpritePlugin } from "@prof-dev/svg-sprite-plugin";
export default defineConfig({
plugins: [
rsbuildSvgSpritePlugin([
{
input: [
{
path: "./icons/plain/",
color: "currentColor",
},
{
path: "./icons/colored/",
},
],
output: "public/static/icons.svg",
declaration: {
path: "src/icons.d.ts",
namespace: "Icons",
export: false,
},
},
]),
],
});All plugins use the same configuration format. Each item in the array generates a separate SVG sprite file.
| Option | Type | Description |
|---|---|---|
| input | SvgSpriteInput[] |
List of input directories and options |
| output | string |
Output path for the generated sprite |
| declaration | SvgSpriteDeclaration? |
TypeScript declaration generation options |
| Option | Type | Description |
|---|---|---|
| path | string |
Directory containing .svg icon files |
| color | string? |
(Optional) CSS color value to replace fills in SVG icons |
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
| path | string |
β | - | Output path for TypeScript declaration file |
| namespace | string |
β | "Icons" |
Type name for the generated interface |
| export | boolean |
β | false |
Whether to add export statement |
The color option supports any valid CSS color format:
currentColor- Inherits color from the parent element's text color#RRGGBBor#RGB- Hexadecimal color valuesrgb(R, G, B)orrgba(R, G, B, A)- RGB/RGBA color valueshsl(H, S%, L%)orhsla(H, S%, L%, A)- HSL/HSLA color valuesvar(--css-variable-name)- CSS custom property variables- Named colors like
red,blue,transparent, etc.
If the color option is omitted, the original colors in the SVG files will be preserved.
When you enable declaration generation, the plugin creates TypeScript interfaces for your icons:
// Generated src/icons.d.ts
interface Icons {
Archive: string;
Cloud: string;
Cloud_Add: string;
File_Add: string;
File_Edit: string;
// ... all your icons
}This provides type safety and autocompletion when referencing your icons in TypeScript code.
After build/start, the plugin will generate sprite files like icons.svg or logos.svg. You can reference the symbols in your HTML:
<!-- Using with default styling (when color option was specified) -->
<svg class="icon">
<use href="/static/icons.svg#icon-name" />
</svg>
<!-- Overriding with inline style -->
<svg class="icon" style="color: blue;">
<use href="/static/icons.svg#icon-name" />
</svg>
<!-- Using with CSS classes -->
<svg class="icon primary-icon">
<use href="/static/icons.svg#icon-name" />
</svg>CSS example when using currentColor:
.primary-icon {
color: var(--primary-color);
}
.danger-icon {
color: #ff0000;
}The Vite plugin runs in development mode (serve). It watches for file changes in the specified directories and regenerates the sprite(s) on-the-fly.
The Webpack plugin supports watch mode and will regenerate sprite files when source SVGs are modified. During production builds, it integrates with Webpack's asset pipeline to output optimized sprite files.
The Rsbuild plugin runs in development mode (dev). It watches for file changes in the specified directories and regenerates the sprite(s) on-the-fly, integrating with Rsbuild's development server.
MIT