Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/yunsii/unplugin-svg-sprite

A framework-agnostic unified plugin for SVG sprite
https://github.com/yunsii/unplugin-svg-sprite

esbuild jannajs nextjs rollup rspack sprite svg svg-sprite svg-stack svgo unplugin vite webpack

Last synced: 7 days ago
JSON representation

A framework-agnostic unified plugin for SVG sprite

Awesome Lists containing this project

README

        

> [!Note]
> If you are using Tailwind CSS, I recommend you can try to use svg icons by tailwind plugin by [iconify.design](https://iconify.design/docs/usage/css/tailwind/#options), where icons are used as background or mask image.
>
> Also I create a tailwind plugin [tailwindcss-plugin-iconify](https://github.com/yunsii/tailwindcss-plugin-iconify) for more easy to use, especially for import local svg icons or import icons from figma.

# unplugin-svg-sprite

[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![License][license-src]][license-href]

> [🌱 Try it online](https://stackblitz.com/github/yunsii/unplugin-svg-sprite)

## πŸŽ‰ Features

- πŸ¦„ [Unified plugin](https://github.com/unjs/unplugin), support Vite/Rollup/Webpack/Nuxt/esbuild
- ❀️ Framework-agnostic, use whatever framework you like
- 😎 Import SVG file directly in the source code
- πŸͺ \*Inject **dynamic SVG sprite** only
- πŸ˜„ HMR supported (Vite/Webpack)
- πŸ€– Detect duplicated SVG shapes
- πŸš€ Auto optimization for unused and duplicated svg shapes (After build)

> There is [some SVG nodes](https://stackoverflow.com/a/74173265/8335317) will make SVG item broken, if `use` node `href` property is external link like `/svg-sprite-symbol#unplugin`. I call it `dynamic SVG`, because regular SVG can use with external **static resource** link smoothly.
>
> Fortunately, with the plugin, you can just use injected `href` property to resolve the situation. Only **dynamic SVG sprite** will inject to the DOM.

## πŸ‘€ Install

```bash
npm i unplugin-svg-sprite
```

Vite

```ts
// vite.config.ts
import svgSprite from 'unplugin-svg-sprite/vite'

export default defineConfig({
plugins: [
svgSprite({
/* options */
}),
],
})
```

Example: [`playground/`](./playground/)


Rollup

```ts
// rollup.config.js
import svgSprite from 'unplugin-svg-sprite/rollup'

export default {
plugins: [
svgSprite({
/* options */
}),
],
}
```


Webpack

```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-svg-sprite/webpack')({
/* options */
}),
],
}
```


Nuxt

```ts
// nuxt.config.js
export default {
vue: {
runtimeCompiler: true,
},
// Nuxt 2 move `modules` into `buildModules`
modules: [
[
'unplugin-svg-sprite/nuxt',
{
/* options */
},
],
],
}
```

> This module works for both Nuxt 2 and [Nuxt Vite](https://github.com/nuxt/vite)


Vue CLI

```ts
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-svg-sprite/webpack')({
/* options */
}),
],
},
}
```


esbuild

```ts
// esbuild.config.js
import { build } from 'esbuild'
import svgSprite from 'unplugin-svg-sprite/esbuild'

build({
plugins: [svgSprite()],
})
```


## πŸ—ΊοΈ Road Map

[Check **todo** issues](https://github.com/yunsii/unplugin-svg-sprite/issues?q=is%3Aopen+label%3Atodo+sort%3Aupdated-desc)

## πŸ“š References

- [svg-sprite](https://github.com/svg-sprite/svg-sprite)
- [How SVG Fragment Identifiers Work](https://css-tricks.com/svg-fragment-identifiers-work/)
- [SVG Stacks](https://simurai.com/blog/2012/04/02/svg-stacks)
- [SVGO](https://github.com/svg/svgo)

## License

[MIT](./LICENSE) License Β© 2023-PRESENT [yunsii](https://github.com/yunsii)

[npm-version-src]: https://img.shields.io/npm/v/unplugin-svg-sprite?color=a1b858&label=
[npm-version-href]: https://www.npmjs.com/package/unplugin-svg-sprite
[npm-downloads-src]: https://img.shields.io/npm/dm/unplugin-svg-sprite.svg?color=a1b858
[npm-downloads-href]: https://www.npmjs.com/package/unplugin-svg-sprite
[license-src]: https://img.shields.io/github/license/yunsii/unplugin-svg-sprite.svg?style=flat&colorB=a1b858
[license-href]: https://github.com/yunsii/unplugin-svg-sprite/blob/main/LICENSE