Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

现有java项目中引入babel?

现有项目后台是java,在项目途中想慢慢写一些ES6的语法,百度了一下教程,引入babel都是grunt,或者glup等构建项目的,有没有能现在现有项目中写ES6,然后实时转码的工具?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如果需要在服务器端转的话,需要用到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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...