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.
This commit is contained in:
Paul O’Shannessy 2015-02-18 19:02:56 -08:00
parent 6c29eba035
commit 0e0a8f65c5
3 changed files with 50 additions and 36 deletions

20
main.js
View File

@ -1,9 +1,18 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'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;
var inlineSourceMap = require('./vendor/inline-source-map');
module.exports = {
transform: function(input, options) {
@ -54,12 +63,3 @@ function innerTransform(input, options) {
}
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;
}

View File

@ -11,10 +11,8 @@
'use strict';
var buffer = require('buffer');
var transform = require('jstransform').transform;
var typesSyntax = require('jstransform/visitors/type-syntax');
var visitors = require('./fbtransform/visitors');
var ReactTools = require('../main');
var inlineSourceMap = require('./inline-source-map');
var headEl;
var dummyAnchor;
@ -34,26 +32,18 @@ var supportsAccessors = Object.prototype.hasOwnProperty('__defineGetter__');
* @return {object} object as returned from jstransform
*/
function transformReact(source, options) {
// TODO: just use react-tools
options = options || {};
var visitorList;
if (options.harmony) {
visitorList = visitors.getAllVisitors();
} else {
visitorList = visitors.transformVisitors.react;
// Force the sourcemaps option manually. We don't want to use it if it will
// break (see above note about supportsAccessors). We'll only override the
// value here if sourceMap was specified and is truthy. This guarantees that
// we won't override any user intent (since this method is exposed publicly).
if (options.sourceMap) {
options.sourceMap = supportsAccessors;
}
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
source = transform(typesSyntax.visitorList, source, options).code;
}
return transform(visitorList, source, {
sourceMap: supportsAccessors && options.sourceMap
});
// Otherwise just pass all options straight through to react-tools.
return ReactTools.transformWithDetails(source, options);
}
/**
@ -149,7 +139,6 @@ function transformCode(code, url, options) {
return transformed.code;
}
var map = transformed.sourceMap.toJSON();
var source;
if (url == null) {
source = "Inline JSX script";
@ -165,13 +154,11 @@ function transformCode(code, url, options) {
dummyAnchor.href = url;
source = dummyAnchor.pathname.substr(1);
}
map.sources = [source];
map.sourcesContent = [code];
return (
transformed.code +
'\n//# sourceMappingURL=data:application/json;base64,' +
buffer.Buffer(JSON.stringify(map)).toString('base64')
'\n' +
inlineSourceMap(transformed.sourceMap, code, source)
);
}

27
vendor/inline-source-map.js vendored Normal file
View File

@ -0,0 +1,27 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
var Buffer = require('buffer').Buffer;
function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
// This can be used with a sourcemap that has already has toJSON called on it.
// Check first.
var json = sourceMap;
if (typeof sourceMap.toJSON === 'function') {
json = sourceMap.toJSON();
}
json.sources = [sourceFilename];
json.sourcesContent = [sourceCode];
var base64 = Buffer(JSON.stringify(json)).toString('base64');
return '//# sourceMappingURL=data:application/json;base64,' + base64;
}
module.exports = inlineSourceMap;