Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

vite-plugin-svg-icons


English| 中文

Used to generate svg sprite map.

Feature


PreloadingAll icons are generated when the project is running, and you only need to operate dom once.
High performanceBuilt-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

  1. ``` shell
  2. yarn add vite-plugin-svg-icons -D
  3. # or
  4. npm i vite-plugin-svg-icons -D
  5. # or
  6. pnpm install vite-plugin-svg-icons -D
  7. ```

Usage


Configuration plugin in vite.config.ts

  1. ``` ts
  2. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  3. import path from 'path'

  4. export 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.          * custom insert position
  14.          * @default: body-last
  15.          */
  16.         inject?: 'body-last' | 'body-first'

  17.         /**
  18.          * custom dom id
  19.          * @default: __svg__icons__dom__
  20.          */
  21.         customDomId: '__svg__icons__dom__',
  22.       }),
  23.     ],
  24.   }
  25. }
  26. ```

Introduce the registration script in src/main.ts

  1. ``` ts
  2. import 'virtual:svg-icons-register'
  3. ```

Here the svg sprite map has been generated

How to use in components


Vue way


/src/components/SvgIcon.vue

  1. ``` vue
  2. <template>
  3.   <svg aria-hidden="true">
  4.     <use :href="symbolId" :fill="color" />
  5.   </svg>
  6. </template>

  7. <script>
  8. import { defineComponent, computed } from 'vue'

  9. export default defineComponent({
  10.   name: 'SvgIcon',
  11.   props: {
  12.     prefix: {
  13.       type: String,
  14.       default: 'icon',
  15.     },
  16.     name: {
  17.       type: String,
  18.       required: true,
  19.     },
  20.     color: {
  21.       type: String,
  22.       default: '#333',
  23.     },
  24.   },
  25.   setup(props) {
  26.     const symbolId = computed(() => `#${props.prefix}-${props.name}`)
  27.     return { symbolId }
  28.   },
  29. })
  30. </script>
  31. ```

Icons Directory Structure


  1. ``` shell
  2. # src/icons

  3. - icon1.svg
  4. - icon2.svg
  5. - icon3.svg
  6. - dir/icon1.svg
  7. ```

/src/App.vue

  1. ``` vue
  2. <template>
  3.   <div>
  4.     <SvgIcon name="icon1"></SvgIcon>
  5.     <SvgIcon name="icon2"></SvgIcon>
  6.     <SvgIcon name="icon3"></SvgIcon>
  7.     <SvgIcon name="dir-icon1"></SvgIcon>
  8.   </div>
  9. </template>

  10. <script>
  11. import { defineComponent, computed } from 'vue'

  12. import SvgIcon from './components/SvgIcon.vue'
  13. export default defineComponent({
  14.   name: 'App',
  15.   components: { SvgIcon },
  16. })
  17. </script>
  18. ```

React way


/src/components/SvgIcon.jsx

  1. ``` js
  2. export default function SvgIcon({
  3.   name,
  4.   prefix = 'icon',
  5.   color = '#333',
  6.   ...props
  7. }) {
  8.   const symbolId = `#${prefix}-${name}`

  9.   return (
  10.     <svg {...props} aria-hidden="true">
  11.       <use href={symbolId} fill={color} />
  12.     </svg>
  13.   )
  14. }
  15. ```

Icons Directory Structure


  1. ``` shell
  2. # src/icons

  3. - icon1.svg
  4. - icon2.svg
  5. - icon3.svg
  6. - dir/icon1.svg
  7. ```

/src/App.jsx

  1. ``` js
  2. import SvgIcon from './components/SvgIcon'

  3. export 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. }
  13. ```

Get all SymbolId


  1. ``` ts
  2. import ids from 'virtual:svg-icons-names'
  3. // => ['icon-icon1','icon-icon2','icon-icon3']
  4. ```

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

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. ``` shell
  2. # src/icons
  3. - icon1.svg # icon-icon1
  4. - icon2.svg # icon-icon2
  5. - icon3.svg # icon-icon3
  6. - dir/icon1.svg # icon-dir-icon1
  7. - dir/dir2/icon1.svg # icon-dir-dir2-icon1
  8. ```

Typescript Support


If using Typescript, you can add in tsconfig.json

  1. ``` json
  2. // tsconfig.json
  3. {
  4.   "compilerOptions": {
  5.     "types": ["vite-plugin-svg-icons/client"]
  6.   }
  7. }
  8. ```

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. ``` shell
  2. pnpm install
  3. cd ./packages/playground/basic
  4. pnpm run dev
  5. pnpm run build

  6. ```

Sample project


Vben Admin

License


MIT © Vben-2020
Last Updated: 2023-05-23 11:11:51