Gulp Iconfont is a TypeScript Gulp plugin that creates icon fonts from SVG icon files. It transforms collections of vector graphics into web fonts that can be deployed across different browsers and platforms, supporting multiple font formats (TTF, EOT, WOFF, WOFF2, SVG).
npm install --save-dev gulp-iconfontimport gulpIconFont from "gulp-iconfont";
import type { GulpIconFontOptions } from "gulp-iconfont";For CommonJS:
const gulpIconFont = require("gulp-iconfont");import gulp from "gulp";
import gulpIconFont from "gulp-iconfont";
const runTimestamp = Math.round(Date.now() / 1000);
gulp.task('iconfont', function () {
return gulpIconFont('assets/icons/*.svg', {
fontName: 'myfont', // required
prependUnicode: true, // recommended option
formats: ['ttf', 'eot', 'woff'], // default, 'woff2' and 'svg' are available
timestamp: runTimestamp, // recommended to get consistent builds when watching files
})
.on('glyphs', function (glyphs, options) {
// CSS templating, e.g.
console.log(glyphs, options);
})
.pipe(gulp.dest('www/fonts/'));
});Gulp Iconfont orchestrates multiple font conversion tools through a unified API that handles the complete pipeline from SVG input to multiple font format outputs:
Main function that creates icon fonts from SVG files with comprehensive format support and configuration options.
/**
* Creates icon fonts from SVG files (default export)
* @param glob - File path pattern(s) to SVG icon files
* @param options - Configuration options combining GulpIconFontOptions and SVGIcons2SVGFontOptions
* @returns A Gulp stream that can be piped and emits 'glyphs' events
*/
export default function gulpIconFont(
glob: string | string[],
options: Partial<GulpIconFontOptions> & SVGIcons2SVGFontOptions
): NodeJS.ReadWriteStream;
/**
* Type definition for gulp-iconfont specific options (exported type)
*/
export type GulpIconFontOptions = {
/** Array of font formats to generate */
formats: ('ttf' | 'eot' | 'woff' | 'woff2' | 'svg')[];
/** Boolean flag for cloning (automatically set based on formats) */
clone: boolean;
/** Auto-hinting configuration: false, true, or path to ttfautohint binary */
autohint: boolean | string;
/** Generation timestamp for consistent builds */
timestamp: number;
};Note: SVGIcons2SVGFontOptions is imported from gulp-svgicons2svgfont and includes the following key options:
fontName (required): Name of the generated fontprependUnicode?: boolean: Prepend unicode to filenamesstartUnicode?: number: Starting unicode codepoint (default: 0xEA01)fontHeight?: number: Height of the fontfontWeight?: number: Font weightfontStyle?: string: Font style (e.g., 'normal', 'italic')normalize?: boolean: Normalize glyph sizescenterHorizontally?: boolean: Center glyphs horizontallycenterVertically?: boolean: Center glyphs verticallyfixedWidth?: boolean: Use fixed width for all glyphsdescent?: number: Font descent valueascent?: number: Font ascent valueround?: number: Rounding precision for coordinatesmetadata?: string: Font metadatametadataProvider?: any: Metadata provider functioninterface Glyph {
/** Name of the glyph */
name: string;
/** Unicode codepoints for the glyph */
unicode: string[];
/** Optional color information */
color?: string;
}Usage Examples:
import gulp from "gulp";
import gulpIconFont from "gulp-iconfont";
// Basic font generation
gulp.task('iconfont', () => {
return gulpIconFont('icons/*.svg', {
fontName: 'myicons',
formats: ['ttf', 'woff', 'woff2'],
timestamp: Math.round(Date.now() / 1000)
})
.pipe(gulp.dest('dist/fonts/'));
});
// With glyph mapping for CSS generation
gulp.task('iconfont-with-css', () => {
let glyphs;
return gulpIconFont('icons/*.svg', {
fontName: 'myicons',
formats: ['ttf', 'woff', 'woff2'],
prependUnicode: true,
normalize: true,
fontHeight: 1001
})
.on('glyphs', (generatedGlyphs, options) => {
glyphs = generatedGlyphs;
// Use glyphs data for CSS generation
console.log('Generated glyphs:', glyphs);
})
.pipe(gulp.dest('dist/fonts/'));
});
// With auto-hinting (requires ttfautohint installed)
gulp.task('iconfont-hinted', () => {
return gulpIconFont('icons/*.svg', {
fontName: 'myicons',
formats: ['ttf'],
autohint: true, // or path to ttfautohint binary
timestamp: Math.round(Date.now() / 1000)
})
.pipe(gulp.dest('dist/fonts/'));
});The gulp-iconfont stream emits events for accessing generated glyph mappings.
interface GulpIconFontStream extends NodeJS.ReadWriteStream {
/**
* Emitted when glyph mappings are generated
* @param event - Event name 'glyphs'
* @param handler - Handler function receiving glyphs and options
*/
on(event: 'glyphs', handler: (glyphs: Glyph[], options: any) => void): this;
}Usage Example:
gulpIconFont('icons/*.svg', { fontName: 'myicons' })
.on('glyphs', (glyphs, options) => {
// Process glyph mappings for CSS generation
glyphs.forEach(glyph => {
console.log(`${glyph.name}: ${glyph.unicode[0]}`);
});
})
.pipe(gulp.dest('fonts/'));Configure which font formats to generate from the source SVG files.
Available Formats:
'ttf': TrueType Font (included by default)'eot': Embedded OpenType (included by default, for IE support)'woff': Web Open Font Format (included by default)'woff2': Web Open Font Format 2.0 (modern browsers, smaller file size)'svg': SVG Font (deprecated but available for legacy support)Usage Examples:
// Default formats (TTF, EOT, WOFF)
gulpIconFont('icons/*.svg', {
fontName: 'myicons'
});
// Modern browsers only
gulpIconFont('icons/*.svg', {
fontName: 'myicons',
formats: ['woff2']
});
// All formats including SVG
gulpIconFont('icons/*.svg', {
fontName: 'myicons',
formats: ['svg', 'ttf', 'eot', 'woff', 'woff2']
});Fine-tune font appearance and layout characteristics.
Key Options:
fontHeight: Overall font height (minimum 500, recommended 1000+)normalize: Normalize glyph sizes for consistent appearancedescent: Font baseline descent valueascent: Font ascent value above baselinecenterHorizontally: Center glyphs horizontally in their boundscenterVertically: Center glyphs vertically in their boundsUsage Example:
gulpIconFont('icons/*.svg', {
fontName: 'myicons',
fontHeight: 1001,
normalize: true,
centerHorizontally: true,
descent: 140
});Note: If some font glyphs aren't converted properly, add normalize: true and a fontHeight greater than 1000.
Control how Unicode codepoints are assigned to glyphs.
gulpIconFont('icons/*.svg', {
fontName: 'myicons',
startUnicode: 0xEA01, // Starting Unicode codepoint
prependUnicode: true // Add Unicode to filename
});When prependUnicode: true, generated files will include Unicode prefixes in their names for easier identification.
Enable automatic font hinting using ttfautohint (requires external installation).
// Basic auto-hinting (requires ttfautohint in PATH)
gulpIconFont('icons/*.svg', {
fontName: 'myicons',
autohint: true,
formats: ['ttf']
});
// Custom ttfautohint path
gulpIconFont('icons/*.svg', {
fontName: 'myicons',
autohint: '/usr/local/bin/ttfautohint',
formats: ['ttf']
});Requirements: