@jo-hemphill/favigen
v2.0.0
Published
Library-first favicon and web app icon generator for bundlers and other integrating applications.
Maintainers
Readme
Favigen
Favigen turns one SVG or raster logo into favicon and app icon assets. Build tools can inspect the plan, render selected assets, and bind the emitted filenames and URLs before generating HTML tags or handing icon entries to a manifest provider.
Favigen 2 is ESM-only and requires Node.js 22.12 or newer. Use import in an ES module, or await import('@jo-hemphill/favigen') from asynchronous CommonJS code.
Install from the npm registry with either package manager.
pnpm add @jo-hemphill/favigen
npm install @jo-hemphill/favigenJimp handles raster processing, and WebAssembly codecs handle SVG and WebP. A standard install includes the codecs and needs no platform-specific image addons.
To build from source, use Node.js 22.18 or later in the 22.x line, or Node.js 24 or newer, and the pnpm version in package.json. Run pnpm install --frozen-lockfile, pnpm test, and pnpm pack.
Generate icons from one source
import { bindIcons, iconTags, planIcons, prepareSource, renderIcons } from '@jo-hemphill/favigen';
const source = await prepareSource('./assets/logo.svg');
const plan = planIcons(source);
const icons = await renderIcons(plan);
const bindings = new Map(icons.map((icon) => [icon.id, {
outputFileName: `assets/${icon.name}`,
url: `/assets/${icon.name}`,
}]));
const tags = iconTags(bindIcons(icons, bindings));Write or emit each icon's bytes through your build tool. In the example, replace the illustrative bindings with its actual filenames and URLs. The default plan contains a multi-size ICO, 32 and 48 px PNGs, and a 180 px Apple touch icon. Use profile: 'pwa' to add 192 and 512 px app icons with both ordinary and maskable variants.
Paths, byte arrays and array buffers are accepted. Raster sources can be static PNG, JPEG or WebP. SVG input supports self-contained vector artwork with outlined text and local references. Favigen checks vector visibility at the selected crop and target size, so a tiny mark in a large canvas can still render when you choose a tight crop. A target that would be blank raises an error. Export complex SVG artwork with filters, stylesheet blocks or embedded images to PNG if it is outside the supported subset.
Customize a specific role
const plan = planIcons(source, {
profile: 'pwa',
variants: {
favicon: { source: await prepareSource('./assets/small-mark.png'), trim: true },
touch: { background: '#182230' },
},
targets: {
'app.any.192': { crop: [0.125, 0, 0.75, 1] },
},
});contain fits the whole crop. cover fills the padded area and clips overflow at that boundary. Crop coordinates are normalized fractions of the oriented source rectangle, after optional transparent-margin trimming. position anchors the artwork along two axes and padding reserves a fraction of the output size on each edge. Use a solid hex background or choose transparent on roles that support it. Use resample: 'nearest' for pixel art. Inspect plan.diagnostics for small-icon aspect ratio and raster upscaling feedback.
Render and publish independently
renderIcon(plan, id)renders one icon.renderIcons(plan, { ids })renders a subset or the entire plan.- The default bounded memory cache reuses matching render recipes. Pass
createDiskCache(directory)to reuse matching bytes across processes, or passcache: falseto bypass caching. ICO and PNG assets can share cached planes. bindIconsstrips the binary payload from the published records and attaches actual bundle names and URLs. Rebind the same rendered icons for each deployment or manifest location.manifestIcons(publication)builds app icon entries for your manifest provider.buildBasicManifest(publication, options)returns an optional basic manifest payload. The caller owns file emission and the manifest link. UsemanifestUrlandurlStyle: 'relative'for links relative to a nested manifest, and setappUrlwhen the app and asset deployment bases differ.
The draft Favigen class and one-shot favigen(source, options) entry point remain available. The legacy image hook uses Jimp 2 and accepts positional resize and contain, along with getBufferAsync, getWidth, and getHeight. See the Jimp 2 migration guide for other image methods. Use the staged API for SVG and incremental rendering. See the design proposal for the Vite adapter and validation plan.
