你好,babel-preset-env
2017年Babel 的官网上在9月宣布 ES2015 / ES2016/ ES2017 等等 ES20xx 时代的 presets 通通被废弃(deprecated),取而代之的是 babel-preset-env,并且承诺它将成为“未来不会过时的(future-proof)”解决方案。
也就是说: babel preset将基于你的实际浏览器及运行环境,自动的确定babel插件及polyfills,转译ES2015及此版本以上的语言。
npm install babel-preset-env --save-dev
在没有配置项的情况下,babel-preset-env表现的同babel-preset-latest一样(或者可以说同babel-preset-es2015, babel-preset-es2016, and babel-preset-es2017结合到一起,表现的一致),具体的操作方法可以参考:https://segmentfault.com/a/1190000011639765
再见,babel-preset-2015
安装 babel-preset-es2015 时
npm install --save-dev babel-preset-es2015
你会发现有如下的 Deprecated警告(文字很欢乐):
babel-preset-es2015@6.24.1: We're super ? excited that you're trying to use ES2015 syntax, but instead of making more yearly presets ? , Babel now has a better preset that we recommend you use instead: npm install babel-preset-env --save-dev. preset-env without options will compile ES2015+ down to ES5 just like using all the presets together and thus is more future proof. It also allows you to target specific browsers so that Babel can do less work and you can ship native ES2015+ to user ? ! We are also in the process of releasing v7, so please give http://babeljs.io/blog/2017/09/12/planning-for-7.0 a read and help test it out in beta! Thanks so much for using Babel ?, please give us a follow on Twitter @babeljs for news on Babel, join http://slack.babeljs.io for discussion/development and help sup port the project at opencollective.com/babel
是的,在2017年第三季度我们终于要和 ES2015 preset 说再见了。
那么你可能会问我们是要迁移到 ES2017 了吗?事实上并非如此,babel-preset-es2017 只是增加了一些特性而已,且仍然需要安装 ES2015。
如何迁移
为了节省你的阅读时间,我先给出一个如何升级的简单版:
首先卸载原来的 preset,然后安装 babel-preset-env:
npm uninstall --save-dev babel-preset-es2015
npm install --save-dev babel-preset-env@next
接下来将你的 .babelrc 文件中“es2015”修改“env”:
{
"presets": [ "env" ],
...
}
好了,恭喜你,就这么简单,你已经可以与 ES2015+ 保持更新了!
更近一步可以参考: https://zhuanlan.zhihu.com/p/29506685