This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
/
index.js
64 lines (58 loc) · 1.73 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
var coffeescript = require('coffee-script');
var isLiterate = function(path) {
return /\.(litcoffee|coffee\.md)$/.test(path);
};
var normalizeChecker = function(item) {
switch (toString.call(item)) {
case '[object RegExp]':
return function(string) {
return item.test(string);
};
case '[object Function]':
return item;
default:
return function() {
return false;
};
}
};
function CoffeeScriptCompiler(config) {
if (config == null) config = {};
var plugin = config.plugins && config.plugins.coffeescript;
var conv = config.conventions && config.conventions.vendor;
this.bare = plugin && plugin.bare;
this.sourceMaps = !!config.sourceMaps;
this.isVendor = normalizeChecker(conv);
}
CoffeeScriptCompiler.prototype.brunchPlugin = true;
CoffeeScriptCompiler.prototype.type = 'javascript';
CoffeeScriptCompiler.prototype.extension = 'coffee';
CoffeeScriptCompiler.prototype.pattern = /\.(coffee(\.md)?|litcoffee)$/;
CoffeeScriptCompiler.prototype.compile = function(data, path, callback) {
var options = {
bare: this.bare == null ? !this.isVendor(path) : this.bare,
sourceMap: this.sourceMaps,
sourceFiles: [path],
literate: isLiterate(path)
};
var compiled;
try {
compiled = coffeescript.compile(data, options);
} catch (err) {
var loc = err.location, error;
if (loc) {
error = loc.first_line + ":" + loc.first_column + " " + (err.toString());
} else {
error = err.toString();
}
return callback(error);
}
var result = (options.sourceMap && typeof compiled === 'object') ? {
data: compiled.js,
map: compiled.v3SourceMap
} : {
data: compiled
};
return callback(null, result);
};
module.exports = CoffeeScriptCompiler;