forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
59 lines (54 loc) · 1.75 KB
/
main.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
'use strict';
/*eslint-disable no-undef*/
var visitors = require('./vendor/fbtransform/visitors');
var transform = require('jstransform').transform;
var typesSyntax = require('jstransform/visitors/type-syntax');
var Buffer = require('buffer').Buffer;
module.exports = {
transform: function(input, options) {
var output = innerTransform(input, options);
var result = output.code;
if (options && options.sourceMap) {
var map = inlineSourceMap(
output.sourceMap,
input,
options.sourceFilename
);
result += '\n' + map;
}
return result;
},
transformWithDetails: function(input, options) {
var output = innerTransform(input, options);
var result = {};
result.code = output.code;
if (options && options.sourceMap) {
result.sourceMap = output.sourceMap.toJSON();
}
return result;
}
};
function innerTransform(input, options) {
options = options || {};
var visitorSets = ['react'];
if (options.harmony) {
visitorSets.push('harmony');
}
if (options.stripTypes) {
// Stripping types needs to happen before the other transforms
// unfortunately, due to bad interactions. For example,
// es6-rest-param-visitors conflict with stripping rest param type
// annotation
input = transform(typesSyntax.visitorList, input, options).code;
}
var visitorList = visitors.getVisitorsBySet(visitorSets);
return transform(visitorList, input, options);
}
function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
var json = sourceMap.toJSON();
json.sources = [sourceFilename];
json.sourcesContent = [sourceCode];
var base64 = Buffer(JSON.stringify(json)).toString('base64');
return '//# sourceMappingURL=data:application/json;base64,' +
base64;
}