-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (76 loc) · 2.17 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const through = require('through2');
const caches = {};
const cache = function(name) {
if (!caches[name]) {
caches[name] = {};
}
caches[name].prev = caches[name].next;
caches[name].next = new Map();
return through.obj(function(file, _enc, callback){
if (!file) return callback();
// saves file to next
caches[name].next.set(file.path, {
original: file.contents,
processed: caches[name].prev
? caches[name].prev.get(file.path)
? caches[name].prev.get(file.path).processed
: undefined
: undefined
});
this.push(file);
return callback();
});
};
const compare = function(name) {
return through.obj(function(file, _enc, callback){
let contents = file.checksum;
if (!contents) {
if (file.isStream()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
contents = file.contents.toString('utf8');
}
}
let cacheFile = undefined;
if (caches[name].prev) {
cacheFile = caches[name].prev.get(file.path);
if (cacheFile && cacheFile.original) {
cacheFile = cacheFile.original.toString('utf8');
}
}
// hit - ignore it
if (typeof cacheFile !== 'undefined' && cacheFile === contents) return callback();
// miss - add it and pass it through
this.push(file);
return callback();
});
};
const uncache = function(name) {
return through.obj(function(file, _enc, callback) {
caches[name].next.set(file.path, {
original: caches[name].next.get(file.path).original,
processed: file
});
//if building for the first time, pass everything through
if (!caches[name].prev) {
this.push(file);
return callback();
}
//bundled once already, loop through all cached file buffers and pass through
for (let i of caches[name].next) {
this.push(i[1].processed);
}
return callback();
});
};
const remove = function(name, filePath) {
if (caches[name]) {
if (caches[name].next)
caches[name].next.delete(filePath);
if (caches[name].prev)
caches[name].prev.delete(filePath);
}
};
module.exports = { cache, compare, uncache, remove, caches };