Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

css-b64-images

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

css-b64-images - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

draft.png

59

lib/css-b64-images.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc