This gulp task removes all unused characters from fonts. It recursively scans the build directory and injects its text contents into fontmin. The task does additionally producewoff2
files next to the default types of eot
, svg
, ttf
and woff
. It's a great post-processing routine to keep your assets small and to get rid of the keep request counts low and transfer sizes small notification in your lighthouse report.
Execution
gulp fontmin
Code
const fontmin = require('gulp-fontmin')
const ttf2woff2 = require('gulp-ttf2woff2')
function minifyFont(text, cb) {
gulp
.src('source/fonts/*.ttf')
.pipe(
fontmin({
text: text
})
)
.pipe(ttf2woff2({ clone: true }))
.pipe(gulp.dest('build/fonts'))
.on('end', cb)
}
gulp.task('fontmin', function (cb) {
let buffers = []
gulp
.src('build/**/*.html')
.on('data', function (file) {
buffers.push(file.contents)
})
.on('end', function () {
let text = Buffer.concat(buffers).toString('utf-8')
minifyFont(text, cb)
})
})