设置 Babel 6
Babel 6 刚刚发布,我们还在整理很多东西。在过去两天里,我们在 GitHub 和 Slack 上的活动比以往任何时候都多。每个帮忙的人都做得很棒。
🌐 Babel 6 is fresh off the press and we’re still getting a lot sorted out. In the past two days we've seen more activity on GitHub and Slack than ever before. Everyone who has been helping out has been doing a great job.
不过,目前文档仍然不够完善,我正在浏览并创建网站的全新“入门指南”部分。
🌐 However, the documentation is still lacking at this point, I’m currently going through and creating an entirely new “Getting Started” section of the site.
这篇博客文章会涵盖大部分基础内容,直到完成为止。
🌐 This blog post will cover most of the basics until that’s completed.
安装 Babel
🌐 Installing Babel
babel 包已经不复存在了。以前,它包含了整个编译器和所有的转换工具,还有一堆 CLI 工具,但这导致下载包不必要地大且有点令人困惑。现在我们把它拆分成了两个独立的包:babel-cli 和 babel-core。
🌐 The babel package is no more. Previously, it was the entire compiler and all the transforms plus a bunch of CLI tools, but this lead to unnecessarily large downloads and was a bit confusing. Now we’ve split it up into two separate packages: babel-cli and babel-core.
$ npm install --global babel-cli
# or
$ npm install --save-dev babel-core
如果你想从命令行使用 Babel,你可以安装 babel-cli;如果你想使用 Node API,你可以安装 babel-core。
🌐 If you want to use Babel from the CLI you can install babel-cli or if you want to use the Node API you can install babel-core.
现在,大多数集成都还没有更新到 Babel 6,但这在接下来的几天里会有所变化。
🌐 Right now, most integrations haven’t been updated for Babel 6, but that will be changing over the coming days.
添加插件和预设
🌐 Adding Plugins and Presets
Babel 6 不带任何默认转换,所以当你在一个文件上运行 Babel 时,它只是把文件原样输出,不会做任何改变。
🌐 Babel 6 ships without any default transforms, so when you run Babel on a file it will just print it back out to you without changing anything.
为了开始编译各种功能,你需要安装插件。例如,如果你想使用箭头函数:
🌐 In order to start compiling various features you need to install plugins. For example if you want to use arrow functions:
首先安装箭头函数插件:
🌐 First install the arrow functions plugin:
$ npm install --save-dev babel-plugin-transform-es2015-arrow-functions
然后像这样把它添加到你的 .babelrc 文件中:
🌐 Then add it to your .babelrc file like this:
{
"plugins": ["transform-es2015-arrow-functions"]
}
现在,如果你在一个含有箭头函数的文件上运行 Babel,它们会被编译。很简单吧?别太快下结论。
🌐 Now if you run Babel on a file with arrow functions they will be compiled. Easy right? Not so fast.
Babel 插件的设计是为了逐步编译。比如说,如果你想使用一个 ES2019 的特性,它会先编译成 ES2018,再编译成 ES2017,依此类推。这保证了如果人们想用原生实现的话也是可以的。
🌐 Babel plugins are designed to compile down incrementally. Say if you wanted to use an ES2019 feature, it would be compiled down to ES2018 then ES2017 and so on. This ensures that people can use native implementation if they want to.
这种情况甚至会发生在同一个规范内,例如 ES2015 的常量会被编译成 ES2015 的 let 变量。所以如果你想把它一直编译到 ES5,你也需要进行相应的编译。
🌐 This even happens within a single specification, for example ES2015 constants will be compiled to ES2015 let variables. So if you want it to go all the way down to ES5 you need to compile that as well.
$ npm install --save-dev babel-plugin-check-es2015-constants
$ npm install --save-dev babel-plugin-transform-es2015-block-scoping
{
"plugins": [
"check-es2015-constants",
"transform-es2015-block-scoping"
]
}
这些依赖不容易跟踪,如果你不想手动指定它们全部,你也可以改为安装一个预设。
🌐 These dependencies aren’t easy to keep track and if you don’t want to manually specify them all you may install a preset instead.
$ npm install --save-dev babel-preset-es2015
{
"presets": ["es2015"]
}
包括所有 JavaScript 版本:
🌐 To Include all JavaScript versions:
$ npm install --save-dev babel-preset-env
{
"presets": ["env"]
}
React 也有它自己的预设:
🌐 React also has it’s own preset:
$ npm install --save-dev babel-preset-react
{
"presets": ["react"]
}
这也是 stage 选项被替换成的内容:
🌐 This is also what the stage option has been replaced with:
$ npm install --save-dev babel-preset-stage-2
{
"presets": ["stage-2"]
}
注意:舞台预设会自动包含其上方的所有阶段(即阶段1包括2和3)