diff --git a/dukpy/__init__.py b/dukpy/__init__.py index f9e3ca2..7dfe272 100644 --- a/dukpy/__init__.py +++ b/dukpy/__init__.py @@ -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 \ No newline at end of file diff --git a/dukpy/babel.py b/dukpy/babel.py index 6da1bc7..e281986 100644 --- a/dukpy/babel.py +++ b/dukpy/babel.py @@ -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'] diff --git a/tests/test_transpilers.py b/tests/test_transpilers.py index 3218082..36cfd49 100644 --- a/tests/test_transpilers.py +++ b/tests/test_transpilers.py @@ -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 =

Hello, world!

;') + + expected = """"use strict"; + +var react_hello = React.createElement(\n "h1",\n null,\n "Hello, world!"\n);""" + + assert expected == ans, report_diff(expected, ans) \ No newline at end of file