Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (72 loc) · 2.24 KB
/
Copy pathindex.js
File metadata and controls
92 lines (72 loc) · 2.24 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
"use strict";
var docblock = require('jstransform/src/docblock');
var transform = require('jstransform').transform;
var reactTransform = require('react-tools').transform;
var visitors = require('react-tools/vendor/fbtransform/visitors');
var through = require('through');
var isJSXExtensionRe = /^.+\.jsx$/;
function parsePragma(data) {
return docblock.parseAsObject(docblock.extract(data));
}
function process(file, isJSXFile, transformer) {
transformer = transformer || reactTransform;
var data = '';
function write(chunk) {
return data += chunk;
}
function compile() {
// jshint -W040
var isJSXPragma = parsePragma(data).jsx !== undefined;
if (isJSXFile || isJSXPragma) {
if (!isJSXPragma) {
data = '/** @jsx React.DOM */' + data;
}
try {
var transformed = transformer(data);
this.queue(transformed);
} catch (error) {
error.name = 'ReactifyError';
error.message = file + ': ' + error.message;
error.fileName = file;
this.emit('error', error);
}
} else {
this.queue(data);
}
return this.queue(null);
// jshint +W040
}
return through(write, compile);
}
function getExtensionsMatcher(extensions) {
return new RegExp('\\.(' + extensions.join('|') + ')$');
}
module.exports = function(file, options) {
options = options || {};
var isJSXFile;
if (options.everything) {
isJSXFile = true;
} else {
var extensions = ['jsx']
.concat(options.extension)
.concat(options.x)
.filter(Boolean)
.map(function(ext) { return ext[0] === '.' ? ext.slice(1) : ext });
isJSXFile = getExtensionsMatcher(extensions).exec(file);
}
var transformVisitors = [].concat(
options.harmony || options.es6 ?
visitors.getAllVisitors() :
visitors.transformVisitors.react);
if (options.visitors) {
[].concat(options.visitors).forEach(function(id) {
transformVisitors = require(id).visitorList.concat(transformVisitors);
});
}
function transformer(source) {
return transform(transformVisitors, source).code;
}
return process(file, isJSXFile, transformer);
};
module.exports.process = process;
module.exports.isJSXExtensionRe = isJSXExtensionRe;