From fc5bb9c9b254e3646b90f6b6ce37bfaf64f1129f Mon Sep 17 00:00:00 2001 From: Andrew Davey Date: Thu, 16 Jan 2014 18:39:33 +0000 Subject: [PATCH 1/2] Add sourceMap option to react-tools transform Allow tools like grunt-react to include inline source maps in the generated JavaScript. Browserify can then combine these source maps when bundling everything together. Usage: ``` var transform = require('react-tools').transform; var output = transform(jsxContent, { sourceMap: true, sourceFilename: 'source.jsx' }); ``` The `output` will have an inline source map comment appended. --- main.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/main.js b/main.js index fc67157a3ac1..91de0b760978 100644 --- a/main.js +++ b/main.js @@ -2,15 +2,31 @@ 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 result = transform(visitors.react, 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 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; +} From 2b387957693f68dea876e77d1fc6f8cee3ed9354 Mon Sep 17 00:00:00 2001 From: Andrew Davey Date: Sun, 18 May 2014 17:51:14 +0100 Subject: [PATCH 2/2] Rebased on master --- main.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 91de0b760978..826104ff89a4 100644 --- a/main.js +++ b/main.js @@ -8,7 +8,8 @@ module.exports = { React: React, transform: function(input, options) { options = options || {}; - var result = transform(visitors.react, input, options); + var visitorList = getVisitors(options.harmony); + var result = transform(visitorList, input, options); var output = result.code; if (options.sourceMap) { var map = inlineSourceMap( @@ -22,10 +23,18 @@ module.exports = { } }; +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 ]; + json.sources = [sourceFilename]; + json.sourcesContent = [sourceCode]; var base64 = Buffer(JSON.stringify(json)).toString('base64'); return '//# sourceMappingURL=data:application/json;base64,' + base64;