Quick Start
Installation
Prerequisites
Ensure that your workspace is set up correctly by running the following commands:
pnpm add -D vite-plugin-svg-icons-ng
npm i -D vite-plugin-svg-icons-ng
yarn add -D vite-plugin-svg-icons-ng
Usage
Import and configure the plugin in vite.config.ts
/vite.config.js
// ...
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons-ng'
import path from 'node:path'
export default defineConfig({
plugins: [
// ...
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), 'src/icons')],
}),
],
})
// ...
import path from 'node:path'
export default defineConfig({
plugins: [
// ...
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), 'src/icons')],
}),
],
})
Then, Import the virtual module in entry file, like main.ts
/main.js
.
import 'virtual:svg-icons/register' // recommended
import 'virtual:svg-icons-register' // deprecated
import 'virtual:svg-icons/register' // recommended
import 'virtual:svg-icons-register' // deprecated
WARN
The virtual module virtual:svg-icons-register
will be deprecated in the future. Because it doesn't comply with the namespace specification.
You can use import 'virtual:svg-icons/register'
instead.
Here, the svg sprite will have been generated. Can use through public component.
Or can use the sprite directly in the HTML.
<svg>
<use xlink:href="#icon-icon1"></use>
</svg>
Get all SymbolId
Can get all SymbolId through virtual:svg-icons/ids
import ids from 'virtual:svg-icons/ids' // recommended
import ids from 'virtual:svg-icons-names' //deprecated
console.log(ids) // ['icon-icon1','icon-icon2','icon-icon3']
WARN
The virtual module virtual:svg-icons-names
will be deprecated in the future. Because it doesn't comply with the namespace specification.
You can use import ids from 'virtual:svg-icons/ids'
instead.