node package manager
Love JavaScript? Your insights can make it even better. Take the 2017 JavaScript Ecosystem Survey »



Replace image refs with base64 encoding in css files

Ex: Localize a library so you aren't reliant on external uptime


.ui-menu .ui-menu-item {
    list-style-image: url("images/ui-bg_flat_75_ffffff_40x100.png");


.ui-menu .ui-menu-item {

Run via JS

var baser = require('base64-css')
baser('./libs/dulce-ui/dui.css', function(err,res){
    if (!err)
        // do something with parsed css 

Run via CLI

# parse local file, writing to target in place:
node bin/cli.js -f ./test/lib.css
# parse local file, saving result in specified directory:
node bin/cli.js -f ./test/lib.css ./assets/
# parse local file, passing remote location prefix for assets:
node bin/cli.js -f ./test/lib.css -p
# parse remote file, saving result in specified directory:
node bin/cli.js -f ./assets/


Images that cannot be located are logged to console.warn and skipped

baser( filePath[, assetPrefix], callback )

filePath Path of CSS file to be parsed; can be local or an http(s) url

assetPrefix (optional) Directory to use as base when locating images; can be local or an http(s) url; if omitted, normal CSS rules are followed

callback Format (err,parsedSource), which can be passed to fs.writeFile

node bin/cli.js -f file_path [-p asset_prefix] [target_directory]

-f file_path Path of CSS file to be parsed

-p asset_prefix (optional) Asset prefix override

target_directory (optional) Directory in which to save result; if omitted, source file is updated in place

To Do

  • Accept directory as input, as opposed to just a single css file
    • Run against all css files in dir