Last active
August 29, 2015 13:57
-
-
Save gstf/9681190 to your computer and use it in GitHub Desktop.
Convert dir of files .svg files to optimized base64 uris compatible as LESS variables
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
#!/usr/bin/env node | |
/* | |
* Convert dir of files .svg files to optimized base64 uris | |
* compatible as LESS variables | |
*/ | |
var | |
fs = require('fs'), | |
path = require('path'), | |
svgo = require('svgo'), | |
async = require('async'); | |
var dir = process.argv.length >= 3 ? process.argv[2] : __dirname; | |
var files = fs.readdirSync(dir); | |
files = files.filter(function(fn) { | |
return path.extname(fn) == '.svg'; | |
}); | |
async.each(files, function(filepath, cb) { | |
fs.readFile(filepath, 'utf8', function(err, data) { | |
if (err) { | |
cb(err); | |
} | |
var svg = new svgo(); | |
svg.optimize(data, function(result) { | |
var fp = path.basename(filepath, '.svg').toLowerCase().replace(new RegExp('[_ ]', 'g'), '-'); | |
var base64 = new Buffer(result.data).toString('base64'); | |
console.log('@' + fp + ': url(data:image/svg+xml;base64,' + base64 + ');\n'); | |
cb(); | |
}); | |
}); | |
}, function(r) { | |
// console.log('err', r); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment