Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example performing babel conversions
- Loading branch information
Jb Aviat
committed
Aug 2, 2016
1 parent
526abee
commit 0443eb8
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python | ||
""" Transform the input stream using babel.transform """ | ||
import os | ||
import sys | ||
|
||
from py_mini_racer import py_mini_racer | ||
|
||
def babel_transform(es_string): | ||
""" Transform the provided string using babel.transform """ | ||
|
||
path_to_babel = os.path.join(os.path.dirname(__file__), '..', 'tests', | ||
'fixtures', 'babel.js') | ||
|
||
es6 = '[1,2,3].map(n => n + 1);' | ||
|
||
babel_source = open(path_to_babel, "r").read() | ||
|
||
# Initializes PyMiniRacer | ||
ctx = py_mini_racer.MiniRacer() | ||
|
||
# Parse babel | ||
ctx.eval("""var self = this; %s """ % babel_source) | ||
|
||
# Transform stuff :) | ||
val = "babel.transform(`%s`)['code']" % es_string | ||
res = ctx.eval(val) | ||
return res | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
if len(sys.argv) != 1: | ||
name = sys.argv[0] | ||
sys.stderr.write("Usage: cat es6file.js | %s\n" % name) | ||
sys.stderr.write("Example: echo [1,2,3].map(n => n + 1); | %s\n" % name) | ||
sys.exit(-1) | ||
|
||
es6_data = sys.stdin.read() | ||
|
||
res = babel_transform(es6_data) | ||
|
||
print(res) |