-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
51 lines (41 loc) · 1.14 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var crypto = require('crypto');
var defaults = require('lodash.defaults');
var through = require('through2');
var plugin = function(name, opt){
var opts = defaults(opt || {}, {
optimizeMemory: false
});
if (!plugin.caches[name]) {
plugin.caches[name] = {};
}
var stream = through.obj(function(file, enc, callback){
var contents = file.checksum;
if (!contents) {
if (file.isStream()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
contents = file.contents.toString('utf8');
// slower for each file
// but good if you need to save on memory
if (opts.optimizeMemory) {
contents = crypto.createHash('md5').update(contents).digest('hex');
}
}
}
var cacheFile = plugin.caches[name][file.path];
// hit - ignore it
if (typeof cacheFile !== 'undefined' && cacheFile === contents) {
callback();
return;
}
// miss - add it and pass it through
plugin.caches[name][file.path] = contents;
this.push(file);
callback();
});
return stream;
};
plugin.caches = {};
module.exports = plugin;