2.2.4 • Published 3 months ago

@spiriit/vite-plugin-svg-spritemap v2.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

npm node-current Coverage Status

vite-plugin-svg-spritemap

This plugin supports Vite 4 and 5.

This ViteJS plugin generates a single SVG spritemap with <symbol>/<view>/<use> for each SVG files. It can also generate a stylesheet (CSS/SCSS/Stylus/Less) containing the sprites to be used directly (via a Data URI or SVG fragments).

The plugin outputs can be fully configurable through options.

This plugin is inspired by svg-spritemap-webpack-plugin for Webpack.

🚀 Features

  • ⚡ Fully integrated in your ViteJS environment
  • 📦 Pack your SVG files in one (spritemap) file
  • ✨ Use your SVG in an <svg> or <img> tags and also directly in your CSS/SCSS/Stylus/Less
  • 🍕 Import SVG fragment as VueJS component
  • 🔥 HMR support

📦 Install

npm i -D @spiriit/vite-plugin-svg-spritemap

# yarn
yarn add -D @spiriit/vite-plugin-svg-spritemap

# pnpm
pnpm add -D @spiriit/vite-plugin-svg-spritemap

👨‍💻 Usage

By default, the plugin will generate a spritemap to support all methods described below (files populated with <view> for fragments and <use> for sprite).

// vite.config.js / vite.config.ts
import VitePluginSvgSpritemap from '@spiriit/vite-plugin-svg-spritemap'

export default {
  plugins: [VitePluginSvgSpritemap('./src/icons/*.svg')]
}

You can access to the spritemap via the route /__spritemap. All files process by ViteJS will transform the path of the file on build. By default, you will need to use the prefix sprite-.

SVG

<svg>
  <use xlink:href="/__spritemap#sprite-spiriit"></use>
</svg>

Img

You need to add the suffix -view to access to the fragment.

<img src="/__spritemap#sprite-spiriit-view" />

CSS

You can also use the spritemap SVGs in your CSS. The plugin supports CSS (basic classes) and also SCSS, Stylus and Less (mixins and map with SVG Data URI and sizes).

First you need to adjust the plugin options to set the output styles. For full styles options, check the Options.

// vite.config.js / vite.config.ts
import VitePluginSvgSpritemap from '@spiriit/vite-plugin-svg-spritemap'

export default {
  plugins: [
    VitePluginSvgSpritemap('./src/icons/*.svg', {
      styles: 'src/scss/spritemap.scss'
    })
  ]
}
// main.scss
@import './spritemap.scss';

After that, you need to import the file in your current styles. Don't forget to load the CSS via ViteJS.

If you use a CSS preprocessing language, you can use the mixin sprite and access to a map with all sprites infos. If not, you will only access to classes.

You can see the usage in the demo folder :

Use Vue components

vite-plugin-svg-spritemap allows you to load icons and create <use> as <svg> and <view> as <img> tags like Vue components.

To do that, import the icons loaded by vite-svg-spritemap and add the ?use or ?view query. The plugin will transform the component.

<script setup lang="ts">
import SpiriitView from './icons/spiriit.svg?view'
import SpiriitUse from './icons/spiriit.svg?use'
import ViteView from './icons/vite.svg?view'
import ViteUse from './icons/vite.svg?use'
</script>

<template>
  <SpiriitUse>
    <!-- You can use the slot to pass a title before the use tag generation -->
    <title>My superb logo</title>
  </SpiriitUse>
  <ViteUse />

  <SpiriitView />
  <ViteView />
</template>

will generate

<svg>
  <title>My superb logo</title>
  <use xlink:href="/__spritemap#sprite-spiriit"></use>
</svg>
<svg>
  <use xlink:href="/__spritemap#sprite-vite"></use>
</svg>
<img src="/__spritemap#sprite-spiriit-view" width="118" height="38">
<img src="/__spritemap#sprite-vite-view" width="31" height="32">

You can see the usage in the corresponding demo folder.

Typescript

For typescript, you need to load the type definitions inside vite-env.d.ts to fix errors with ?use/?view query.

/// <reference types="vite/client" />
/// <reference types="@spiriit/vite-plugin-svg-spritemap/client" />

Usage with Nuxt 3

