Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
"use strict";
2
3
var transform = require('jstransform').transform;
4
var reactTransform = require('react-tools').transform;
5
var visitors = require('react-tools/vendor/fbtransform/visitors');
6
var typeSyntax = require('jstransform/visitors/type-syntax');
7
var through = require('through');
8
9
var isJSXExtensionRe = /^.+\.jsx$/;
10
11
function process(file, isJSXFile, transformer) {
12
transformer = transformer || reactTransform;
13
14
var data = '';
15
function write(chunk) {
16
return data += chunk;
17
}
18
19
function compile() {
20
// jshint -W040
21
if (isJSXFile) {
22
try {
23
var transformed = transformer(data);
24
this.queue(transformed);
25
} catch (error) {
26
error.name = 'ReactifyError';
27
error.message = file + ': ' + error.message;
28
error.fileName = file;
29
30
this.emit('error', error);
31
}
32
} else {
33
this.queue(data);
34
}
35
return this.queue(null);
36
// jshint +W040
37
}
38
39
return through(write, compile);
40
}
41
42
function getExtensionsMatcher(extensions) {
43
return new RegExp('\\.(' + extensions.join('|') + ')$');
44
}
45
46
var VISITORS_OPTION_WARNED = false;
47
48
module.exports = function(file, options) {
49
options = options || {};
50
51
var isJSXFile;
52
53
if (options.everything) {
54
55
isJSXFile = true;
56
} else {
57
var extensions = ['js', 'jsx']
58
.concat(options.extension)
59
.concat(options.x)
60
.filter(Boolean)
61
.map(function(ext) { return ext[0] === '.' ? ext.slice(1) : ext });
62
63
isJSXFile = getExtensionsMatcher(extensions).exec(file);
64
}
65
66
var transformVisitors = [].concat(
67
options.harmony || options.es6 ?
68
visitors.getAllVisitors() :
69
visitors.transformVisitors.react);
70
71
if (options.visitors) {
72
if (!VISITORS_OPTION_WARNED) {
73
VISITORS_OPTION_WARNED = true;
74
console.warn(
75
'reactify: option "visitors" is deprecated ' +
76
'and will be removed in future releases'
77
);
78
}
79
[].concat(options.visitors).forEach(function(id) {
80
transformVisitors = require(id).visitorList.concat(transformVisitors);
81
});
82
}
83
84
var transformOptions = {
85
es5: options.target === 'es5'
86
};
87
88
function transformer(source) {
89
// Stripping types needs to happen before the other transforms
90
if (options['strip-types'] || options.stripTypes) {
91
source = transform(typeSyntax.visitorList, source, transformOptions).code;
92
}
93
94
return transform(transformVisitors, source, transformOptions).code;
95
}
96
97
return process(file, isJSXFile, transformer);
98
};
99
module.exports.process = process;
100
module.exports.isJSXExtensionRe = isJSXExtensionRe;
101
102