@jayson991/svg2font
v1.0.0
Published
Generate icon fonts from SVG files — TypeScript/WASM library
Readme
@jayson991/svg2font
Generate icon fonts from SVG files — TypeScript/WASM library
A WebAssembly module wrapping the same Rust core as the CLI. Provides full TypeScript types and async/await support. Zero JavaScript font dependencies — all font generation happens in Rust compiled to WASM.
For the CLI tool, see @jayson991/svg2font-cli.
Requirements
- Node.js 18 or higher
Installation
npm install @jayson991/svg2font
# Or with pnpm
pnpm add @jayson991/svg2font
# Or with yarn
yarn add @jayson991/svg2fontQuick Start
import { generate } from '@jayson991/svg2font';
const result = await generate({
src: 'icons/**/*.svg',
dist: 'dist',
fontName: 'myicons',
});
console.log(`Generated ${result.glyphs.length} icons`);
console.log(`Bundle: ${result.zipPath}`);API
generate(options): Promise<GenerateResult>
export function generate(options: GenerateOptions): Promise<GenerateResult>;GenerateOptions
| Field | Type | Required | Default | Description |
|-------|------|----------|---------|-------------|
| src | string | Yes | — | Glob pattern for source SVG files (e.g. "icons/**/*.svg") |
| dist | string | Yes | — | Output directory |
| fontName | string | Yes | — | Font family name used in file names and CSS |
| prefix | string | No | "icon" | CSS class prefix |
| startCodepoint | number | No | 57345 (0xe001) | Starting Unicode codepoint |
GenerateResult
| Field | Type | Description |
|-------|------|-------------|
| glyphs | GlyphMeta[] | Metadata for every generated icon |
| zipPath | string | Absolute path to the generated ZIP archive |
GlyphMeta
| Field | Type | Description |
|-------|------|-------------|
| name | string | Icon name in kebab-case (e.g. "arrow-right") |
| codepoint | number | Assigned Unicode codepoint (e.g. 57345) |
| unicode | string | HTML entity (e.g. "") |
| className | string | Full CSS class name (e.g. "icon-arrow-right") |
Examples
Basic usage
import { generate } from '@jayson991/svg2font';
const result = await generate({
src: 'src/assets/icons/**/*.svg',
dist: 'public/fonts',
fontName: 'myicons',
prefix: 'icon',
});
for (const glyph of result.glyphs) {
console.log(`${glyph.className} → ${glyph.unicode}`);
}Custom codepoint range
import { generate } from '@jayson991/svg2font';
import { writeFileSync } from 'fs';
const result = await generate({
src: 'icons/**/*.svg',
dist: 'dist',
fontName: 'design-system',
prefix: 'ds',
startCodepoint: 0xf000,
});
// Write a custom mapping file
const mapping = Object.fromEntries(
result.glyphs.map(g => [g.name, { unicode: g.unicode, className: g.className }])
);
writeFileSync('icon-map.json', JSON.stringify(mapping, null, 2));Build script integration
// scripts/build-icons.ts
import { generate } from '@jayson991/svg2font';
async function main() {
const result = await generate({
src: 'design/icons/**/*.svg',
dist: 'src/assets/fonts',
fontName: 'app-icons',
prefix: 'icon',
});
console.log(`Built ${result.glyphs.length} icons → ${result.zipPath}`);
}
main().catch(console.error);Output
The dist/{fontName}/ directory is created containing:
{name}.ttf— TrueType font{name}.woff— WOFF (zlib){name}.woff2— WOFF2 (brotli){name}.eot— EOT (IE11){name}.svg— SVG font{name}.css— Stylesheet with@font-face{name}.js— SVG sprite auto-injector{name}.symbol.svg— SVG sprite{name}.json— Glyph metadatademo.html/demo.css— Interactive preview
A ZIP archive is also written at {dist}/{fontName}.zip.
License
MIT © Jayson Wu
