{"id":13601724,"url":"https://github.com/vbenjs/vite-plugin-svg-icons","last_synced_at":"2025-12-30T10:48:38.992Z","repository":{"id":37311693,"uuid":"344832831","full_name":"vbenjs/vite-plugin-svg-icons","owner":"vbenjs","description":"Vite Plugin for fast creating SVG sprites.","archived":false,"fork":false,"pushed_at":"2024-08-10T16:16:01.000Z","size":527,"stargazers_count":880,"open_issues_count":67,"forks_count":119,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-08T02:04:43.131Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vbenjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-05T14:17:27.000Z","updated_at":"2025-04-08T00:46:54.000Z","dependencies_parsed_at":"2025-01-03T05:04:54.912Z","dependency_job_id":"a81232f6-eaa1-4214-8e4a-65205082f4e0","html_url":"https://github.com/vbenjs/vite-plugin-svg-icons","commit_stats":{"total_commits":67,"total_committers":11,"mean_commits":6.090909090909091,"dds":"0.19402985074626866","last_synced_commit":"bb334a992739afa418a455c01cb762386a50840a"},"previous_names":["anncwb/vite-plugin-svg-icons"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vbenjs%2Fvite-plugin-svg-icons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vbenjs%2Fvite-plugin-svg-icons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vbenjs%2Fvite-plugin-svg-icons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vbenjs%2Fvite-plugin-svg-icons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vbenjs","download_url":"https://codeload.github.com/vbenjs/vite-plugin-svg-icons/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247761143,"owners_count":20991545,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:01:07.135Z","updated_at":"2025-12-30T10:48:38.935Z","avatar_url":"https://github.com/vbenjs.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# vite-plugin-svg-icons\n\n**English** | [中文](./README.zh_CN.md)\n\nUsed to generate svg sprite map.\n\n## Feature\n\n- **Preloading** All icons are generated when the project is running, and you only need to operate dom once.\n- **High performance** Built-in cache, it will be regenerated only when the file is modified.\n\n## Installation (yarn or npm)\n\n**node version:** \u003e=12.0.0\n\n**vite version:** \u003e=2.0.0\n\n```bash\nyarn add vite-plugin-svg-icons -D\n# or\nnpm i vite-plugin-svg-icons -D\n# or\npnpm install vite-plugin-svg-icons -D\n```\n\n## Usage\n\n- Configuration plugin in vite.config.ts\n\n```ts\nimport { createSvgIconsPlugin } from 'vite-plugin-svg-icons'\nimport path from 'path'\n\nexport default () =\u003e {\n  return {\n    plugins: [\n      createSvgIconsPlugin({\n        // Specify the icon folder to be cached\n        iconDirs: [path.resolve(process.cwd(), 'src/icons')],\n        // Specify symbolId format\n        symbolId: 'icon-[dir]-[name]',\n\n        /**\n         * custom insert position\n         * @default: body-last\n         */\n        inject?: 'body-last' | 'body-first'\n\n        /**\n         * custom dom id\n         * @default: __svg__icons__dom__\n         */\n        customDomId: '__svg__icons__dom__',\n      }),\n    ],\n  }\n}\n\n```\n\n- Introduce the registration script in src/main.ts\n\n```ts\nimport 'virtual:svg-icons-register'\n```\n\nHere the svg sprite map has been generated\n\n## How to use in components\n\n### **Vue way**\n\n`/src/components/SvgIcon.vue`\n\n```vue\n\u003ctemplate\u003e\n  \u003csvg aria-hidden=\"true\"\u003e\n    \u003cuse :href=\"symbolId\" :fill=\"color\" /\u003e\n  \u003c/svg\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\nimport { defineComponent, computed } from 'vue'\n\nexport default defineComponent({\n  name: 'SvgIcon',\n  props: {\n    prefix: {\n      type: String,\n      default: 'icon',\n    },\n    name: {\n      type: String,\n      required: true,\n    },\n    color: {\n      type: String,\n      default: '#333',\n    },\n  },\n  setup(props) {\n    const symbolId = computed(() =\u003e `#${props.prefix}-${props.name}`)\n    return { symbolId }\n  },\n})\n\u003c/script\u003e\n```\n\n#### **Icons Directory Structure**\n\n```bash\n# src/icons\n\n- icon1.svg\n- icon2.svg\n- icon3.svg\n- dir/icon1.svg\n```\n\n`/src/App.vue`\n\n```vue\n\u003ctemplate\u003e\n  \u003cdiv\u003e\n    \u003cSvgIcon name=\"icon1\"\u003e\u003c/SvgIcon\u003e\n    \u003cSvgIcon name=\"icon2\"\u003e\u003c/SvgIcon\u003e\n    \u003cSvgIcon name=\"icon3\"\u003e\u003c/SvgIcon\u003e\n    \u003cSvgIcon name=\"dir-icon1\"\u003e\u003c/SvgIcon\u003e\n  \u003c/div\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\nimport { defineComponent, computed } from 'vue'\n\nimport SvgIcon from './components/SvgIcon.vue'\nexport default defineComponent({\n  name: 'App',\n  components: { SvgIcon },\n})\n\u003c/script\u003e\n```\n\n### **React way**\n\n`/src/components/SvgIcon.jsx`\n\n```jsx\nexport default function SvgIcon({\n  name,\n  prefix = 'icon',\n  color = '#333',\n  ...props\n}) {\n  const symbolId = `#${prefix}-${name}`\n\n  return (\n    \u003csvg {...props} aria-hidden=\"true\"\u003e\n      \u003cuse href={symbolId} fill={color} /\u003e\n    \u003c/svg\u003e\n  )\n}\n```\n\n#### **Icons Directory Structure**\n\n```bash\n# src/icons\n\n- icon1.svg\n- icon2.svg\n- icon3.svg\n- dir/icon1.svg\n```\n\n`/src/App.jsx`\n\n```jsx\nimport SvgIcon from './components/SvgIcon'\n\nexport default function App() {\n  return (\n    \u003c\u003e\n      \u003cSvgIcon name=\"icon1\"\u003e\u003c/SvgIcon\u003e\n      \u003cSvgIcon name=\"icon1\"\u003e\u003c/SvgIcon\u003e\n      \u003cSvgIcon name=\"icon1\"\u003e\u003c/SvgIcon\u003e\n      \u003cSvgIcon name=\"dir-icon1\"\u003e\u003c/SvgIcon\u003e\n    \u003c/\u003e\n  )\n}\n```\n\n### Get all SymbolId\n\n```ts\nimport ids from 'virtual:svg-icons-names'\n// =\u003e ['icon-icon1','icon-icon2','icon-icon3']\n```\n\n### Options\n\n| Parameter   | Type                   | Default               | Description                                                                           |\n| ----------- | ---------------------- | --------------------- | ------------------------------------------------------------------------------------- |\n| iconDirs    | `string[]`             | -                     | Need to generate the icon folder of the Sprite image                                  |\n| symbolId    | `string`               | `icon-[dir]-[name]`   | svg symbolId format, see the description below                                        |\n| svgoOptions | `boolean｜SvgoOptions` | `true`                | svg compression configuration, can be an object[Options](https://github.com/svg/svgo) |\n| inject      | `string`               | `body-last`           | svgDom default insertion position, optional `body-first`                              |\n| customDomId | `string`               | `__svg__icons__dom__` | Customize the ID of the svgDom insert node                                            |\n\n**symbolId**\n\n`icon-[dir]-[name]`\n\n**[name]:**\n\nsvg file name\n\n**[dir]**\n\nThe svg of the plug-in will not generate hash to distinguish, but distinguish it by folder.\n\nIf the folder corresponding to `iconDirs` contains this other folder\n\nexample:\n\nThen the generated SymbolId is written in the comment\n\n```bash\n# src/icons\n- icon1.svg # icon-icon1\n- icon2.svg # icon-icon2\n- icon3.svg # icon-icon3\n- dir/icon1.svg # icon-dir-icon1\n- dir/dir2/icon1.svg # icon-dir-dir2-icon1\n```\n\n## Typescript Support\n\nIf using `Typescript`, you can add in `tsconfig.json`\n\n```json\n// tsconfig.json\n{\n  \"compilerOptions\": {\n    \"types\": [\"vite-plugin-svg-icons/client\"]\n  }\n}\n```\n\n**Note**\n\nAlthough 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`.\n\nThis needs to be avoided by the developer himself\n\n## Example\n\n**Run**\n\n```bash\n\npnpm install\ncd ./packages/playground/basic\npnpm run dev\npnpm run build\n\n```\n\n## Sample project\n\n[Vben Admin](https://github.com/anncwb/vue-vben-admin)\n\n## License\n\n[MIT © Vben-2020](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvbenjs%2Fvite-plugin-svg-icons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvbenjs%2Fvite-plugin-svg-icons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvbenjs%2Fvite-plugin-svg-icons/lists"}