mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 12:20:38 +01: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:
parent
1baca43391
commit
fc5bb9c9b2
30
main.js
30
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user