forked from sindresorhus/gulp-rev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (26 loc) · 763 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict';
var crypto = require('crypto');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
function md5(str) {
return crypto.createHash('md5').update(str, 'utf8').digest('hex');
}
module.exports = function () {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-rev', 'Streaming not supported'));
return cb();
}
var hash = md5(file.contents.toString()).slice(0, 8);
var ext = path.extname(file.path);
var filename = path.basename(file.path, ext) + '-' + hash + ext;
file.path = path.join(path.dirname(file.path), filename);
this.push(file);
cb();
});
};