简体   繁体   中英

How to use babel to transform es6 to es5 programmatically?

I have a function which runs string as javascript code through eval() . It works fine if the string is es5 but doesn't work for es6 . I know babel can transport es6 to es5 but most of them use cases are done in compile stage. How can I use babel programmatically?

Babel has an API .

I assume you can do something like this:

eval(babel.transform(code, options).code)

However I would strongly reconsider that! First, eval is usually a very, very dangerous thing, and next babel is huge. You don't want to deliver that to a browser if you don't have to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM