@samitouri / QOS-React-2 / commits / fc5bb9c9b2

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.

Andrew Davey committed Jan 16, 2014 at 18:39 UTC fc5bb9c9b254e3646b90f6b6ce37bfaf64f1129f
1 file changed +23 -7
main.js
+23 -7
@@ -2,15 +2,31 @@
2
3 var visitors = require('./vendor/fbtransform/visitors');
4 var transform = require('jstransform').transform;
5 +var Buffer = require('buffer').Buffer;
6
7 module.exports = {
7 - transform: function(code, options) {
8 - var visitorList;
9 - if (options && options.harmony) {
10 - visitorList = visitors.getAllVisitors();
11 - } else {
12 - visitorList = visitors.transformVisitors.react;
8 + React: React,
9 + transform: function(input, options) {
10 + options = options || {};
11 + var result = transform(visitors.react, input, options);
12 + var output = result.code;
13 + if (options.sourceMap) {
14 + var map = inlineSourceMap(
15 + result.sourceMap,
16 + input,
17 + options.sourceFilename
18 + );
19 + output += '\n' + map;
20 }
14 - return transform(visitorList, code).code;
21 + return output;
22 }
23 };
24 +
25 +function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
26 + var json = sourceMap.toJSON();
27 + json.sources = [ sourceFilename ];
28 + json.sourcesContent = [ sourceCode ];
29 + var base64 = Buffer(JSON.stringify(json)).toString('base64');
30 + return '//# sourceMappingURL=data:application/json;base64,' +
31 + base64;
32 +}