forked from andreypopp/reactify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (55 loc) · 1.55 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
/**
* Reactify — a Browserify transform for JSX (a superset of JS used by React.js)
*/
"use strict";
var ReactTools = require('react-tools');
var through = require('through');
function reactify(filename, options) {
options = options || {};
var buf = [];
function write(chunk) {
if (!Buffer.isBuffer(chunk)) {
chunk = new Buffer(chunk)
}
return buf.push(chunk)
}
function compile() {
var source = Buffer.concat(buf).toString();
// jshint -W040
if (isJSXFile(filename, options)) {
try {
var output = ReactTools.transform(source, {
es5: options.target === 'es5',
sourceMap: true,
sourceFilename: filename,
stripTypes: options['strip-types'] || options.stripTypes,
harmony: options.harmony || options.es6
});
this.queue(output);
} catch (error) {
error.name = 'ReactifyError';
error.message = filename + ': ' + error.message;
error.fileName = filename;
this.emit('error', error);
}
} else {
this.queue(source);
}
return this.queue(null);
// jshint +W040
}
return through(write, compile);
}
function isJSXFile(filename, options) {
if (options.everything) {
return true;
} else {
var extensions = ['js', 'jsx']
.concat(options.extension)
.concat(options.x)
.filter(Boolean)
.map(function(ext) { return ext[0] === '.' ? ext.slice(1) : ext });
return new RegExp('\\.(' + extensions.join('|') + ')$').exec(filename);
}
}
module.exports = reactify;