Git Product home page Git Product logo

vite-plugin-svg-spritemap's Introduction

npm node-current Coverage Status

vite-plugin-svg-spritemap

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
  • ๐Ÿ”ฅ 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. It will be process in build. By default, you will need to use the prefix sprite-.

SVG

<svg>
  <use 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 needs 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 :

๐Ÿ›  Options

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

Options Type Default Description
output boolean or object true See output
styles false or object or string false File destination like src/css/spritemap.css or styles object
prefix string sprite- Define the prefix uses for sprite id in <symbol>/<use>/<view>
svgo boolean or object true Take an SVGO Options object. If true, it will use the default SVGO preset, if false, it will disable SVGO optimization

output

Options Type Default Description
filename string [name].[hash][extname] The destination of the file. You can use output filename like Rollup
use boolean true Insert use element in the spritemap
view boolean true Insert view element in the spritemap

styles

Options Type Description
filename string The destination of the stylesheet file like your source folder
lang less/scss/styl/css or undefined

๐Ÿƒ What's next

๐Ÿ‘จโ€๐Ÿ’ผ Licence

MIT

vite-plugin-svg-spritemap's People

Contributors

applelo avatar simeonoff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.