A Vite Plugin that generates fonts from your SVG icons and allows you to use your icons in your HTML.
vite-svg-2-webfont uses the webfonts-generator package to create fonts in any format.
It also generates CSS files so that you can use your icons directly in your HTML, using CSS classes.
npm i -D vite-svg-2-webfont
yarn add -D vite-svg-2-webfont
pnpm add -D vite-svg-2-webfont
Add the plugin to the vite.config.ts with the wanted setup, and import the virtual module, to inject the icons CSS font to the bundle.
Add the plugin to your vite configs plugin array.
// vite.config.ts
import { resolve } from 'path';
import { defineConfig } from 'vite';
import viteSvgToWebfont from 'vite-svg-2-webfont';
export default defineConfig({
plugins: [
viteSvgToWebfont({
context: resolve(__dirname, 'icons'),
}),
],
});Import the virtual module into the app
// main.ts
import 'virtual:vite-svg-2-webfont.css';Use the font in templates with the icon font class and an icon class name.
The default font class name is .icon and can be overriden by passing the baseSelector configuration option.
Icon class names are derived from their .svg file name, and prefixed with the value of classPrefix which defaults to icon-.
In the below example, the add class would display the icon created from the file {context}/add.svg
<i class="icon icon-add"></i>The plugin has an API consisting of one required parameter and multiple optional parameters allowing to fully customize plugin setup.
stringsvg files will execute. The SVG files will be used to generate the icon font.string['*.svg']string'iconfont'stringpath.resolve(context, '..', 'artifacts')stringpath.join(dest, fontName + '.css')type: string
description: Path of custom CSS template. Generator uses handlebars templates. Tht template receives options from templateOptions along with the following options:
string – Value of the src property for @font-face.object - Codepoints of icons in hex format.Paths of default templates are stored in the webfontsGenerator.templates object.
webfontsGenerator.templates.css – Default CSS template path. It generates classes with names based on values from options.templateOptions.webfontsGenerator.templates.scss – Default SCSS template path. It generates mixin webfont-icon to add icon styles. It is safe to use multiple generated files with mixins together.See webfonts-generator#csstemplate
stringcssDeststringpath.join(dest, fontName + '.html')stringoptions.templateOptions along with the following options:
string – Styles generated with default CSS template. (cssFontsPath is changed to relative path from htmlDest to dest)string[] – Names of icons.booleantruebooleanfalsenumber10e12number0booleanfalsenumberbooleanfalseboolean | string | string[]true Generate all file types.false Generate no files.'html' - Generate a HTML file'css' - Generate CSS file'fonts' - Generate font files (based on the types requested)falsestring | string[]svgttfwoffwoff2eot['eot', 'woff', 'woff2', 'ttf', 'svg']{ [key: string]: number }startCodepoint skipping duplicates.string'icon-'string'.icon'{ [format in 'svg' | 'ttf' | 'woff2' | 'woff' | 'eot']?: unknown };string'vite-svg-2-webfont.css'booleanfalsebooleanfalseThe plugin exposes a public API that can be used inside another plugins using Rollup inter-plugin communication mechanism.
Currently available methods:
Array<{ type: 'svg' | 'ttf' | 'woff2' | 'woff' | 'eot', href: string }>type - a font format generated by a pluginhref - a path to a generated font