Usage
Standard setup
The typical integration has three parts:
Add the plugin to
vite.config.tstsimport { resolve } from 'node:path'; import { defineConfig } from 'vite'; import viteSvgToWebfont from 'vite-svg-2-webfont'; export default defineConfig({ plugins: [ viteSvgToWebfont({ context: resolve(import.meta.dirname, 'icons'), }), ], });Import
virtual:vite-svg-2-webfont.csstsimport 'virtual:vite-svg-2-webfont.css';Use generated class names in markup, for example:
html<i class="icon icon-add"></i>
How class names are generated
- The default base selector is
.icon, seebaseSelector - The default class prefix is
icon-, seeclassPrefix - SVG file names become icon names (e.g.
add.svgbecomes{classPrefix}-add)
For example, if context contains add.svg, the generated CSS class is icon-add, and you would use it like this:
html
<i class="icon icon-add"></i>If you want different class names, change classPrefix and baseSelector.
Development file output
By default, the plugin does not write generated assets to disk during development. If you want preview artifacts while iterating, use generateFiles to enable file output and specify which files to generate:
ts
viteSvgToWebfont({
context: './src/icons',
generateFiles: ['css', 'fonts', 'html'],
});Build-time behavior
- Preload tags can be injected into built HTML with
preloadFormats - Preload injection can be limited to selected HTML entrypoints with
shouldProcessHtml - When
inlineistrue, no preload tags are injected because assets are embedded in the CSS - File output during build is disabled unless
allowWriteFilesInBuildis enabled
Related options
Common options you may want to adjust early:
See the full Configuration reference for details.