Skip to content

Instantly share code, notes, and snippets.

@RobertWHurst
Created June 20, 2020 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertWHurst/5d77902f269f9d26a1a87882228bd692 to your computer and use it in GitHub Desktop.
Save RobertWHurst/5d77902f269f9d26a1a87882228bd692 to your computer and use it in GitHub Desktop.
Convert ttf fonts to woff and woff2
const ttf2woff = require('ttf2woff')
const ttf2woff2 = require('ttf2woff')
const fs = require('fs')
const path = require('path')
const dir = fs.readdirSync('./')
for (const file of dir) {
const ext = path.extname(file)
if (ext !== '.woff' && ext !== '.woff2') {
continue
}
console.log(`deleting ${file}`)
fs.unlinkSync(file)
}
for (const file of dir) {
const ext = path.extname(file)
if (ext !== '.ttf') {
continue
}
const basename = path.basename(file, ext)
console.log(`reading ${basename}.ttf`)
const ttfSrc = fs.readFileSync(file)
console.log(`converting ${basename}.ttf to woff`)
const woffSrc = ttf2woff(ttfSrc)
console.log(`converting ${basename}.ttf to woff2`)
const woff2Src = ttf2woff2(ttfSrc)
console.log(`creating ${basename}.woff`)
fs.writeFileSync(`${basename}.woff`, woffSrc)
console.log(`creating ${basename}.woff2`)
fs.writeFileSync(`${basename}.woff2`, woff2Src)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment