Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Use ESLint with eslint-config-xo-space
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdy Dara committed Dec 10, 2015
1 parent d5844fe commit e78728a
Show file tree
Hide file tree
Showing 4 changed files with 420 additions and 428 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -3,7 +3,7 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
19 changes: 12 additions & 7 deletions package.json
Expand Up @@ -3,10 +3,7 @@
"description": "Gulp's task for transform all resources found in a CSS into base64-encoded data URI strings",
"version": "1.3.2",
"homepage": "http://github.com/zckrs/gulp-css-base64",
"repository": {
"type": "git",
"url": "http://github.com/zckrs/gulp-css-base64.git"
},
"repository": "zckrs/gulp-css-base64.git",
"bugs": {
"url": "http://github.com/zckrs/gulp-css-base64/issues",
"email": "mdara@eleven-labs.com"
Expand All @@ -16,6 +13,9 @@
"Mehdy Dara <mdara@eleven-labs.com> (http://eleven-labs.com/)",
"Callum Jefferies <callum.jefferies@gmail.com> (http://callumj.uk/)"
],
"files": [
"src"
],
"main": "./src/index.js",
"keywords": [
"gulpplugin",
Expand All @@ -33,18 +33,23 @@
},
"devDependencies": {
"coveralls": "^2.11.2",
"eslint": "^1.10.3",
"eslint-config-xo-space": "^0.7.0",
"event-stream": "^3.3.2",
"istanbul": "^0.4.1",
"mocha": "^2.3.4",
"mocha-lcov-reporter": "^1.0.0"
},
"scripts": {
"test": "mocha --reporter spec",
"test": "eslint . && mocha --reporter spec",
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha --report html -- test/*.js -R spec -t 5000"
},
"license": "MIT",
"directories": {
"test": "test"
"eslintConfig": {
"extends": "xo-space",
"env": {
"mocha": true
}
}
}
274 changes: 135 additions & 139 deletions src/index.js
@@ -1,197 +1,193 @@
'use strict';

// NodeJS library
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var util = require('util');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var util = require('util');
var Stream = require('stream').Stream;

// NPM library
var gutil = require('gulp-util');
var gutil = require('gulp-util');
var through = require('through2');
var request = require('request');
var buffers = require('buffers');
var async = require('async');
var chalk = require('chalk');
var async = require('async');
var chalk = require('chalk');

var rImages = /url(?:\(['|"]?)(.*?)(?:['|"]?\))(?!.*\/\*base64:skip\*\/)/ig;

function gulpCssBase64(opts) {

opts = JSON.parse(JSON.stringify(opts || {}));
opts.maxWeightResource = opts.maxWeightResource || 32768;
if (util.isArray(opts.extensionsAllowed)) {
opts.extensionsAllowed = opts.extensionsAllowed;
} else {
opts.extensionsAllowed = [];
}
opts.extensionsAllowed = opts.extensionsAllowed || [];
opts.baseDir = opts.baseDir || '';
opts.verbose = process.argv.indexOf('--verbose') !== -1;
opts = JSON.parse(JSON.stringify(opts || {}));
opts.maxWeightResource = opts.maxWeightResource || 32768;
if (util.isArray(opts.extensionsAllowed)) {
opts.extensionsAllowed = opts.extensionsAllowed;
} else {
opts.extensionsAllowed = [];
}
opts.extensionsAllowed = opts.extensionsAllowed || [];
opts.baseDir = opts.baseDir || '';
opts.verbose = process.argv.indexOf('--verbose') !== -1;

// Creating a stream through which each file will pass
var stream = through.obj(function (file, enc, callbackStream) {
var stream = through.obj(function (file, enc, callbackStream) {
var currentStream = this;
var cache = [];

var currentStream = this;
var cache = [];

if (file.isNull()) {
if (file.isNull()) {
// Do nothing if no contents
currentStream.push(file);
currentStream.push(file);

return callbackStream();
}
return callbackStream();
}

if (file.isBuffer()) {
var src = file.contents.toString();
var result = [];
if (file.isBuffer()) {
var src = file.contents.toString();
var result = [];

async.whilst(
async.whilst(
function () {
result = rImages.exec(src);
result = rImages.exec(src);

return result !== null;
return result !== null;
},
function (callback) {
if (cache[result[1]]) {
src = src.replace(result[1], cache[result[1]]);
callback();
return;
}

if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(result[1])) == -1) {
log('Ignores ' + chalk.yellow(result[1]) + ', extension not allowed ' + chalk.yellow(path.extname(result[1])), opts.verbose);
if (cache[result[1]]) {
src = src.replace(result[1], cache[result[1]]);
callback();
return;
}

if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(result[1])) === -1) {
log('Ignores ' + chalk.yellow(result[1]) + ', extension not allowed ' + chalk.yellow(path.extname(result[1])), opts.verbose);
callback();
return;
}

encodeResource(result[1], file, opts, function (fileRes) {
if (undefined !== fileRes) {
if (fileRes.contents.length > opts.maxWeightResource) {
log('Ignores ' + chalk.yellow(result[1]) + ', file is too big ' + chalk.yellow(fileRes.contents.length + ' bytes'), opts.verbose);
callback();
return;
}

encodeResource(result[1], file, opts, function (fileRes) {
if (undefined !== fileRes) {
}

if (fileRes.contents.length > opts.maxWeightResource) {
log('Ignores ' + chalk.yellow(result[1]) + ', file is too big ' + chalk.yellow(fileRes.contents.length + ' bytes'), opts.verbose);
callback();
return;
}

var strRes = 'data:' + mime.lookup(fileRes.path) + ';base64,' + fileRes.contents.toString('base64');
src = src.replace(result[1], strRes);
var strRes = 'data:' + mime.lookup(fileRes.path) + ';base64,' + fileRes.contents.toString('base64');
src = src.replace(result[1], strRes);

// Store in cache
cache[result[1]] = strRes;
}
callback();
});
cache[result[1]] = strRes;
}
callback();
});
},
function () {
file.contents = new Buffer(src);
currentStream.push(file);
file.contents = new Buffer(src);
currentStream.push(file);

return callbackStream();
return callbackStream();
}
);
}
}

if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-css-base64', 'Stream not supported!'));
}
});
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-css-base64', 'Stream not supported!'));
}
});

// returning the file stream
return stream;
return stream;
}

function encodeResource(img, file, opts, doneCallback) {
var fileRes = new gutil.File();

if (/^data:/.test(img)) {
log('Ignores ' + chalk.yellow(img.substring(0, 30) + '...') + ', already encoded', opts.verbose);
doneCallback();
return;
var fileRes = new gutil.File();

if (/^data:/.test(img)) {
log('Ignores ' + chalk.yellow(img.substring(0, 30) + '...') + ', already encoded', opts.verbose);
doneCallback();
return;
}

if (img[0] === '#') {
log('Ignores ' + chalk.yellow(img.substring(0, 30) + '...') + ', SVG mask', opts.verbose);
doneCallback();
return;
}

if (/^(http|https|\/\/)/.test(img)) {
log('Fetch ' + chalk.yellow(img), opts.verbose);
// different case for uri start '//'
if (img[0] + img[1] === '//') {
img = 'http:' + img;
}

if (img[0] === '#') {
log('Ignores ' + chalk.yellow(img.substring(0, 30) + '...') + ', SVG mask', opts.verbose);
fetchRemoteRessource(img, function (resultBuffer) {
if (resultBuffer === null) {
log('Error: ' + chalk.red(img) + ', unable to fetch', opts.verbose);
doneCallback();
return;
}
}
fileRes.path = img;
fileRes.contents = resultBuffer;
doneCallback(fileRes);
return;
});
} else {
var location = '';
var binRes = '';

if (/^(http|https|\/\/)/.test(img)) {
log('Fetch ' + chalk.yellow(img), opts.verbose);
// different case for uri start '//'
if (img[0] + img[1] === '//') {
img = 'http:' + img;
}

fetchRemoteRessource(img, function (resultBuffer) {
if (null === resultBuffer) {
log('Error: ' + chalk.red(img) + ', unable to fetch', opts.verbose);
doneCallback();
return;
} else {
fileRes.path = img;
fileRes.contents = resultBuffer;
doneCallback(fileRes);
return;
}
});
} else {
var location = '';
var binRes = '';

location = img.charAt(0) === '/' ? (opts.baseDir || '') + img : path.join(path.dirname(file.path), (opts.baseDir || '') + '/' + img);
location = location.replace(/([?#].*)$/, "");

if (!fs.existsSync(location)) {
log('Error: ' + chalk.red(location) + ', file not found', opts.verbose);
doneCallback();
return;
}

binRes = fs.readFileSync(location);

fileRes.path = location;
fileRes.contents = binRes;

doneCallback(fileRes);
return;
location = img.charAt(0) === '/' ? (opts.baseDir || '') + img : path.join(path.dirname(file.path), (opts.baseDir || '') + '/' + img);
location = location.replace(/([?#].*)$/, '');

if (!fs.existsSync(location)) {
log('Error: ' + chalk.red(location) + ', file not found', opts.verbose);
doneCallback();
return;
}

binRes = fs.readFileSync(location);

fileRes.path = location;
fileRes.contents = binRes;

doneCallback(fileRes);
return;
}
}

function fetchRemoteRessource(url, callback) {
var resultBuffer;
var buffList = buffers();
var imageStream = new Stream();

imageStream.writable = true;
imageStream.write = function (data) {
buffList.push(new Buffer(data));
};
imageStream.end = function () {
resultBuffer = buffList.toBuffer();
};

request(url, function (error, response, body) {
if (error) {
callback(null);
return;
}
var resultBuffer;
var buffList = buffers();
var imageStream = new Stream();

imageStream.writable = true;
imageStream.write = function (data) {
buffList.push(new Buffer(data));
};
imageStream.end = function () {
resultBuffer = buffList.toBuffer();
};

request(url, function (error, response) {
if (error) {
callback(null);
return;
}

// Bail if we get anything other than 200
if (response.statusCode !== 200) {
callback(null);
return;
}
if (response.statusCode !== 200) {
callback(null);
return;
}

callback(resultBuffer);
}).pipe(imageStream);
callback(resultBuffer);
}).pipe(imageStream);
}

function log(message, isVerbose) {
if (true === isVerbose) {
gutil.log(message);
}
if (isVerbose === true) {
gutil.log(message);
}
}

// Exporting the plugin main function
Expand Down

0 comments on commit e78728a

Please sign in to comment.