css-b64-images
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -8,34 +8,41 @@ var fs = require('fs'), | ||
module.exports = optimizeFile; | ||
module.exports = { | ||
fromFile: fromFile, | ||
fromString: fromString | ||
}; | ||
function optimizeFile(cssFile, root, cb) { | ||
fs.readFile(cssFile, function(err, css){ | ||
function fromString(css, relativePath, rootPath , cb) { | ||
if(!css.replace && css.toString) css = css.toString(); | ||
var urls = [], match; | ||
while (match = imgRegex.exec(css)) { | ||
urls.push(match[1]); | ||
} | ||
forEachSeries(urls, base64img, function(err){ | ||
if(err) return cb(err, css); | ||
css = css.toString(); | ||
var urls = [], match; | ||
while (match = imgRegex.exec(css)) { | ||
urls.push(match[1]); | ||
cb(null, css); | ||
}); | ||
function base64img(imageUrl, cb){ | ||
if(externalUrlRegex.test(imageUrl)) { | ||
return cb(new Error('Skip ' + imageUrl + ' External file.'), css); | ||
} | ||
forEachSeries(urls, base64img, function(err){ | ||
var imagePath; | ||
if(absoluteUrlRegex.test(imageUrl)) { | ||
imagePath = Path.join(root, imageUrl.substr(1)); | ||
}else{ | ||
imagePath = Path.join(relativePath, imageUrl); | ||
} | ||
replaceUrlByB64(imageUrl, imagePath, css, function (err, newCss){ | ||
if(err) return cb(err, css); | ||
cb(null, css); | ||
css = newCss; | ||
cb(); | ||
}); | ||
} | ||
} | ||
function base64img(imageUrl, cb){ | ||
if(externalUrlRegex.test(imageUrl)) { | ||
return cb(new Error('Skip ' + imageUrl + ' External file.'), css); | ||
} | ||
var imagePath; | ||
if(absoluteUrlRegex.test(imageUrl)) { | ||
imagePath = Path.join(root, imageUrl.substr(1)); | ||
}else{ | ||
imagePath = Path.join(Path.dirname(cssFile), imageUrl); | ||
} | ||
replaceUrlByB64(imageUrl, imagePath, css, function (err, newCss){ | ||
if(err) return cb(err, css); | ||
css = newCss; | ||
cb(); | ||
}); | ||
} | ||
function fromFile(cssFile, root, cb) { | ||
fs.readFile(cssFile, function(err, css){ | ||
if(err) return cb(err, css); | ||
fromString(css.toString(), Path.dirname(cssFile), root, cb); | ||
}); | ||
@@ -42,0 +49,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Base64 images in your css", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "lib/css-b64-images.js", | ||
@@ -23,2 +23,6 @@ "bin": "bin/css-b64-images", | ||
}, | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/MIT" | ||
}], | ||
"optionalDependencies": {}, | ||
@@ -25,0 +29,0 @@ "engines": { |
@@ -6,2 +6,4 @@ css-base64-images | ||
![Base64](https://github.com/Filirom1/css-base64-images/raw/master/draft.png) | ||
Transform this: | ||
@@ -44,4 +46,6 @@ | ||
optimizeFile(cssFile, root, cb) | ||
### From File | ||
fromFile(cssFile, root, cb) | ||
You must specify the `root` path for absolute URLs to work. | ||
@@ -51,3 +55,3 @@ | ||
b64img('/your/www/root/dir/css/your-stylesheet.css', '/your/www/root/dir/', function(err, css){ | ||
b64img.fromFile('/your/www/root/dir/css/your-stylesheet.css', '/your/www/root/dir/', function(err, css){ | ||
if(err) console.error('Error:', err); | ||
@@ -57,1 +61,17 @@ console.log(css); | ||
### From String | ||
fromString(css, relativePath, rootPath , cb) | ||
var b64img = require('css-b64-images'); | ||
var css = fs.readFileSync('/your/www/root/dir/css/your-stylesheet.css'); | ||
b64img.fromString(css, '/your/www/root/dir/css/', '/your/www/root/dir/', function(err, css){ | ||
if(err) console.error('Error:', err); | ||
console.log(css); | ||
}); | ||
## LICENSE | ||
MIT |
var Path = require('path'), | ||
fs = require('fs'), | ||
b64 = require('..'); | ||
@@ -6,17 +7,31 @@ | ||
describe('A tricky CSS', function(){ | ||
describe('A complex CSS', function(){ | ||
var cssFile = Path.join(__dirname, 'fixture', 'css', 'style.css'), | ||
relative = Path.join(__dirname, 'fixture', 'css'); | ||
root = Path.join(__dirname, 'fixture'); | ||
it('should be optimized with base64', function(done){ | ||
b64(cssFile, root, function(err, css){ | ||
css.should.include(".single-quote {\n background: url('data:image/gif;base64,"); | ||
css.should.include(".double-quote {\n background: url(\"data:image/gif;base64,"); | ||
css.should.include(".absolute {\n background: url('data:image/gif;base64,"); | ||
css.should.include(".external {\n background: url('http"); | ||
css.should.include(".tooBig {\n background: url('../img"); | ||
css.should.include(".not-found {\n background: url('../img"); | ||
it('a file should be optimized with base64', function(done){ | ||
b64.fromFile(cssFile, root, function(err, css){ | ||
cssShouldBeCorrect(css); | ||
done(); | ||
}); | ||
}); | ||
it('a string should be optimized with base64', function(done){ | ||
var css = fs.readFileSync(cssFile); | ||
b64.fromString(css, relative, root, function(err, css){ | ||
cssShouldBeCorrect(css); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
function cssShouldBeCorrect(css){ | ||
css.should.include(".single-quote {\n background: url('data:image/gif;base64,"); | ||
css.should.include(".double-quote {\n background: url(\"data:image/gif;base64,"); | ||
css.should.include(".absolute {\n background: url('data:image/gif;base64,"); | ||
css.should.include(".external {\n background: url('http"); | ||
css.should.include(".tooBig {\n background: url('../img"); | ||
css.should.include(".not-found {\n background: url('../img"); | ||
} |
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
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
1154063
19
0
151
74
2