Configuration
Reference for webfont() options, cosmiconfig files, and svgicons2svgfont parameters. CLI flags map to the same names — see the CLI reference.
Result
webfont() resolves to an object with generated font buffers (and optional template output). The config property contains the effective options used for the run (defaults, discovered config, and any options you passed in), plus optional output metadata when a configuration file was found or loaded.
Result and ResultConfig are exported from the package entry:
import { webfont, type Result, type ResultConfig } from "webfont";
const result: Result = await webfont({ files: "src/svg-icons/**/*.svg" });
const config: ResultConfig | undefined = result.config;result.config.filePath
- Type:
string|undefined - Description: Absolute path to the configuration file that was discovered or loaded. Omitted when no configuration file was found and defaults were used.
- Note: Output-only metadata — not an input option. Do not set
filePathin.webfontrc,package.json, or thewebfont()call.
files
- Type:
string|array - Description: A file glob, or array of file globs. Ultimately passed to fast-glob to figure out what files you want to get.
- SVG mode: one or more
.svgicon paths or globs (default pipeline). - TTF encoding mode: one or more
.ttfpaths or globs. - Webfont decompress mode: one or more
.woff/.woff2paths, globs, orhttps://…URLs (seeformatsand Capabilities at a glance). - Do not mix
.svg,.ttf, and.woff/.woff2in the same run. - Every matched file must have a supported extension (
.svg,.ttf,.woff,.woff2); extension-less files such asLICENSEare not ignored when matched by a broad glob. node_modulesandbower_componentsare always ignored.
configFile
- Type:
string - Description: Path to a specific configuration file
(JSON, YAML, or CommonJS)or the name of a module innode_modulesthat points to one. - Note: If you do not provide
configFile, webfont will search up the directory tree for configuration file in the following places, in this order:- a
webfontproperty inpackage.json - a
.webfontrcfile (with or without filename extension:.json,.yaml, and.jsare available) - a
webfont.config.jsfile exporting a JSobject. The search will begin in the working directory and move up the directory tree until it finds a configuration file.
- a
- Note: When a configuration file is discovered or loaded, the resolved absolute path is available on
result.config.filePath(see Result).
fontName
- Type:
string - Default:
webfont - Description: The font family name you want.
formats
- Type:
array - Default:
['svg', 'ttf', 'eot', 'woff', 'woff2'](SVG input). For TTF input, defaults to['woff', 'woff2']when SVG-pipeline formats are still configured. For WOFF/WOFF2 input, defaults to['ttf']when SVG-pipeline formats are still configured. - Possible values:
svg,ttf,otf,eot,woff,woff2 - Description: Font file types to generate.
- SVG input:
svg,ttf,eot,woff,woff2only.otfis not supported (pipeline uses TrueType outlines). - TTF input:
svg(SVG font),ttf,eot,woff, and/orwoff2.svgis opt-in (not in the defaultwoff+woff2set);otfis not produced in this mode. - WOFF/WOFF2 input:
ttfand/orotfonly — must match the decompressed SFNT flavor inside the file (not arbitrary transcoding).eot,woff,woff2, andsvgare not produced in this mode.
- SVG input:
- CLI: pass
-f/--formatsas a JSON array (for example'["woff2"]') or as a comma-separated list (for examplewoff2orsvg, ttf, woff2). Invalid format names throw an error. - API and config files:
formatsmust be an array of the values above; unknown names (for exampleicon) throw before the pipeline runs.
template
Type:
stringorstring[]Default:
nullPossible values:
css,scss,styl,html,json(feel free to contribute more), or a path to a custom.njktemplate.Pass an array to generate multiple outputs in one run (for example
['html', 'scss']for preview + styles, #158).CLI: pass
-t/--templateas a single name, a JSON array ('["html","scss"]'), or a comma-separated list (html,scss).Note: If you want to use a custom template use this option pass in a path
stringlike this:jswebfont({ template: "./path/to/my-template.css", });Or
jswebfont({ template: path.resolve(__dirname, "./my-template.css"), });Or
jswebfont({ template: path.resolve(__dirname, "./my-template.styl"), });
templateClassName
- Type:
string - Default:
null - Description: Default font class name.
templateFontPath
- Type:
string - Default:
./ - Description: Path to generated fonts in the
CSSfile.
templateFontName
- Type:
string - Default: Same as
fontNamewhen not set. - Description: CSS
font-familyname emitted by built-in templates (fontFamilyin Nunjucks context). Output font files are always named afterfontName(for exampleicons-a.woff2); built-in templates reference those files with NunjucksfontName/fontFamilyplaceholders inurl(...)andfont-family(#331).
templateCacheString
- Type:
string - Default:
Date.now()at generation time - Description: Query string appended to font
url(...)values in built-in templates (before optional&v=hash). Use a fixed value (for examplev=1) when you only need manual cache busting; pair withaddHashInFontUrlfor automatic content-based busting.
addHashInFontUrl
- Type:
boolean - Default:
false - Description: When
trueand a built-intemplateis used, append&v=<md5>to each font URL in the generated CSS/SCSS/Styl. The hash is derived from the SVG font output — it changes only when icon data changes. Output filenames stayfontName.woff2,fontName.scss, etc. (stable@importpaths). - CLI:
--addHashInFontUrl - Addresses browser cache issues without renaming font files on every build (#125).
Example — stable webfont.scss, cache-busted font URLs:
webfont "icons/*.svg" -d dist/icons -t scss --addHashInFontUrlimport { webfont } from "webfont";
await webfont({
files: "src/icons/**/*.svg",
fontName: "webfont",
template: "scss",
templateFontPath: "./fonts/",
addHashInFontUrl: true,
});
// Writes dist/icons/webfont.scss and dist/icons/webfont.woff2 (fixed names).
// SCSS contains url("./fonts/webfont.woff2?...&v=<md5>") — hash updates when icons change.Do not use Math.random() in fontName — that renames both font files and the stylesheet on every run.
unicodeRange
- Type:
boolean | string - Default:
false(omitunicode-rangefrom built-in templates) - Description: When
trueand a built-intemplateis used, emit aunicode-rangedeclaration in@font-faceso the browser can load only the icon font that owns the code points on the page. With the defaultstartUnicode(0xEA01), the range is derived from the minimum and maximum allocated code points (for exampleU+EA01-EA03). Pass a CSS value string (for exampleU+EA01-EAFF) to override the computed range. Set tofalseto omit the rule (default). - CLI:
--unicode-rangeto enable auto-computed range - Helps when multiple icon fonts share a page (#322) — each
@font-facecan declare which private-use code points it owns so the browser skips downloading fonts that cannot render the glyphs in use. - Ligature trade-off: computed
unicode-rangecovers private-use code points only (ligature names are ignored when building the range). If you type icon names with OpenType ligatures (font-feature-settings: "liga"), a PUA-onlyunicode-rangeprevents the browser from applying the icon font to ASCII letters, so ligatures do not render. Prefer class + codepoint icons in production whenunicode-rangeis enabled, or leaveunicodeRangeat the defaultfalseif you rely on ligature names.
ligatures
- Type:
boolean - Default:
false(ligature glyphs are not added unless you opt in) - Description: When
true, each glyph gets a second unicode entry: the icon name with hyphens replaced by underscores (for examplephone-call→phone_call). Browsers can map that character sequence to the icon glyph when OpenType ligatures are enabled (font-feature-settings: "liga"). - CLI:
--ligaturesto enable ligature glyphs in the font. - Why off by default: large icon sets (thousands of glyphs) produce huge GSUB tables. Firefox on Windows (DirectWrite) can hang or slow dramatically when ligatures are enabled (#558). Prefer class + private-use codepoint CSS for MDI-scale fonts. webfont prints a warning when glyph count exceeds 2000 with ligatures enabled.
templateFontLigatures
- Type:
boolean - Default:
true - Description: When
trueand the built-inhtmlpreview template is used, the generated CSS enablesfont-feature-settings: "liga"on the Ligature section (#icon-ligatures) so ligature names render as icons instead of invisible/missing glyphs. If you also enableunicodeRange, the HTML template omitsunicode-rangeon@font-facewhile ligature preview is on (ligature strings are ASCII; a PUA-onlyunicode-rangewould block the icon font for those characters). Set tofalseif you inject your own ligature CSS. - CLI:
--no-template-font-ligaturesto omit the CSS rule. - Requires
ligaturesenabled (--ligatures/ligatures: true). Class-based icons (.font-name-icon::beforewith a private-use codepoint) work without this option.
glyphTransformFn
Type:
functionDefault:
nullDescription: If you want to transform glyph metadata (e.g. titles of CSS classes or unicode) before transferring it in your style template for your icons, you can use this option with glyphs metadata object.
Example:
jsimport { webfont } from "webfont"; webfont({ files: "src/svg-icons/**/*.svg", glyphTransformFn: (obj) => { obj.name += "_transform"; something(); return obj; }, }) .then((result) => { // Do something with result Function.prototype(result); // Or return it return result; }) .catch((error) => { throw error; });
glyphContentTransformFn
Type:
functionDefault:
nullDescription: Transform each SVG glyph before font generation (SVG pipeline only). Use this hook to preprocess SVGs — for example stroke-based icons converted to filled paths. webfont does not bundle preprocessors; install tools such as
svg-outline-strokein your project and call them from this function (see ADR 0011).glyphTransformFnonly changes metadata; this hook receives the fullGlyphData(contents,srcPath, optionalmetadata) and must return the new SVG string.Example (stroke icons, #144 — install
svg-outline-strokein your app, not in webfont):jsimport outlineStroke from "svg-outline-stroke"; import { webfont } from "webfont"; await webfont({ files: "src/svg-icons/**/*.svg", glyphContentTransformFn: async (glyph) => outlineStroke(glyph.contents), });For better trace quality with
svg-outline-stroke, use a largerwidth/height/viewBoxon the source SVG (see the library README).
optimizeSvg
Type:
booleanDefault:
falseDescription: When
true, run a conservative SVGO pass on each SVG after diagnostics and beforeglyphContentTransformFn. Removes editor cruft (comments, metadata, doctype) without rewriting paths or removingviewBox. Does not convert stroke-only artwork to fills — pair withglyphContentTransformFnfor stroke icons (#327, #724).CLI:
--optimize-svgOptional:
svgoConfig— SVGOConfigobject; whenpluginsis set, it replaces the built-in conservative plugin list.jsimport { webfont } from "webfont"; await webfont({ files: "src/icons/**/*.svg", optimizeSvg: true, glyphContentTransformFn: async (glyph) => { // optional: stroke-to-fill after SVGO cleanup return glyph.contents; }, });
svgTools (alpha)
Type:
{ diagnose?: boolean; onMessage?: (message: string) => void }Default:
undefined(disabled)Description: Alpha. Scan SVG sources for icon-font incompatibilities before font generation (SVG pipeline only). Does not modify SVGs — use
glyphContentTransformFnto preprocess (for example stroke-to-fill withsvg-outline-stroke, installed in your project). See ADR 0011.diagnose: log warnings forfill-rule: evenodd, stroke-only paths (fill="none"+stroke),<use>symbol references (#612), and poorly supported elements (<line>,<polyline>,<clipPath>). Results are also returned onresult.svgDiagnosticswhen enabled.onMessage: optional sink for diagnostic log lines (tests, custom UIs).
CLI:
--svg-diagnose(alpha).Example:
jsimport { webfont } from "webfont"; const result = await webfont({ files: "src/icons/**/*.svg", svgTools: { diagnose: true }, }); console.log(result.svgDiagnostics);See TROUBLESHOOTING.md — “Icon details missing after export”.
ttfPostProcess
Type:
functionDefault:
undefinedDescription: Post-process the generated TTF buffer after it is built and before webfont derives WOFF/WOFF2/EOT from it (SVG pipeline only). The callback receives
(ttf, { fontName, formats })and returns the new font bytes (BufferorUint8Array, sync or async). Every derived format is produced from the returned buffer. This is a caller-owned extension point — webfont bundles nothing here, so optional/native steps such as autohinting (ttfautohint) live in your project or a separate package, keeping the core free of native dependencies (same philosophy asglyphContentTransformFnand ADR 0011; see #749).Example (autohinting — install the hinting tool in your app, not in webfont):
jsimport { webfont } from "webfont"; import { autohint } from "some-ttfautohint-wrapper"; await webfont({ files: "src/svg-icons/**/*.svg", formats: ["ttf", "woff2"], ttfPostProcess: async (ttf) => autohint(ttf), });
metadataProvider
Type:
functionDefault: built-in metadata service (reads icon names and unicode from SVG files)
Description: Custom callback to resolve glyph metadata for each source file. Receives the file path and a Node-style callback
(error, metadata)wheremetadatais{ name: string, unicode?: string | string[] }. When omitted, webfont uses its default metadata reader.jsimport { webfont } from "webfont"; webfont({ files: "src/svg-icons/**/*.svg", metadataProvider: (srcPath, callback) => { callback(null, { name: "custom-icon-name" }); }, });
sort
- Type:
bool - Default:
true - Description: Whether you want to sort the icons sorted by name.
svgicons2svgfont
These options are passed directly to svgicons2svgfont as top-level webfont() fields (same names as CLI flags).
fontName
- Type:
string - Default: Taken from the fontName
- Description: The font family name you want.
fontId
- Type:
string - Default: The
fontNamevalue - Description: The font id you want.
fontStyle
- Type:
string - Default:
'' - Description: The font style you want.
fontWeight
- Type:
string - Default:
'' - Description: The font weight you want.
fixedWidth
- Type:
boolean - Default:
false - Description: Creates a monospace font of the width of the largest input icon.
centerHorizontally
- Type:
boolean - Default:
false - Description: Calculate the bounds of a glyph and center it horizontally.
centerVertically
- Type:
boolean - Default:
false - Description: Center the glyphs vertically in the generated font.
normalize
- Type:
boolean - Default:
false - Description: Normalize icons by scaling them to the height of the highest icon.
fontHeight
- Type:
number - Default:
MAX(icons.height) - Description: The outputted font height (defaults to the height of the highest input icon).
round
- Type:
number - Default:
10e12Setup SVG path rounding.
descent
- Type:
number - Default:
0 - Description: The font descent. It is useful to fix the font baseline yourself.
- Warning: The descent is a positive value!.
ascent
- Type:
number - Default:
fontHeight - descent - Description: The font ascent. Use this options only if you know what you're doing. A suitable value for this is computed for you.
metadata
- Type:
string - Default:
undefined - Description: The font metadata. You can set any character data in, but this is the recommended place for a copyright mention.
Are you a contributor?
Update this file in the same PR when webfont() options, defaults, accepted values, or svgicons2svgfont forwarding change. See CONTRIBUTING.md.