Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,3 @@ gulp.task('css', function() {
.pipe(gulp.dest('./dist'));
});
```

`imagehash` takes an optional parameter to point to the root of your web
folder.

```js
var imagehash = require('gulp-css-image-hash');
gulp.task('css', function() {
return gulp.src('./css/*.css')
.pipe(imagehash('./web'))
.pipe(gulp.dest('./dist'));
});
```
84 changes: 31 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,66 @@
'use strict';

var through = require('through2'),
async = require('async'),
fs = require('fs'),
crypto = require('crypto'),
extname = require('path').extname,
gutil = require('gulp-util');
async = require('async'),
fs = require('fs'),
crypto = require('crypto'),

var PLUGIN_NAME = 'gulp-css-image-hash';

function cssImageHash(webPath, includeFileExtensions) {
var stream = through.obj(function(file, enc, cb) {
function cssImageHash() {
var stream = through.obj(function (file, enc, cb) {
var that = this;

var regex = /url\(([^\)]+)\)/g,
matches = null;

var regex = /url\(([^\)]+)\)/g
var matches = null
var asString = '';

var filePath = path.dirname(file.path);

if (file.isBuffer()) {
asString = String(file.contents);

matches = asString.match(regex);
}

if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}

if (matches != null && matches.length) {
var pairs = [];
matches = matches.forEach(function(curValue) {
// remove url()
matches = matches.forEach(function (curValue) {
var path = curValue.slice(4).slice(0, -1);

// Remove surrounding quotes
if (path[0] == '"' || path[0] == "'") {
path = path.slice(1).slice(0, -1);
}

pairs.push([curValue, path]);
});

async.eachSeries(pairs, function(tuple, callback) {
var path = tuple[1];

if (Array.isArray(includeFileExtensions)) {
var extension = extname(tuple[1]).slice(1);

if(includeFileExtensions.indexOf(extension) < 0) {
return callback();
}
}

if (path.substr(0, 4) == 'data') {
async.eachSeries(pairs, function (tuple, callback) {

var imgPath = tuple[1];

if (imgPath.substr(0, 4) == 'data') {
return callback();
}

if (typeof(webPath) == 'function') {
path = webPath(path);
} else if (typeof(webPath) != 'undefined') {
path = webPath + path;
}

if (!path) {

if (!imgPath) {
return callback();
}

var md5 = crypto.createHash('md5'),
file = fs.ReadStream(path),
hash = '';
file.on('data', function(d) {
var md5 = crypto.createHash('md5')
var hash = '';
var file = fs.ReadStream(path.join(filePath, imgPath))

file.on('data', function (d) {
md5.update(d);
});
file.on('end', function() {

file.on('end', function () {
hash = md5.digest('hex');
asString = asString.replace(tuple[0], 'url(' + tuple[1] + '?h=' + hash + ')');
callback();
});

file.on('error', function(error) {
gutil.log('gulp-css-image-hash: Skipping ' + path);

file.on('error', function (error) {
callback();
});
}, function (err) {
Expand All @@ -93,10 +70,11 @@ function cssImageHash(webPath, includeFileExtensions) {
});
} else {
this.push(file);
cb();
cb();
}
});
})

return stream;
};
}

module.exports = cssImageHash;