Skip to content

Commit

Permalink
Merge pull request facebook#910 from andrewdavey/react-transform-sour…
Browse files Browse the repository at this point in the history
…ce-map

Add sourceMap option to react-tools transform
  • Loading branch information
zpao committed Jun 16, 2014
2 parents 20a5041 + 2b38795 commit 08a207f
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,40 @@

var visitors = require('./vendor/fbtransform/visitors');
var transform = require('jstransform').transform;
var Buffer = require('buffer').Buffer;

module.exports = {
transform: function(code, options) {
var visitorList;
if (options && options.harmony) {
visitorList = visitors.getAllVisitors();
} else {
visitorList = visitors.transformVisitors.react;
React: React,
transform: function(input, options) {
options = options || {};
var visitorList = getVisitors(options.harmony);
var result = transform(visitorList, input, options);
var output = result.code;
if (options.sourceMap) {
var map = inlineSourceMap(
result.sourceMap,
input,
options.sourceFilename
);
output += '\n' + map;
}
return transform(visitorList, code).code;
return output;
}
};

function getVisitors(harmony) {
if (harmony) {
return visitors.getAllVisitors();
} else {
return visitors.transformVisitors.react;
}
}

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;
}

0 comments on commit 08a207f

Please sign in to comment.