Skip to content

Commit

Permalink
Sync with offline development
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent Andersen committed Jul 7, 2013
1 parent 1139028 commit 9184b80
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions bin/imageinliner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var src = path.join(path.dirname(fs.realpathSync(__filename)), '../src');
require(src + '/main.js');
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "image-inliner",
"version": "0.0.1",
"readme": "empty",
"main": "./src/main",
"bin": {
"imageinliner": "./bin/imageinliner"
},
"dependencies": {
"mime": "~1.2.9"
}
}
45 changes: 45 additions & 0 deletions src/inline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var fs = require("fs");
var mime = require("mime");
var path = require("path");

function cleanFileData(fileData) {
// remove linebreaks
fileData = fileData.replace(/(\r\n|\n|\r)/gm,'');
// removes xml comments
fileData = fileData.replace(/<!--[^>]*-->/g, '');
// remove dubble spaces
fileData = fileData.replace(/ +(?= )/g,'');
// fileData = fileData.replace(/\'/gm,"\'");

return fileData;
}

function createCSSBackgroundImage(mimeType, encoding, fileData) {
var css = ' background-image:';
css += "url('data:" + mimeType + ";"+ encoding+"," + fileData + "');";
return css;
}

function inlineImage(imagePath, cssFileBasePath) {

imagePath = imagePath.replace(/\"|\'/g, "");
imagePath = path.join(basePath, cssFileBasePath, imagePath);
var mimeType = mime.lookup(imagePath);

if(fs.statSync(imagePath).size > fileSizeLimit) {
return;
}

var cssImage;
if(mimeType === "image/svg+xml") {
var fileData = fs.readFileSync(imagePath, "utf-8");
cssImage = createCSSBackgroundImage(mimeType, "utf-8", cleanFileData(fileData));
} else {
var fileData = fs.readFileSync(imagePath).toString('base64');
cssImage = createCSSBackgroundImage(mimeType, "base64", fileData);
}

return cssImage;
}

exports.inline = inlineImage;
48 changes: 48 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var fs = require("fs");
var mime = require("mime");
var path = require("path");
var inline = require("./inline");


var fileSizeLimit = 61440;
var basePath = process.env.PWD;


var listOfFiles = process.argv.splice(2);
if(!listOfFiles) {
return;
}

listOfFiles.forEach(function (filePath) {
var fileBasePath = path.dirname(filePath);
var reg = /background.*:.*url\((.*)\)/;

fs.readFile(filePath, "utf-8", function (err, fileData) {
if (err) throw err;

var fileMimeType = mime.lookup(filePath);
if(fileMimeType !== "text/css") {
throw "Filetype has to be css";
}

var outputFilePath = filePath + ".output.css";
var strArray = fileData.split("\n");
var modifiedStrArray = strArray.slice(0);
var indexOffset = 0;

strArray.forEach(function(line, index){
var extracted = reg.exec(line);
if(extracted && extracted[1]) {
if(cssImage = inline.inlineImage(extracted[1], fileBasePath)) {
modifiedStrArray.splice(++indexOffset + index, 0, cssImage);
}
}
});

fs.writeFile(outputFilePath, modifiedStrArray.join("\n"), function (error) {
if (error) throw error;
console.log('File saved: ' + outputFilePath);
});
});
});

0 comments on commit 9184b80

Please sign in to comment.