mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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.
This commit is contained in:
@@ -2,15 +2,31 @@
|
|||||||
|
|
||||||
var visitors = require('./vendor/fbtransform/visitors');
|
var visitors = require('./vendor/fbtransform/visitors');
|
||||||
var transform = require('jstransform').transform;
|
var transform = require('jstransform').transform;
|
||||||
|
var Buffer = require('buffer').Buffer;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transform: function(code, options) {
|
React: React,
|
||||||
var visitorList;
|
transform: function(input, options) {
|
||||||
if (options && options.harmony) {
|
options = options || {};
|
||||||
visitorList = visitors.getAllVisitors();
|
var result = transform(visitors.react, input, options);
|
||||||
} else {
|
var output = result.code;
|
||||||
visitorList = visitors.transformVisitors.react;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user