Support for JSX transpiling

This commit is contained in:
Alessandro Molina 2016-04-09 22:20:41 +02:00
parent 5b06de69b8
commit 331a81b723
3 changed files with 17 additions and 1 deletions

View File

@ -1,5 +1,5 @@
from .evaljs import evaljs, JSInterpreter
from ._dukpy import JSRuntimeError
from .coffee import coffee_compile
from .babel import babel_compile
from .babel import babel_compile, jsx_compile
from .tsc import typescript_compile

View File

@ -18,3 +18,10 @@ def babel_compile(source, **kwargs):
es6code=source,
babel_options=kwargs
)
def jsx_compile(source, mode='react'):
modes = {
'react': ["transform-react-jsx"]
}
return babel_compile(source, plugins=modes.get(mode, []))['code']

View File

@ -71,3 +71,12 @@ var greeter = new Greeter("Hello, world!");
});"""
assert expected in ans, report_diff(expected, ans)
def test_jsx(self):
ans = dukpy.jsx_compile('var react_hello = <h1>Hello, world!</h1>;')
expected = """"use strict";
var react_hello = React.createElement(\n "h1",\n null,\n "Hello, world!"\n);"""
assert expected == ans, report_diff(expected, ans)