-
Notifications
You must be signed in to change notification settings - Fork 1
/
cjsx-in-browser-src.js
32 lines (24 loc) · 1.09 KB
/
cjsx-in-browser-src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(function(){
var coffeeReactTransform = require('coffee-react-transform')
var CoffeeScript = require('coffee-script')
function sourceMapComment(originalCJSX, transformedCJSX, javascript, sourceMap, fileName) {
sourceMap.sources = [fileName, fileName + "_transformed", fileName + "_compiled"];
sourceMap.sourcesContent = [originalCJSX, transformedCJSX, javascript];
var base64 = btoa(JSON.stringify(sourceMap));
var dataUri = 'data:application/json;charset=utf-8;base64,' + base64;
return "\n//@ sourceMappingURL=" + dataUri;
}
function processCJSX (originalCJSX) {
var coffee = coffeeReactTransform(originalCJSX);
var compilerResult = CoffeeScript.compile(coffee, {
sourceMap: true
});
var javascript = compilerResult.js;
var v3SourceMap = JSON.parse(compilerResult.v3SourceMap);
eval(javascript + sourceMapComment(originalCJSX, coffee, javascript, v3SourceMap, "cjsx" + i));
}
var scripts = document.querySelectorAll("script[type='text/cjsx']");
for (var i = 0; i < scripts.length; i++){
processCJSX(scripts[i].innerHTML);
}
})();