!NOTE This plugin only works with Nuxt 3 and Vite as a bundler.

You just need to install the plugin and set it in the right place for Nuxt 3.

// nuxt.config.ts
import VitePluginSvgSpritemap from '@spiriit/vite-plugin-svg-spritemap'

export default defineNuxtConfig({
  vite: {
    plugins: [
      VitePluginSvgSpritemap('./assets/icons/*.svg'),
    ]
  }
})

You can see the usage in the corresponding demo folder.

Typescript

For usage with TypeScript, you will need to add in a .d.ts file the reference type /// <reference types="@spiriit/vite-plugin-svg-spritemap/dist/client" /> (see issue #38) to use ?use/?view query.

/// <reference types="@spiriit/vite-plugin-svg-spritemap/dist/client" />

Use for backend integration

ViteJS allows to be use to serve assets. So, you can connect ViteJS with Wordpress, Drupal or any kind of backend.

!IMPORTANT To make vite-plugin-svg-spritemap works with this kind of environnment, you will need to handle the right url inside your backend if you are on dev or build.

For example, with <use> on dev, using direcly the id of the svg (with the injectSVGOnDev option).

<svg>
  <use xlink:href="#sprite-spiriit"></use>
</svg>

And in prod, by putting the correct URL manually thanks to the manifest.json file information :

<svg>
  <use xlink:href="https://my-cool-website.com/dist/assets/spritemap.95b4c41a.svg#sprite-spiriit"></use>
</svg>

To prevent CORS issue with SVG and <use>, you can use the injectSVGOnDev option. Don't forget to add the HMR script directly above you close body.

<script type="module" src="http://localhost:5173/@vite-plugin-svg-spritemap/client"></script>

You can see an example of integration in the corresponding demo folder.

🛠 Options

The first argument is a glob path (using fast-glob) and the second is an object with the following options :

OptionsTypeDefaultDescription
outputboolean or object or stringtrueAs a string, set the destination of the file (see output.filename). For more control, see output. Set to false to disable output.
stylesfalse or object or stringfalseFile destination like src/css/spritemap.css or styles object
prefixstring or falsesprite-Define the prefix uses for sprite id in <symbol>/<use>/<view>. Set to false to disable the prefix
svgoboolean or objecttrueTake an SVGO Options object. If true, it will use the default SVGO preset, if false, it will disable SVGO optimization
injectSVGOnDevbooleanfalseInject the SVG Spritemap inside the body on dev
idify(name:string, svg:object) => stringname => options.prefix + nameFunction allowing to customize the id of each symbol of the spritemap svg.

output

OptionsTypeDefaultDescription
filenamestring[name].[hash][extname]The destination of the file. You can use output filename like Rollup. Note: Doesn't support hash number.
namestringspritemap.svgThe name of file, appear on the manifest key
usebooleantrueInsert use element in the spritemap
viewbooleantrueInsert view element in the spritemap

styles

OptionsTypeDescription
filenamestringThe destination of the stylesheet file like your source folder
langless/scss/styl/css or undefined

Example with full options :

// vite.config.js / vite.config.ts
import VitePluginSVGSpritemap from '@spiriit/vite-plugin-svg-spritemap'

export default {
  plugins: [
    VitePluginSVGSpritemap('./src/icons/*.svg', {
      prefix: 'icon-',
      output: {
        filename: '[name].[hash][extname]',
        name: 'spritemap.svg',
        view: false,
        use: true,
      },
      svgo: {
        plugins: [
          {
            name: 'removeStyleElement',
          },
        ],
      },
      injectSVGOnDev: true,
      idefy: (name, svg) => `icon-${name}-cheese`,
      styles: {
        lang: 'scss',
        filename: 'src/scss/spritemap.scss'
      }
    })
  ]
}

🏃 What's next

👨‍💼 Licence

MIT

2.2.4

3 months ago

2.2.3

4 months ago

2.2.2

4 months ago

2.2.1

4 months ago

2.2.0

4 months ago

2.1.0

5 months ago

1.2.0

9 months ago

1.1.0

9 months ago

2.0.1

5 months ago

1.4.0

6 months ago

2.0.0

5 months ago

1.3.0

9 months ago

1.0.2

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago