An open API service indexing awesome lists of open source software.

https://github.com/endmvp/vite-plugin-svgsg


https://github.com/endmvp/vite-plugin-svgsg

icon-sprite-generator icons svg svg-icons svg-sprite svg-sprite-loader svg-sprites vite vite-plugin vite-plugin-svgr vite-react

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

          

# 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:

```bash
npm install vite-plugin-svg-sprite --save-dev
```

```bash
pnpm add vite-plugin-svg-sprite --save-dev
```

```bash
yarn add vite-plugin-svg-sprite --save-dev
```

## Usage

In your vite.config.js (or vite.config.ts), add the plugin like this:

```js
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
]
});
```
1. The plugin watches the specified folder for changes to `.svg` files. It automatically rebuilds the sprite whenever any SVG files in the source directory are modified.
2. It generates a single SVG sprite file, `icon-sprite.svg`, in the specified output directory. This sprite contains all icons as `` elements, with each icon's ID derived from its folder structure and filename (`#subfolder(optional)_filename`).
3. In components, you can reference icons from the sprite using the `` tag:

```tsx
import Icons from "./assets/icon-sprite.svg"

```

## Options

- `iconsDir`: The directory containing your SVG icons. (e.g. `'src/icons'`)
- `outDir`: The output directory where the `icon-sprite.svg` will be generated. (e.g. `'dist'`)

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Author

Vasyl Vovk ([GitHub](https://github.com/Kristalkill))