-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (42 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
52
53
var compile = require('./compile');
var through = require('through');
var crypto = require('crypto');
var merge = require('merge');
var _cache = {};
function getHash(data) {
return crypto
.createHash('md5')
.update(data)
.digest('hex');
}
function polyify(opts) {
var opts = opts || {};
var extensions = opts.extensions || /\.js$/i;
return function (file, options) {
if (!extensions.test(file)) {
return through();
}
var data = '';
options = merge(opts, options);
return through(write, end);
function write(buf) { data += buf };
function end() {
var hash = getHash(data);
var cached = _cache[file];
if (!cached || cached.hash !== hash) {
try {
_cache[file] = { compiled: compile(data, options), hash: hash }
this.queue(_cache[file].compiled);
} catch (e) {
e.name = 'Polyify Error';
e.message = file + ': ' + e.message;
e.fileName = file;
this.emit('error', e);
}
}
return this.queue(null);
}
}
};
exports = module.exports = polyify();
exports.configure = polyify;