Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app.lab;
- import java.io.BufferedWriter;
- public class BabelLab {
- public static void main(String[] args) throws Exception {
- // https://github.com/Daniel15/babel-standalone
- String babelPath = "src/main/webapp/resources/js/babel.min.js";
- Reader babelScript = new InputStreamReader(new FileInputStream(babelPath), StandardCharsets.UTF_8.name());
- ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType("text/javascript");
- SimpleBindings bindings = new SimpleBindings();
- engine.eval(babelScript, bindings);
- String eval = "Babel.transform(input, { presets: ['es2015', 'react', 'stage-2'] }).code";
- // bindings.put("input", "<Component name={name} age='16' hobby={'reading'} />");
- // Object output = engine.eval(eval, bindings);
- // System.out.println(output);
- Map<String, String> jsxPathMap = new ImmutableMap.Builder<String, String>()
- .put("src/main/webapp/resources/js/form.js.jsx", "src/main/webapp/resources/js/form.js")
- .put("src/main/webapp/resources/js/reactcomp.js.jsx", "src/main/webapp/resources/js/reactcomp.js")
- .build();
- for (String jsxPath: jsxPathMap.keySet()) {
- byte[] fileBytes = Files.readAllBytes(new File(jsxPath).toPath());
- String fileString = new String(fileBytes, StandardCharsets.UTF_8.name());
- bindings.put("input", fileString);
- System.out.print("converting " + jsxPath + " ... ");
- String outputx = (String) engine.eval(eval, bindings);
- String outPath = jsxPathMap.get(jsxPath);
- System.out.println("Done ! Now writing to " + outPath);
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(outPath))) {
- writer.write(outputx);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement