Gathering detailed insights and metrics for vite-plugin-svg4vue
Gathering detailed insights and metrics for vite-plugin-svg4vue
A vite plugin which can transform svg icon to vue component. Support optimization via SVGO, support customize svg color and size.
npm install vite-plugin-svg4vue
Typescript
Module System
Node Version
NPM Version
45.8
Supply Chain
88.3
Quality
87.6
Maintenance
100
Vulnerability
98.6
License
JavaScript (36.43%)
Vue (31.57%)
TypeScript (23.12%)
SCSS (7.97%)
HTML (0.91%)
Total Downloads
22,301
Last Day
43
Last Week
426
Last Month
1,255
Last Year
12,129
19 Stars
146 Commits
4 Forks
2 Watching
7 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
4.0.4
Package Id
vite-plugin-svg4vue@4.0.4
Unpacked Size
21.22 kB
Size
5.68 kB
File Count
8
NPM Version
10.8.2
Node Version
20.18.1
Publised On
23 Jan 2025
Cumulative downloads
Total Downloads
Last day
-25.9%
43
Compared to previous day
Last week
17%
426
Compared to previous week
Last month
57.3%
1,255
Compared to previous month
Last year
29.9%
12,129
Compared to previous year
A vite (3.x || 4.x)
plugin which can transform SVG
icon to vue (2.7.X || 3.x)
component.
this plugin dependencies on
vue/compiler-sfc
, so keep yourvue
version to 3.2.13+ or 2.7.14+.
?url
, ?component
and ?raw
query string.fill
, fill-opacity
, stroke
, stroke-opacity
attribute.font-size
and it will be responsive.1# pnpm 2$ pnpm add vite-plugin-svg4vue -D 3 4# yarn 5$ yarn add vite-plugin-svg4vue --dev 6 7# npm 8$ npm i vite-plugin-svg4vue -D
1// vite.config.ts 2 3import { defineConfig } from 'vite' 4import vue from '@vitejs/plugin-vue' 5import { svg4VuePlugin } from 'vite-plugin-svg4vue' 6 7export default defineConfig({ 8 plugins: [ 9 vue(), 10 svg4VuePlugin(), 11 ], 12})
1<template> 2 <div> 3 <h2>SVG component: </h2> 4 5 <!-- you can change the svg icon color with `fill` attribute when it's a monochrome icon --> 6 <LogoSvg fill="red" /> 7 8 <!-- you can change the svg icon size with `font-size` or `width`. both of this will be responsive --> 9 <LogoSvg font-size="48" /> 10 <LogoSvg width="48" /> 11 12 <h2>SVG url: </h2> 13 14 <p> 15 <img :src="logoSvgUrl" alt="" width="36" /> 16 </p> 17 18 <h2>SVG raw: </h2> 19 20 <p> 21 <span v-html="logoSvgRaw"></span> 22 </p> 23 </div> 24</template> 25 26<script setup lang="ts"> 27import LogoSvg from '@/icons/logo.svg?component' 28import logoSvgUrl from '@/icons/logo.svg?url' 29import logoSvgRaw from '@/icons/logo.svg?raw' 30</script>
SVGO can be explicitly disabled for one file by adding the ?skipsvgo
query string:
1<template> 2 <div class="d-flex align-center mb-16"> 3 <label class="mr-12"> ?component&skipsvgo: </label> 4 <PPTSvg /> 5 </div> 6 7 <div class="d-flex align-center"> 8 <label class="mr-12"> ?raw&skipsvgo: </label> 9 <span v-html="PPTSvgRaw"></span> 10 </div> 11</template> 12 13<script setup lang="ts"> 14import PPTSvg from '@/icons/ppt.svg?component&skipsvgo' 15import PPTSvgRaw from '@/icons/ppt.svg?raw&skipsvgo' 16</script>
If you are using TypeScript, vite-plugin-svg4vue/client can be added to d.ts
declaration file.
1/// <reference types="vite-plugin-svg4vue/client" />
Key | Default value | Description | Type |
---|---|---|---|
svgoConfig | {} | SVGO config. if set to false , will disable SVGO. | object/boolean |
enableSvgoPresetDefaultConfig | true | Whether to enable preset-default configuration for SVGO | boolean |
defaultExport | url | Default behavior when importing .svg files, possible options are: url , component and raw | string |
assetsDirName | icons | Restrict SVG to a specific folder. As long as the SVG is in the assetsDirName folder, it can be processed by this plugin, Even if the folder is nested, such as, path/to/icons/*.svg , icons/path/to/svg/*.svg , path/to/icons/path/to/*.svg and so on. If set assetsDirname to false , will make it work with arbitrary file path. | string/boolean |
enableBuildCache | true | Whether to enable caching at build time | boolean |
enableMonochromeSvgOptimize | true | Whether to enable monochrome svg icon optimize which can move child node (named path, Even the path wrapped by g) 's fill , fill-opacity and stroke , stroke-opacity attribute to its parent node (svg element). So that you can change the svg icon color with fill and stroke . | boolean |
enableSvgSizeResponsive | true | Whether to enable svg icon responsive. | boolean |
enableSvgSizeResponsive
do ?In fact, for the svg node, vite-plugin-svg4vue will set the width
value to font-size
, remove svg height
and set width
to 1em
, so that the svg size will be responsive and you can manipulate it's size with font-size
.
Just in case, it records the original size of the svg as a css variables:
1<svg style="--svg-origin-width: ${origin width}; --svg-origin-height: ${origin height};"></svg>
So, you can easily apply its original size.
1<template> 2 <LogoSvg fill="red" style="width: var(--svg-origin-width); height: var(--svg-origin-height)" /> 3</template> 4 5<script setup lang="ts"> 6import LogoSvg from '@/icons/logo.svg?component' 7</script>
No vulnerabilities found.
No security vulnerabilities found.