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

Commit 3fee3fc

Browse files
author
Mehdy Dara
committed
Merge pull request #17 from zckrs/option-verbose
Add verbose option
2 parents ddfffa5 + ea08363 commit 3fee3fc

File tree

5 files changed

+182
-70
lines changed

5 files changed

+182
-70
lines changed

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ Inspired by [grunt-image-embed](https://github.com/ehynds/grunt-image-embed) and
88

99
* Supports local and remote resources.
1010
* Supports buffer (and stream **WIP**).
11-
* Ability to use a pre-process function for each resource find in CSS. Default there is no function. [Details](#optionspreprocess)
12-
* Ability to define a relative base directory to gulpfile.js. Default is the current directory. [Details](#optionsbasedir)
13-
* Ability to remove a local resource after encoding. Default is unable. [Details](#optionsdeleteafterencoding)
14-
* Ability to specify a weight limit. Default is 32kB which is IE8's limit. [Details](#optionsmaxweightresource)
15-
* Ability to filter on file extensions. Default there is no filter. [Details](#optionsextensionsallowed)
16-
* Ignore a resource by specifying a directive comment in CSS. [Details](#ignore-a-specific-resource)
11+
* [[>]](#optionsverbose) Ability to enable verbose mode with debugs messages. Default is unable.
12+
* [[>]](#optionspreprocess) Ability to use a pre-process function for each resource find in CSS. Default there is no function.
13+
* [[>]](#optionsbasedir) Ability to define a relative base directory to gulpfile.js. Default is the current directory.
14+
* [[>]](#optionsdeleteafterencoding) Ability to remove a local resource after encoding. Default is unable.
15+
* [[>]](#optionsmaxweightresource) Ability to specify a weight limit. Default is 32kB which is IE8's limit.
16+
* [[>]](#optionsextensionsallowed) Ability to filter on file extensions. Default there is no filter.
17+
* [[>]](#ignore-a-specific-resource) Ignore a resource by specifying a directive comment in CSS.
1718
* Existing data URIs will be ignored.
1819
* Existing SVG masks will be ignored.
1920

@@ -41,6 +42,7 @@ gulp.task('default', function () {
4142
gulp.task('default', function () {
4243
return gulp.src('src/css/input.css')
4344
.pipe(cssBase64({
45+
verbose: true
4446
baseDir: "../../images",
4547
maxWeightResource: 100,
4648
extensionsAllowed: ['.gif', '.jpg']
@@ -51,11 +53,41 @@ gulp.task('default', function () {
5153

5254
## Options
5355

56+
#### options.verbose
57+
Type: `Boolean`
58+
59+
Default value: `false`
60+
61+
Note: Writes in stdout for more infos on process. (Resource ignored, fetch remote resource, etc)
62+
5463
#### options.preProcess
55-
Type: `Function(buffer, mimeType, callback)`
64+
Type: `Function(vinylFile, callback)`
5665

5766
Default value: ``
5867

68+
Note: Vinyl is a virtual file format. See [description](https://github.com/wearefractal/vinyl#file)
69+
70+
Example:
71+
```js
72+
// Apply a rotation on each PNG image with NPM library http://aheckmann.github.io/gm/
73+
var gm = require('gm').subClass({
74+
imageMagick: true
75+
});
76+
[...]
77+
.pipe(cssBase64({
78+
preProcess : function(vinylFile, callback) {
79+
if ('image/png' === mime.lookup(vinylFile.path)) {
80+
gm(vinylFile.path).rotate('green', 180).toBuffer(function (err, buffer) {
81+
vinylFile.contents = buffer;
82+
return callback(vinylFile);
83+
})
84+
}
85+
}
86+
}))
87+
[...]
88+
89+
```
90+
5991
#### options.baseDir
6092
Type: `String`
6193

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gulp-css-base64",
33
"description": "Gulp's task for transform all resources found in a CSS into base64-encoded data URI strings",
4-
"version": "1.2.2",
4+
"version": "1.2.3",
55
"homepage": "http://github.com/zckrs/gulp-css-base64",
66
"repository": {
77
"type": "git",
@@ -28,7 +28,9 @@
2828
"through2": "~0.4.1",
2929
"async": "~0.8.0",
3030
"request": "~2.34.0",
31-
"buffers": "~0.1.1"
31+
"buffers": "~0.1.1",
32+
"chalk": "~0.4.0",
33+
"dateformat": "~1.0.8-1.2.3"
3234
},
3335
"devDependencies": {
3436
"mocha": "~1.18.2",
@@ -40,7 +42,7 @@
4042
"scripts": {
4143
"test": "mocha --reporter spec",
4244
"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",
43-
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha --report html -- tests/*.mocha.js -R spec -t 5000"
45+
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha --report html -- test/*.js -R spec -t 5000"
4446
},
4547
"license": "MIT",
4648
"directories": {

src/index.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
'use strict';
22

3+
// NodeJS library
34
var fs = require('fs');
45
var path = require("path");
56
var mime = require("mime");
67
var Stream = require('stream').Stream;
78

9+
// NPM library
810
var async = require("async");
911
var through = require('through2');
1012
var request = require('request');
1113
var buffers = require('buffers');
14+
var chalk = require('chalk');
1215
var gutil = require('gulp-util');
1316
var PluginError = gutil.PluginError;
1417

18+
// Local library
19+
var customLog = require('./lib/log');
20+
1521
// Consts
1622
var PLUGIN_NAME = 'gulp-css-base64';
1723
var rImages = /url(?:\(['|"]?)(.*?)(?:['|"]?\))(?!.*\/\*base64:skip\*\/)/ig;
@@ -24,6 +30,7 @@ function gulpCssBase64(opts) {
2430
opts.extensionsAllowed = opts.extensionsAllowed || [];
2531
opts.baseDir = opts.baseDir || '';
2632
opts.preProcess = opts.preProcess || '';
33+
opts.verbose = opts.verbose || false;
2734

2835
// Creating a stream through which each file will pass
2936
var stream = through.obj(function (file, enc, callbackStream) {
@@ -56,7 +63,7 @@ function gulpCssBase64(opts) {
5663
}
5764

5865
if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(result[1])) == -1) {
59-
gutil.log("gulp-css-base64 : Resource dont have allowed extension " + gutil.colors.black.bgYellow(path.extname(result[1])));
66+
log("Ignores " + chalk.yellow(result[1]) + ", extension not allowed " + chalk.yellow(path.extname(result[1])), opts.verbose);
6067
callback();
6168
return;
6269
}
@@ -65,13 +72,13 @@ function gulpCssBase64(opts) {
6572
if (undefined !== fileRes) {
6673

6774
if (fileRes.contents.length > opts.maxWeightResource) {
68-
gutil.log("gulp-css-base64 : File is too big " + gutil.colors.black.bgYellow(fileRes.contents.length + " octets") + " : " + result[1]);
75+
log("Ignores " + chalk.yellow(result[1]) + ", file is too big " + chalk.yellow(fileRes.contents.length + " bytes"), opts.verbose);
6976
callback();
7077
return;
7178
}
7279

73-
if(opts.deleteAfterEncoding && fileRes.path) {
74-
gutil.log("gulp-css-base64 : Resource delete " + gutil.colors.black.bgYellow(fileRes.path));
80+
if (opts.deleteAfterEncoding && fileRes.path) {
81+
log("Delete source file " + chalk.yellow(fileRes.path), opts.verbose);
7582
fs.unlinkSync(fileRes.path);
7683
}
7784

@@ -106,34 +113,34 @@ function encodeResource(img, file, opts, doneCallback) {
106113
var fileRes = new gutil.File();
107114

108115
if (/^data:/.test(img)) {
109-
gutil.log("gulp-css-base64 : Resource is already base64 " + gutil.colors.black.bgYellow(img.substring(0, 30) + '...'));
116+
log("Ignores " + chalk.yellow(img.substring(0, 30) + '...') + ", already encoded", opts.verbose);
110117
doneCallback();
111118
return;
112119
}
113120

114121
if (img[0] === '#') {
115-
gutil.log("gulp-css-base64 : SVG mask ignored " + gutil.colors.black.bgYellow(img));
122+
log("Ignores " + chalk.yellow(img.substring(0, 30) + '...') + ", SVG mask", opts.verbose);
116123
doneCallback();
117124
return;
118125
}
119126

120127
if (/^(http|https|\/\/)/.test(img)) {
121-
122-
gutil.log("gulp-css-base64 : Remote resource " + gutil.colors.black.bgYellow(img));
128+
log("Fetch " + chalk.yellow(img), opts.verbose);
123129
// different case for uri start '//'
124130
if (img[0] + img[1] === '//') {
125131
img = 'http:' + img;
126132
}
127133

128134
fetchRemoteRessource(img, function (resultBuffer) {
129-
if (null !== resultBuffer) {
135+
if (null === resultBuffer) {
136+
log("Error: " + chalk.red(img) + ", unable to fetch", opts.verbose);
137+
doneCallback();
138+
return;
139+
} else {
130140
fileRes.path = img;
131141
fileRes.contents = resultBuffer;
132142
doneCallback(fileRes);
133143
return;
134-
} else {
135-
doneCallback();
136-
return;
137144
}
138145
});
139146
} else {
@@ -143,7 +150,7 @@ function encodeResource(img, file, opts, doneCallback) {
143150
location = img.charAt(0) === "/" ? (opts.baseDir || "") + img : path.join(path.dirname(file.path), (opts.baseDir || "") + "/" + img);
144151

145152
if (!fs.existsSync(location)) {
146-
gutil.log("gulp-css-base64 : File not found " + gutil.colors.black.bgYellow(location));
153+
log("Error: " + chalk.red(location) + ", file not found", opts.verbose);
147154
doneCallback();
148155
return;
149156
}
@@ -163,7 +170,6 @@ function encodeResource(img, file, opts, doneCallback) {
163170
return;
164171
}
165172
}
166-
167173
}
168174

169175
function fetchRemoteRessource(url, callback) {
@@ -181,14 +187,12 @@ function fetchRemoteRessource(url, callback) {
181187

182188
request(url, function (error, response, body) {
183189
if (error) {
184-
gutil.log("gulp-css-base64 : Unable to get resource " + gutil.colors.black.bgYellow(url) + ". Error " + gutil.colors.black.bgYellow(error.message));
185190
callback(null);
186191
return;
187192
}
188193

189194
// Bail if we get anything other than 200
190195
if (response.statusCode !== 200) {
191-
gutil.log("gulp-css-base64 : Unable to get resource " + gutil.colors.black.bgYellow(url) + ". Status code " + gutil.colors.black.bgYellow(response.statusCode));
192196
callback(null);
193197
return;
194198
}
@@ -197,5 +201,11 @@ function fetchRemoteRessource(url, callback) {
197201
}).pipe(imageStream);
198202
}
199203

204+
function log(message, isVerbose) {
205+
if (true === isVerbose) {
206+
customLog(message);
207+
}
208+
}
209+
200210
// Exporting the plugin main function
201211
module.exports = gulpCssBase64;

src/lib/log.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var util = require('util');
2+
var chalk = require('chalk');
3+
var date = require('dateformat');
4+
5+
module.exports = function () {
6+
var time = '[' + chalk.grey(date(new Date(), 'HH:MM:ss')) + ']';
7+
var sig = '[' + chalk.green('gulp-css-base64') + ']';
8+
var args = Array.prototype.slice.call(arguments);
9+
args.unshift(sig);
10+
args.unshift(time);
11+
console.log.apply(console, args);
12+
13+
return this;
14+
};

0 commit comments

Comments
 (0)