UNPKG

976 BJavaScriptView Raw
1'use strict';
2
3var coffeeify = require('coffeeify')
4 , crypto = require('crypto')
5 , through = require('through')
6 , cache = {};
7
8function getHash(data) {
9 return crypto
10 .createHash('md5')
11 .update(data)
12 .digest('hex');
13}
14
15function cachingCoffeify(file) {
16 if (!coffeeify.isCoffee(file)) return through();
17
18 var data = ''
19 , stream = through(write, end);
20
21 function write (buf) { data += buf; }
22 function end() {
23 var hash = getHash(data)
24 , cached = cache[file];
25
26 if (!cached || cached.hash !== hash) {
27 coffeeify.compile(file, data, function(error, result) {
28 if (error) return stream.emit('error', error);
29 cache[file] = { compiled: result, hash: hash };
30 stream.queue(result);
31 stream.queue(null);
32 });
33 } else {
34 stream.queue(cache[file].compiled);
35 stream.queue(null);
36 }
37 }
38
39 return stream;
40};
41
42cachingCoffeify.cache = cache;
43
44
45module.exports = cachingCoffeify;