forked from jnordberg/coffeeify
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (97 loc) · 3.28 KB
/
index.js
File metadata and controls
117 lines (97 loc) · 3.28 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var util = require('util');
var through = require('through');
var convert = require('convert-source-map');
var coffeereact = require('coffee-react');
function hasCoffeeExtension (file) {
return (/\.((lit)?coffee|coffee\.md)$/).test(file);
}
function hasLiterateExtension (file) {
return (/\.(litcoffee|coffee\.md)$/).test(file);
}
function ParseError(error, src, file) {
/* Creates a ParseError from a CoffeeScript SyntaxError
modeled after substack's syntax-error module */
SyntaxError.call(this);
this.message = error.message;
this.line = error.location.first_line + 1; // cs linenums are 0-indexed
this.column = error.location.first_column + 1; // same with columns
var markerLen = 2;
if(error.location.first_line === error.location.last_line) {
markerLen += error.location.last_column - error.location.first_column;
}
this.annotated = [
file + ':' + this.line,
src.split('\n')[this.line - 1],
Array(this.column).join(' ') + Array(markerLen).join('^'),
'ParseError: ' + this.message
].join('\n');
}
ParseError.prototype = Object.create(SyntaxError.prototype);
ParseError.prototype.toString = function () {
return this.annotated;
};
ParseError.prototype.inspect = function () {
return this.annotated;
};
function compile(file, data, callback) {
var compiled;
try {
compiled = coffeereact.compile(data, {
sourceMap: true,
generatedFile: file,
inline: true,
bare: true,
literate: hasLiterateExtension(file)
});
} catch (e) {
var error = e;
if (e.location) {
error = new ParseError(e, data, file);
}
callback(error);
return;
}
var map = convert.fromJSON(compiled.v3SourceMap);
map.setProperty('sources', [file]);
callback(null, compiled.js + '\n' + map.toComment() + '\n');
}
function coffeereactify(file, opts) {
opts = opts || {};
var passthroughCoffee = opts['coffeeout'] || false;
var hasCoffeeExt = hasCoffeeExtension(file);
var hasCJSXExt = coffeereact.hasCJSXExtension(file);
if (!(hasCoffeeExt || hasCJSXExt)) {
return through();
}
var data = '', stream = through(write, end);
return stream;
function write(buf) {
data += buf;
}
function end() {
if (hasCoffeeExt && passthroughCoffee) {
// passthrough un-compiled coffeescript
var transformed;
try {
transformed = coffeereact.transform(data);
} catch (error) {
return stream.emit('error', error);
}
stream.queue(transformed || data);
stream.queue(null);
} else {
// otherwise compile either cjsx or pure coffee
compile(file, data, function(error, result) {
if (error) stream.emit('error', error);
stream.queue(result);
stream.queue(null);
});
}
}
}
coffeereactify.compile = compile;
coffeereactify.isCoffee = hasCoffeeExtension;
coffeereactify.isLiterate = hasLiterateExtension;
coffeereactify.hasCJSXExtension = coffeereact.hasCJSXExtension;
coffeereactify.hasCJSXPragma = coffeereact.hasCJSXPragma;
module.exports = coffeereactify;