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

Commit b626e08

Browse files
author
Mehdy Dara
committed
Merge pull request #9 from zckrs/option-deleter-after-encoding
Option delete after encoding
2 parents 6987a28 + cdfe0d6 commit b626e08

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Inspired by [grunt-image-embed](https://github.com/ehynds/grunt-image-embed) and
99
* Supports local and remote resources.
1010
* Supports buffer (and stream **WIP**).
1111
* Ability to define a relative base directory to gulpfile.js. Default is the current directory. [Details](#optionsbasedir)
12+
* Ability to remove a local resource after encoding. Default is unable. [Details](#optionsdeleteafterencoding)
1213
* Ability to specify a weight limit. Default is 32kB which is IE8's limit. [Details](#optionsmaxweightresource)
1314
* Ability to filter on file extensions. Default there is no filter. [Details](#optionsextensionsallowed)
1415
* Ignore a resource by specifying a directive comment in CSS. [Details](#ignore-a-specific-resource)
@@ -53,8 +54,14 @@ Type: `String`
5354

5455
Default value: ``
5556

56-
Notes: If you have absolute image paths in your stylesheet, the path specified in this option will be used as the base directory. By default plugin used the current directory of gulpfile.js to find local resources.
57+
Note: If you have absolute image paths in your stylesheet, the path specified in this option will be used as the base directory. By default plugin used the current directory of gulpfile.js to find local resources.
5758

59+
#### options.deleteAfterEncoding
60+
Type: `Boolean`
61+
62+
Default value: false
63+
64+
Note: Delete a local source file after encoding.
5865

5966
#### options.maxWeightResource
6067
Type: `Number`

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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.0.2",
4+
"version": "1.1.0",
55
"homepage": "http://github.com/zckrs/gulp-css-base64",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/zckrs/gulp-css-base64.git"
99
},
1010
"bugs": {
1111
"url": "http://github.com/zckrs/gulp-css-base64/issues",
12-
"email": "project@hostname.com"
12+
"email": "mdara@eleven-labs.com"
1313
},
1414
"contributors": [
1515
"Mehdy Dara <[email protected]> (http://eleven-labs.com/)"

src/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var rImages = /url(?:\(['|"]?)(.*?)(?:['|"]?\))(?!.*\/\*base64:skip\*\/)/ig;
2020
function gulpCssBase64(opts) {
2121

2222
opts = opts || {};
23+
opts.deleteAfterEncoding = opts.deleteAfterEncoding || false;
2324
opts.maxWeightResource = opts.maxWeightResource || 32768;
2425
opts.extensionsAllowed = opts.extensionsAllowed || [];
2526
opts.baseDir = opts.baseDir || '';
@@ -28,6 +29,7 @@ function gulpCssBase64(opts) {
2829
var stream = through.obj(function (file, enc, callbackStream) {
2930

3031
var currentStream = this;
32+
var cache = [];
3133

3234
if (file.isNull()) {
3335
// Do nothing if no contents
@@ -47,12 +49,19 @@ function gulpCssBase64(opts) {
4749
return result !== null;
4850
},
4951
function (callback) {
50-
encodeResource(result[1], file, opts, function (strRes) {
51-
if (undefined !== strRes) {
52-
src = src.replace(result[1], strRes);
53-
}
52+
if (cache[result[1]]) {
53+
src = src.replace(result[1], cache[result[1]]);
5454
callback();
55-
});
55+
} else {
56+
encodeResource(result[1], file, opts, function (strRes) {
57+
if (undefined !== strRes) {
58+
src = src.replace(result[1], strRes);
59+
// Store in cache
60+
cache[result[1]] = strRes;
61+
}
62+
callback();
63+
});
64+
}
5665
},
5766
function () {
5867
file.contents = new Buffer(src);
@@ -110,21 +119,26 @@ function encodeResource(img, file, opts, doneCallback) {
110119
location = img.charAt(0) === "/" ? (opts.baseDir || "") + img : path.join(path.dirname(file.path), (opts.baseDir || "") + "/" + img);
111120

112121
if (!fs.existsSync(location)) {
113-
gutil.log("gulp-css-base64 : Ressource not found " + gutil.colors.black.bgYellow(location));
122+
gutil.log("gulp-css-base64 : File not found " + gutil.colors.black.bgYellow(location));
114123
doneCallback();
115124
return;
116125
}
117126

118127
binRes = fs.readFileSync(location);
119128

120129
if (binRes.length > opts.maxWeightResource) {
121-
gutil.log("gulp-css-base64 : Resource is too big " + gutil.colors.black.bgYellow(binRes.length + " octets"));
130+
gutil.log("gulp-css-base64 : File is too big " + gutil.colors.black.bgYellow(binRes.length + " octets") + " : " + location);
122131
doneCallback();
123132
return;
124133
}
125134

126135
var strRes = "data:" + mime.lookup(location) + ";base64," + binRes.toString("base64");
127136

137+
if(opts.deleteAfterEncoding) {
138+
gutil.log("gulp-css-base64 : Resource delete " + gutil.colors.black.bgYellow(location));
139+
fs.unlinkSync(location);
140+
}
141+
128142
doneCallback(strRes);
129143
return;
130144
}

test/test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var fs = require('fs');
12
var assert = require('assert');
23
var es = require('event-stream');
34
var gutil = require('gulp-util');
@@ -294,6 +295,67 @@ describe('gulp-css-base64', function () {
294295
});
295296
});
296297

298+
it('should delete local resource source file after encoding', function (done) {
299+
fs.writeFileSync('test/fixtures/image/very-very-small_copy.png', fs.readFileSync('test/fixtures/image/very-very-small.png'));
300+
// create the fake file
301+
var fakeFile = new gutil.File({
302+
contents: new Buffer('.button_alert{background:url(test/fixtures/image/very-very-small_copy.png) no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}')
303+
});
304+
305+
// Create a css-base64 plugin stream
306+
var stream = base64({
307+
deleteAfterEncoding : true
308+
});
309+
310+
// write the fake file to it
311+
stream.write(fakeFile);
312+
313+
// wait for the file to come back out
314+
stream.once('data', function (file) {
315+
// make sure it came out the same way it went in
316+
assert(file.isBuffer());
317+
318+
// check the contents
319+
assert.equal(file.contents.toString('utf8'), '.button_alert{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANAQAAAABakNnRAAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAAAEgAAABIAEbJaz4AAAArSURBVAjXY/j/g2H/C4b5Jxj6OxgaOEBoxgmGDg8GIACyuRoYjkowfKgAACBpDLQ2kvRRAAAAAElFTkSuQmCC) no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}');
320+
321+
// check if file is removed
322+
assert(!fs.existsSync('test/fixtures/image/very-very-small_copy.png'));
323+
324+
done();
325+
});
326+
});
327+
328+
it('should use cache when css contain duplicate uri resource', function (done) {
329+
// example case : css contain two url() with same uri and deleteAfterEncoding is enabled
330+
fs.writeFileSync('test/fixtures/image/very-very-small_copy.png', fs.readFileSync('test/fixtures/image/very-very-small.png'));
331+
// create the fake file
332+
var fakeFile = new gutil.File({
333+
contents: new Buffer('.button_alert{background:url(test/fixtures/image/very-very-small_copy.png) no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline.button_alert{background:url(test/fixtures/image/very-very-small_copy.png) no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}')
334+
});
335+
336+
// Create a css-base64 plugin stream
337+
var stream = base64({
338+
deleteAfterEncoding : true
339+
});
340+
341+
// write the fake file to it
342+
stream.write(fakeFile);
343+
344+
// wait for the file to come back out
345+
stream.once('data', function (file) {
346+
// make sure it came out the same way it went in
347+
assert(file.isBuffer());
348+
349+
// check the contents
350+
assert.equal(file.contents.toString('utf8'), '.button_alert{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANAQAAAABakNnRAAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAAAEgAAABIAEbJaz4AAAArSURBVAjXY/j/g2H/C4b5Jxj6OxgaOEBoxgmGDg8GIACyuRoYjkowfKgAACBpDLQ2kvRRAAAAAElFTkSuQmCC) no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline.button_alert{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANAQAAAABakNnRAAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAAAEgAAABIAEbJaz4AAAArSURBVAjXY/j/g2H/C4b5Jxj6OxgaOEBoxgmGDg8GIACyuRoYjkowfKgAACBpDLQ2kvRRAAAAAElFTkSuQmCC) no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}');
351+
352+
// check if file is removed
353+
assert(!fs.existsSync('test/fixtures/image/very-very-small_copy.png'));
354+
355+
done();
356+
});
357+
});
358+
297359
it('should convert if remote resource (http://)', function (done) {
298360
// create the fake file
299361
var fakeFile = new gutil.File({

0 commit comments

Comments
 (0)