如果需要在服务器端转的话,需要用到javax.script.ScriptEngine,可以参考以下代码,但感觉性能一般
String babelScript = IOUtils.toString(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("com/ourteam/frame/web/babel.min.js"), "utf-8");
String polyfillScript = IOUtils.toString(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("com/ourteam/frame/web/polyfill.min.js"), "utf-8");
engine.eval(polyfillScript, babelBindings);
engine.eval(babelScript, babelBindings);
babelBindings.put("input", "<Component />");
System.out.println( engine.eval("Babel.transform(input, { presets: ['es2015', 'react', 'stage-0'] }).code", babelBindings));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…