@digitalacorn/vite-plugin-svg-icons
Version:
Vite Plugin for fast creating SVG sprites.
291 lines (216 loc) • 7.07 kB
Markdown
Used to generate svg sprite map.
> <br/>Please Note: this is fork of [vite-plugin-svg-icons](https://www.npmjs.com/package/vite-plugin-svg-icons) <br/><br/> Initially created so I can proceed using the feature in this [pull request](https://github.com/vbenjs/vite-plugin-svg-icons/pull/68).
> <br/><br/>There has been no sign of activity from the maintainer of the above.
> <br/><br/>
- 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.
- **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.
**node version:** >=12.0.0
**vite version:** >=2.0.0
```bash
yarn add vite-plugin-svg-icons -D
npm i vite-plugin-svg-icons -D
pnpm install vite-plugin-svg-icons -D
```
- Configuration plugin in vite.config.ts
```ts
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
export default () => {
return {
plugins: [
createSvgIconsPlugin({
// Specify the icon folder to be cached
iconDirs: [path.resolve(process.cwd(), 'src/icons')],
// Specify symbolId format
symbolId: 'icon-[dir]-[name]',
/**
* custom insert position
* @default: body-last
*/
inject?: 'body-last' | 'body-first'
/**
* custom dom id
* @default: __svg__icons__dom__
*/
customDomId: '__svg__icons__dom__',
/**
* option to perform a replacement of stroke colors with currentColor
* @default:true
*/
replaceStrokeWithCurrentColor: true
}),
],
}
}
```
- Introduce the registration script in src/main.ts
```ts
import 'virtual:svg-icons-register'
```
Here the svg sprite map has been generated
`/src/components/SvgIcon.vue`
```vue
<template>
<svg aria-hidden="true">
<use :href="symbolId" :fill="color" />
</svg>
</template>
<script>
import { defineComponent, computed } from 'vue'
export default defineComponent({
name: 'SvgIcon',
props: {
prefix: {
type: String,
default: 'icon',
},
name: {
type: String,
required: true,
},
color: {
type: String,
default: '#333',
},
},
setup(props) {
const symbolId = computed(() => `
return { symbolId }
},
})
</script>
```
```bash
- icon1.svg
- icon2.svg
- icon3.svg
- dir/icon1.svg
```
`/src/App.vue`
```vue
<template>
<div>
<SvgIcon name="icon1"></SvgIcon>
<SvgIcon name="icon2"></SvgIcon>
<SvgIcon name="icon3"></SvgIcon>
<SvgIcon name="dir-icon1"></SvgIcon>
</div>
</template>
<script>
import { defineComponent, computed } from 'vue'
import SvgIcon from './components/SvgIcon.vue'
export default defineComponent({
name: 'App',
components: { SvgIcon },
})
</script>
```
### **React way**
`/src/components/SvgIcon.jsx`
```jsx
export default function SvgIcon({
name,
prefix = 'icon',
color = '#333',
...props
}) {
const symbolId = `
return (
<svg {...props} aria-hidden="true">
<use href={symbolId} fill={color} />
</svg>
)
}
```
```bash
- icon1.svg
- icon2.svg
- icon3.svg
- dir/icon1.svg
```
`/src/App.jsx`
```jsx
import SvgIcon from './components/SvgIcon'
export default function App() {
return (
<>
<SvgIcon name="icon1"></SvgIcon>
<SvgIcon name="icon1"></SvgIcon>
<SvgIcon name="icon1"></SvgIcon>
<SvgIcon name="dir-icon1"></SvgIcon>
</>
)
}
```
```ts
import ids from 'virtual:svg-icons-names'
// => ['icon-icon1','icon-icon2','icon-icon3']
```
| 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 object[Options](https://github.com/svg/svgo) |
| 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
```bash
- icon1.svg
- icon2.svg
- icon3.svg
- dir/icon1.svg
- dir/dir2/icon1.svg
```
If using `Typescript`, you can add in `tsconfig.json`
```json
// tsconfig.json
{
"compilerOptions": {
"types": ["vite-plugin-svg-icons/client"]
}
}
```
**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
**Run**
```bash
pnpm install
cd ./packages/playground/basic
pnpm run dev
pnpm run build
```
[](https://github.com/anncwb/vue-vben-admin)
[](./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](https://www.npmjs.com/package/vite-plugin-svg-icons).