Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from garth/gist:1388969
Created November 23, 2011 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eliperelman/1390043 to your computer and use it in GitHub Desktop.
Save eliperelman/1390043 to your computer and use it in GitHub Desktop.
Convert url image ref into inline base 64 in css with nodejs
var css = 'insert lots of css here';
var files = {};
css = css.replace(/url\((\S*)\.(png|jpg|jpeg|gif)\)/g, function(match, file, type)
{
var fileName = file + '.' + type;
var base64 = fs.readFileSync(fileName).toString('base64');
if (typeof(files[fileName]) !== 'undefined') {
console.log('Warning: ' + fileName + ' has already been base64 encoded in the css');
}
files[fileName] = true;
return 'url("data:image/' + (type === 'jpg' ? 'jpeg' : type) + ';base64,' + base64 + '")';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment