-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (37 loc) · 1.09 KB
/
index.js
File metadata and controls
46 lines (37 loc) · 1.09 KB
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
var cs = require('coffee-script');
var calculate = require('sse4_crc32').calculate;
var cache = Object.create(null);
exports = module.exports = function (options) {
options = options || {};
options.bare = true;
options.sourceMap = true;
return function coffeescript(file, done) {
if (!~exports.extensions.indexOf(file.extension)) return done();
file.read(function (err, string) {
if (err) return done(err);
var hash = file.filename + '#' + calculate(string);
var res;
try {
res = cache[hash] = cache[hash] || cs.compile(string, options);
} catch (err) {
done(err);
return;
}
// have any future plugin treat this string as JS
file.extension = 'js';
// rewrite source map
var map = JSON.parse(res.v3SourceMap);
map.sources[0] = file.filename;
map.sourcesContent = [string];
file.string = res.js;
file.sourceMap = JSON.stringify(map);
file.originalString = string; // why not
done();
})
}
}
// extensions to support
exports.extensions = [
'coffee',
'litcoffee'
];