SHOW:
|
|
- or go back to the newest paste.
1 | package app.lab; | |
2 | ||
3 | import java.io.BufferedWriter; | |
4 | ||
5 | public class BabelLab { | |
6 | public static void main(String[] args) throws Exception { | |
7 | // https://github.com/Daniel15/babel-standalone | |
8 | String babelPath = "src/main/webapp/resources/js/babel.min.js"; | |
9 | Reader babelScript = new InputStreamReader(new FileInputStream(babelPath), StandardCharsets.UTF_8.name()); | |
10 | ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType("text/javascript"); | |
11 | ||
12 | SimpleBindings bindings = new SimpleBindings(); | |
13 | engine.eval(babelScript, bindings); | |
14 | ||
15 | String eval = "Babel.transform(input, { presets: ['es2015', 'react', 'stage-2'] }).code"; | |
16 | // bindings.put("input", "<Component name={name} age='16' hobby={'reading'} />"); | |
17 | // Object output = engine.eval(eval, bindings); | |
18 | // System.out.println(output); | |
19 | ||
20 | Map<String, String> jsxPathMap = new ImmutableMap.Builder<String, String>() | |
21 | .put("src/main/webapp/resources/js/form.js.jsx", "src/main/webapp/resources/js/form.js") | |
22 | .put("src/main/webapp/resources/js/reactcomp.js.jsx", "src/main/webapp/resources/js/reactcomp.js") | |
23 | .build(); | |
24 | for (String jsxPath: jsxPathMap.keySet()) { | |
25 | byte[] fileBytes = Files.readAllBytes(new File(jsxPath).toPath()); | |
26 | String fileString = new String(fileBytes, StandardCharsets.UTF_8.name()); | |
27 | bindings.put("input", fileString); | |
28 | System.out.print("converting " + jsxPath + " ... "); | |
29 | String outputx = (String) engine.eval(eval, bindings); | |
30 | String outPath = jsxPathMap.get(jsxPath); | |
31 | System.out.println("Done ! Now writing to " + outPath); | |
32 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(outPath))) { | |
33 | writer.write(outputx); | |
34 | } | |
35 | } | |
36 | } | |
37 | } |