Vite Plugin for fast creating SVG sprites.
Installations
npm install @digitalacorn/vite-plugin-svg-icons
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
16.20.2
NPM Version
8.19.4
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (85.26%)
Vue (8.7%)
Shell (3.64%)
HTML (2.39%)
Developer
digitalacorn
Download Statistics
Total Downloads
28,061
Last Day
34
Last Week
162
Last Month
694
Last Year
16,563
GitHub Statistics
5 Stars
81 Commits
3 Forks
3 Branches
1 Contributors
Bundle Size
1.48 MB
Minified
403.90 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.0-pre.2
Package Id
@digitalacorn/vite-plugin-svg-icons@3.0.0-pre.2
Unpacked Size
33.67 kB
Size
7.64 kB
File Count
7
NPM Version
8.19.4
Node Version
16.20.2
Publised On
12 Jun 2024
Total Downloads
Cumulative downloads
Total Downloads
28,061
Last day
54.5%
34
Compared to previous day
Last week
-16.1%
162
Compared to previous week
Last month
3.9%
694
Compared to previous month
Last year
52.2%
16,563
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
5
@digitalacorn/vite-plugin-svg-icons
Used to generate svg sprite map.
Please Note: this is fork of vite-plugin-svg-icons
Initially created so I can proceed using the feature in this pull request.
There has been no sign of activity from the maintainer of the above.
Summary of additions:
- Adds the ability to disable the replacement of all stroke colours with currentColour.
- Version 3+ Also this package now includes significant increments to modues used, removing vulnerabilities, and without more test coverage could introduce breaking changes from the 2.X version.
Feature
- Preloading All icons are generated when the project is running, and you only need to operate dom once.
- High performance Built-in cache, it will be regenerated only when the file is modified.
Installation (yarn or npm)
node version: >=12.0.0
vite version: >=2.0.0
1yarn add vite-plugin-svg-icons -D 2# or 3npm i vite-plugin-svg-icons -D 4# or 5pnpm install vite-plugin-svg-icons -D
Usage
- Configuration plugin in vite.config.ts
1import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' 2import path from 'path' 3 4export default () => { 5 return { 6 plugins: [ 7 createSvgIconsPlugin({ 8 // Specify the icon folder to be cached 9 iconDirs: [path.resolve(process.cwd(), 'src/icons')], 10 // Specify symbolId format 11 symbolId: 'icon-[dir]-[name]', 12 13 /** 14 * custom insert position 15 * @default: body-last 16 */ 17 inject?: 'body-last' | 'body-first' 18 19 /** 20 * custom dom id 21 * @default: __svg__icons__dom__ 22 */ 23 customDomId: '__svg__icons__dom__', 24 25 /** 26 * option to perform a replacement of stroke colors with currentColor 27 * @default:true 28 */ 29 replaceStrokeWithCurrentColor: true 30 }), 31 ], 32 } 33} 34
- Introduce the registration script in src/main.ts
1import 'virtual:svg-icons-register'
Here the svg sprite map has been generated
How to use in components
Vue way
/src/components/SvgIcon.vue
1<template> 2 <svg aria-hidden="true"> 3 <use :href="symbolId" :fill="color" /> 4 </svg> 5</template> 6 7<script> 8import { defineComponent, computed } from 'vue' 9 10export default defineComponent({ 11 name: 'SvgIcon', 12 props: { 13 prefix: { 14 type: String, 15 default: 'icon', 16 }, 17 name: { 18 type: String, 19 required: true, 20 }, 21 color: { 22 type: String, 23 default: '#333', 24 }, 25 }, 26 setup(props) { 27 const symbolId = computed(() => `#${props.prefix}-${props.name}`) 28 return { symbolId } 29 }, 30}) 31</script>
Icons Directory Structure
1# src/icons 2 3- icon1.svg 4- icon2.svg 5- icon3.svg 6- dir/icon1.svg
/src/App.vue
1<template> 2 <div> 3 <SvgIcon name="icon1"></SvgIcon> 4 <SvgIcon name="icon2"></SvgIcon> 5 <SvgIcon name="icon3"></SvgIcon> 6 <SvgIcon name="dir-icon1"></SvgIcon> 7 </div> 8</template> 9 10<script> 11import { defineComponent, computed } from 'vue' 12 13import SvgIcon from './components/SvgIcon.vue' 14export default defineComponent({ 15 name: 'App', 16 components: { SvgIcon }, 17}) 18</script>
React way
/src/components/SvgIcon.jsx
1export default function SvgIcon({
2 name,
3 prefix = 'icon',
4 color = '#333',
5 ...props
6}) {
7 const symbolId = `#${prefix}-${name}`
8
9 return (
10 <svg {...props} aria-hidden="true">
11 <use href={symbolId} fill={color} />
12 </svg>
13 )
14}
Icons Directory Structure
1# src/icons 2 3- icon1.svg 4- icon2.svg 5- icon3.svg 6- dir/icon1.svg
/src/App.jsx
1import SvgIcon from './components/SvgIcon' 2 3export default function App() { 4 return ( 5 <> 6 <SvgIcon name="icon1"></SvgIcon> 7 <SvgIcon name="icon1"></SvgIcon> 8 <SvgIcon name="icon1"></SvgIcon> 9 <SvgIcon name="dir-icon1"></SvgIcon> 10 </> 11 ) 12}
Get all SymbolId
1import ids from 'virtual:svg-icons-names' 2// => ['icon-icon1','icon-icon2','icon-icon3']
Options
Parameter | Type | Default | Description |
---|---|---|---|
iconDirs | string[] | - | Need to generate the icon folder of the Sprite image |
symbolId | string | icon-[dir]-[name] | svg symbolId format, see the description below |
svgoOptions | boolean|SvgoOptions | true | svg compression configuration, can be an objectOptions |
inject | string | body-last | svgDom default insertion position, optional body-first |
customDomId | string | __svg__icons__dom__ | Customize the ID of the svgDom insert node |
replaceStrokeWithCurrentColor | boolean | true | Whether to perform a replacement of stroke colors with currentColor |
symbolId
icon-[dir]-[name]
[name]:
svg file name
[dir]
The svg of the plug-in will not generate hash to distinguish, but distinguish it by folder.
If the folder corresponding to iconDirs
contains this other folder
example:
Then the generated SymbolId is written in the comment
1# src/icons 2- icon1.svg # icon-icon1 3- icon2.svg # icon-icon2 4- icon3.svg # icon-icon3 5- dir/icon1.svg # icon-dir-icon1 6- dir/dir2/icon1.svg # icon-dir-dir2-icon1
Typescript Support
If using Typescript
, you can add in tsconfig.json
1// tsconfig.json 2{ 3 "compilerOptions": { 4 "types": ["vite-plugin-svg-icons/client"] 5 } 6}
Note
Although the use of folders to distinguish between them can largely avoid the problem of duplicate names, there will also be svgs with multiple folders and the same file name in iconDirs
.
This needs to be avoided by the developer himself
Example
Run
1 2pnpm install 3cd ./packages/playground/basic 4pnpm run dev 5pnpm run build 6
Sample project
License
This package may be deprecated if the PR is accepted and other updates are implemented by the current maintainer of the package from which it is forked. Unless you need thw specific updates, security improvements or features it is recommended that you use the original package vite-plugin-svg-icons.
No vulnerabilities found.
No security vulnerabilities found.