Created
February 28, 2019 03:29
-
-
Save NdYAG/981ee6c7404ec3c8888ba8b7d7674e6e to your computer and use it in GitHub Desktop.
webpack loader for font subset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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