Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81145 views
1
'use strict';
2
3
var visitors = require('./vendor/fbtransform/visitors');
4
var transform = require('jstransform').transform;
5
var typesSyntax = require('jstransform/visitors/type-syntax');
6
var Buffer = require('buffer').Buffer;
7
8
module.exports = {
9
transform: function(input, options) {
10
var output = innerTransform(input, options);
11
var result = output.code;
12
if (options && options.sourceMap) {
13
var map = inlineSourceMap(
14
output.sourceMap,
15
input,
16
options.sourceFilename
17
);
18
result += '\n' + map;
19
}
20
return result;
21
},
22
transformWithDetails: function(input, options) {
23
var output = innerTransform(input, options);
24
var result = {};
25
result.code = output.code;
26
if (options && options.sourceMap) {
27
result.sourceMap = output.sourceMap.toJSON();
28
}
29
return result;
30
}
31
};
32
33
function innerTransform(input, options) {
34
options = options || {};
35
36
var visitorSets = ['react'];
37
if (options.harmony) {
38
visitorSets.push('harmony');
39
}
40
if (options.stripTypes) {
41
// Stripping types needs to happen before the other transforms
42
// unfortunately, due to bad interactions. For example,
43
// es6-rest-param-visitors conflict with stripping rest param type
44
// annotation
45
input = transform(typesSyntax.visitorList, input, options).code;
46
}
47
48
var visitorList = visitors.getVisitorsBySet(visitorSets);
49
return transform(visitorList, input, options);
50
}
51
52
function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
53
var json = sourceMap.toJSON();
54
json.sources = [sourceFilename];
55
json.sourcesContent = [sourceCode];
56
var base64 = Buffer(JSON.stringify(json)).toString('base64');
57
return '//# sourceMappingURL=data:application/json;base64,' +
58
base64;
59
}
60
61