css-b64-images
Advanced tools
Comparing version 0.2.3 to 0.2.4
var fs = require('fs'), | ||
Path = require('path'), | ||
MAX_SIZE = 4096, | ||
/* Adapted from https://gist.github.com/2594980 */ | ||
@@ -26,3 +27,7 @@ imgRegex = /url\s?\(['"]?(.*?)(?=['"]?\))/gi, | ||
function fromString(css, relativePath, rootPath , cb) { | ||
function fromString(css, relativePath, rootPath , options, cb) { | ||
if(!cb) { | ||
cb = options; | ||
options = {maxSize: MAX_SIZE}; | ||
} | ||
if(!css.replace && css.toString) css = css.toString(); | ||
@@ -49,3 +54,3 @@ var urls = [], match; | ||
} | ||
replaceUrlByB64(imageUrl, imagePath, css, function (err, newCss){ | ||
replaceUrlByB64(imageUrl, imagePath, css, options, function (err, newCss){ | ||
if(err) return cb(err, css); | ||
@@ -58,14 +63,18 @@ css = newCss; | ||
function fromFile(cssFile, root, cb) { | ||
function fromFile(cssFile, root, options, cb) { | ||
if(!cb) { | ||
cb = options; | ||
options = {maxSize: MAX_SIZE}; | ||
} | ||
fs.readFile(cssFile, function(err, css){ | ||
if(err) return cb(err, css); | ||
fromString(css.toString(), Path.dirname(cssFile), root, cb); | ||
fromString(css.toString(), Path.dirname(cssFile), root, options, cb); | ||
}); | ||
} | ||
function replaceUrlByB64(imageUrl, imagePath, css, cb){ | ||
function replaceUrlByB64(imageUrl, imagePath, css, options, cb){ | ||
imagePath = imagePath.replace(/[?#].*/g, ''); | ||
fs.stat(imagePath, function(err, stat){ | ||
if(err) return cb(err, css); | ||
if (stat.size > 4096){ | ||
if (stat.size > options.maxSize){ | ||
return cb(new Error('Skip ' + imageUrl + ' Exceed max size'), css); | ||
@@ -72,0 +81,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Base64 images in your css", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"main": "lib/css-b64-images.js", | ||
@@ -8,0 +8,0 @@ "bin": "bin/css-b64-images", |
@@ -31,3 +31,3 @@ [![build status](https://secure.travis-ci.org/Filirom1/css-base64-images.png)](http://travis-ci.org/Filirom1/css-base64-images) | ||
* File bigger than 4Ko | ||
* File bigger than 4Ko (configurable) | ||
* external urls: url("http://my-company.ext/img/background-pattern.gif"); | ||
@@ -49,3 +49,3 @@ * not found images | ||
fromFile(cssFile, root, cb) | ||
fromFile(cssFile, root, [options,] cb) | ||
@@ -63,3 +63,3 @@ You must specify the `root` path for absolute URLs to work. | ||
fromString(css, relativePath, rootPath , cb) | ||
fromString(css, relativePath, rootPath, [options,] cb) | ||
@@ -74,5 +74,9 @@ var b64img = require('css-b64-images'); | ||
### Options | ||
* maxSize: (default 4096) bigger images are not base64 in the CSS | ||
## LICENSE | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1155515
176
79