Use same pathways for browser transforms as we use in react-tools
This makes sure we can use the same options everywhere, even from react-rails, without having to specify each one separately in JSXTransformer as well.
Paul O’Shannessy committed
Feb 18, 2015 at 19:02 UTC
0e0a8f65c52cae5c1e7ea7ef4bd196465667f842
3 files changed
+49
-35
main.js
+10
-10
@@ -1,9 +1,18 @@
1
+/**
2
+ * Copyright 2013-2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
'use strict';
11
/*eslint-disable no-undef*/
12
var visitors = require('./vendor/fbtransform/visitors');
13
var transform = require('jstransform').transform;
14
var typesSyntax = require('jstransform/visitors/type-syntax');
6
-var Buffer = require('buffer').Buffer;
15
+var inlineSourceMap = require('./vendor/inline-source-map');
16
17
module.exports = {
18
transform: function(input, options) {
@@ -54,12 +63,3 @@ function innerTransform(input, options) {
63
}
64
return transform(visitorList, input, options);
65
}
57
-
58
-function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
59
- var json = sourceMap.toJSON();
60
- json.sources = [sourceFilename];
61
- json.sourcesContent = [sourceCode];
62
- var base64 = Buffer(JSON.stringify(json)).toString('base64');
63
- return '//# sourceMappingURL=data:application/json;base64,' +
64
- base64;
65
-}
vendor/browser-transforms.js
+12
-25
@@ -11,10 +11,8 @@
11
12
'use strict';
13
14
-var buffer = require('buffer');
15
-var transform = require('jstransform').transform;
16
-var typesSyntax = require('jstransform/visitors/type-syntax');
17
-var visitors = require('./fbtransform/visitors');
14
+var ReactTools = require('../main');
15
+var inlineSourceMap = require('./inline-source-map');
16
17
var headEl;
18
var dummyAnchor;
@@ -34,26 +32,18 @@ var supportsAccessors = Object.prototype.hasOwnProperty('__defineGetter__');
32
* @return {object} object as returned from jstransform
33
*/
34
function transformReact(source, options) {
37
- // TODO: just use react-tools
35
options = options || {};
39
- var visitorList;
40
- if (options.harmony) {
41
- visitorList = visitors.getAllVisitors();
42
- } else {
43
- visitorList = visitors.transformVisitors.react;
44
- }
36
46
- if (options.stripTypes) {
47
- // Stripping types needs to happen before the other transforms
48
- // unfortunately, due to bad interactions. For example,
49
- // es6-rest-param-visitors conflict with stripping rest param type
50
- // annotation
51
- source = transform(typesSyntax.visitorList, source, options).code;
37
+ // Force the sourcemaps option manually. We don't want to use it if it will
38
+ // break (see above note about supportsAccessors). We'll only override the
39
+ // value here if sourceMap was specified and is truthy. This guarantees that
40
+ // we won't override any user intent (since this method is exposed publicly).
41
+ if (options.sourceMap) {
42
+ options.sourceMap = supportsAccessors;
43
}
44
54
- return transform(visitorList, source, {
55
- sourceMap: supportsAccessors && options.sourceMap
56
- });
45
+ // Otherwise just pass all options straight through to react-tools.
46
+ return ReactTools.transformWithDetails(source, options);
47
}
48
49
/**
@@ -149,7 +139,6 @@ function transformCode(code, url, options) {
139
return transformed.code;
140
}
141
152
- var map = transformed.sourceMap.toJSON();
142
var source;
143
if (url == null) {
144
source = "Inline JSX script";
@@ -165,13 +154,11 @@ function transformCode(code, url, options) {
154
dummyAnchor.href = url;
155
source = dummyAnchor.pathname.substr(1);
156
}
168
- map.sources = [source];
169
- map.sourcesContent = [code];
157
158
return (
159
transformed.code +
173
- '\n//# sourceMappingURL=data:application/json;base64,' +
174
- buffer.Buffer(JSON.stringify(map)).toString('base64')
160
+ '\n' +
161
+ inlineSourceMap(transformed.sourceMap, code, source)
162
);
163
}
164
vendor/inline-source-map.js
new
+27
@@ -0,0 +1,27 @@
1
+/**
2
+ * Copyright 2013-2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
+'use strict';
11
+
12
+var Buffer = require('buffer').Buffer;
13
+
14
+function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
15
+ // This can be used with a sourcemap that has already has toJSON called on it.
16
+ // Check first.
17
+ var json = sourceMap;
18
+ if (typeof sourceMap.toJSON === 'function') {
19
+ json = sourceMap.toJSON();
20
+ }
21
+ json.sources = [sourceFilename];
22
+ json.sourcesContent = [sourceCode];
23
+ var base64 = Buffer(JSON.stringify(json)).toString('base64');
24
+ return '//# sourceMappingURL=data:application/json;base64,' + base64;
25
+}
26
+
27
+module.exports = inlineSourceMap;