Skip to content

Instantly share code, notes, and snippets.

@NdYAG
Created February 28, 2019 03:29
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 NdYAG/981ee6c7404ec3c8888ba8b7d7674e6e to your computer and use it in GitHub Desktop.
Save NdYAG/981ee6c7404ec3c8888ba8b7d7674e6e to your computer and use it in GitHub Desktop.
webpack loader for font subset
const os = require('os')
const fs = require('fs')
const path = require('path')
const exec = require('child_process').exec
const fontsubset = function(source, text, callback) {
const tmp = path.join(os.tmpdir(), 'tmp.ttf')
const subset = `node_modules/fontsubset/bin/fontsubset -s ${text} ${source} ${tmp}`
const sfntedit = path.resolve(__dirname, '../assets/font/', 'sfntedit') + ' -d vhea ' + tmp
exec(subset, function() {
exec(sfntedit, function() {
fs.readFile(tmp, function(err, data) {
callback(null, data)
})
})
})
}
module.exports = function(content) {
this.cacheable && this.cacheable();
const callback = this.async()
const source = this.resourcePath
const command = 'grep -hr --include "*.js" main_title src'
exec(command, (error, stdout, stderr) => {
let text = stdout.split('').filter(function(s) {
return s.charCodeAt() > 128
}).join('')
fontsubset(source, text, callback)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment