npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vitejs-svg-plugin

v1.0.5

Published

[![npm version](https://img.shields.io/npm/v/vitejs-svg-plugin.svg)](https://www.npmjs.com/package/vitejs-svg-plugin) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Readme

vitejs-svg-plugin

npm version License: MIT

A Vite plugin that automatically loads, optimizes, and registers SVG files as inline symbols for efficient usage in your projects.

Features

  • ✅ Automatically scans and loads SVG files from specified directories
  • ✅ Optimizes SVGs using SVGO for smaller file sizes
  • ✅ Supports configurable ID prefix for SVG symbols
  • ✅ Automatically sets fill="currentColor" for consistent styling
  • ✅ Removes width/height attributes for flexible sizing
  • ✅ Registers SVGs as inline symbols in the DOM for efficient rendering
  • ✅ Generates TypeScript type definitions for SVG icon names
  • ✅ Supports CSS mode for using SVGs as CSS background images
  • ✅ Supports manual chunking for better code splitting
  • ✅ Works seamlessly with Vite 3, 4, 5, and 6+

Installation

npm install vitejs-svg-plugin --save-dev

Or using pnpm:

pnpm add -D vitejs-svg-plugin

Usage

Basic Configuration

Add the plugin to your vite.config.ts:

import { defineConfig } from 'vite'
import { createSvgLoader } from 'vitejs-svg-plugin'

export default defineConfig({
  plugins: [
    createSvgLoader({
      path: 'src/assets/svg'
    })
  ]
})

Advanced Configuration

import { defineConfig } from 'vite'
import { createSvgLoader } from 'vitejs-svg-plugin'

export default defineConfig({
  plugins: [
    createSvgLoader({
      path: 'src/assets/svg',
      prefix: 'icon', // Default: 'icon'
      css: false, // Default: false
      output: 'src/types/svg-icons.d.ts'
    })
  ]
})

Usage in Components

The plugin automatically injects the SVG loader into your entry file (src/main.js or src/main.ts). You can use SVG icons in your components:

Vue:

<template>
  <svg class="icon">
    <use :xlink:href="'#icon_home'" />
  </svg>
</template>

React:

const Icon = ({ name }: { name: string }) => (
  <svg className="icon">
    <use xlinkHref={`#icon_${name}`} />
  </svg>
)

Configuration Options

path (required)

Type: string

The directory path where your SVG files are located. The plugin will recursively scan this directory for .svg files.

path: 'src/assets/icons'

prefix (optional)

Type: string
Default: 'icon'

The prefix to use for SVG symbol IDs. This helps avoid ID conflicts when multiple SVG plugins are used.

prefix: 'my-icon' // Results in IDs like: my-icon_home, my-icon_user

css (optional)

Type: boolean
Default: false

Enable CSS mode to use SVGs as CSS background images. When enabled, the plugin generates CSS variables with data URIs instead of SVG symbols.

css: true

CSS Mode Usage:

.icon-home {
  ...
}

.icon-user {
  ...
}
<template>
  <div class="icon-home"></div>
  <div class="icon-user"></div>
</template>

output (optional)

Type: string

Path to generate a TypeScript file containing icon names. This enables type-safe icon usage.

output: 'src/types/svg-icons.d.ts'

Generated file example:

/**
 * Svg Icon names (vitejs-svg-plugin)
 * @author SinJay Xie
 * @date Mon Mar 03 2026
 * @description Load SVG amount: 3
 */
export type SvgIconNames = 'home' | 'user' | 'settings' | string;

How It Works

Default Mode (SVG Symbols)

  1. Scan: The plugin scans the specified directory for all .svg files
  2. Optimize: Each SVG is optimized using SVGO with plugins:
    • removeEmptyAttrs: Removes empty attributes
    • removeAttrs: Removes width and height attributes for flexible sizing
    • addAttributesToSVGElement: Adds fill="currentColor" and configurable ID prefix
  3. Convert: SVG elements are converted to <symbol> elements
  4. Register: Symbols are injected into the DOM as a hidden SVG container
  5. Use: You can reference any SVG using <use xlink:href="#prefix_name" />

CSS Mode

When css: true is enabled:

  1. Scan: The plugin scans the specified directory for all .svg files
  2. Optimize: Each SVG is optimized using SVGO with datauri: 'enc' for base64 encoding
  3. Generate CSS: Creates CSS classes with data URIs as background images
  4. Use: Apply CSS classes to elements to display SVG icons

Example Project Structure

src/
├── assets/
│   └── svg/
│       ├── home.svg
│       ├── user.svg
│       └── settings.svg
├── components/
│   └── Icon.vue
└── main.ts

TypeScript Support

When using the output option, the plugin generates type definitions that allow you to:

  • Get autocomplete for icon names
  • Catch typos at compile time
  • Refactor icon names safely
  • Use the generated SvgIconNames type for type-safe icon usage

Example with TypeScript:

import type { SvgIconNames } from '@/types/svg-icons'

// Type-safe icon usage
const icon: SvgIconNames = 'home'

License

MIT © sinjayxie