Skip to content

关于babel-core #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
thelongmarch opened this issue Feb 7, 2018 · 2 comments
Open

关于babel-core #2

thelongmarch opened this issue Feb 7, 2018 · 2 comments

Comments

@thelongmarch
Copy link

有个疑问,以编程的方式来使用 Babel,如果某些代码需要调用Babel的API进行转码,就要使用babel-core模块。为啥转换的只是{ code, map, ast },不是应该转换成es5代码啥的,那这个模块在实际项目中到底怎么用的?求告知

@sunyongjian
Copy link
Owner

@thelongmarch babel-core 是 babel 语法分析的核心库,的确是会如你所说的,指定 plugins 执行后得到的 result 是一个对象,它包含很多内容,比如你的 options (也就是 core 执行传给的参数),ast 是抽象语法树,可以理解为是给解析器看的,然后 code 就是编译后的代码了,你可以打出来看看~ 但是像我们平常用,很少直接用到 babel-core,通常是用 babel-cli 或者是 babel-loader。babel-cli 就是执行 babel 的时候调用的,比如 babel a.js --out-file b.js,就是把 a 文件通过 babel 编译得到 b,而 cli 内部是依赖 babel-core 处理的,只是变成了命令行工具方便我们使用。而 babel-loader 就是结合 webpack 使用的,实现也是依赖 babel-core。core 在项目中,基本不会用到,因为已经有封装好的工具了,但是你仍然可以自己去通过写脚本的形式做一些事,通常是工具类的。比如通过 transformFileSync 编译得到 code,写到新的文件里去,或者实现一个前端项目,可以上传文件,让用户配置参数,做 babel 的编译处理,然后可以把编译好的文件再反给用户下载。

@thelongmarch
Copy link
Author

谢谢了,还是你理解的透彻,我一直纳闷为啥很少看到项目中使用,core确实是babel的核心,佩服!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants