Babel 中文文档
  • 印记中文
  • 文档
  • 配置
  • 试用
  • 视频
  • 博客
  • 赞助
  • 团队
  • GitHub

›All Blog Posts

All Blog Posts

  • 7.19.0 Released: Stage 3 decorators and more RegExp features!
  • 7.18.0 Released: Destructuring private elements and TypeScript 4.7
  • 7.17.0 Released: RegExp 'v' mode and ... 🥁 decorators!
  • 7.16.0 发布: ESLint 8 和 TypeScript 4.5
  • 7.15.0 发布:支持 Hack-style 管道, TypeScript 枚举常量和 Rhino 目标
  • Babel is used by millions, so why are we running out of money?
  • 7.14.0 Released: New class features enabled by default, TypeScript 4.3, and better CommonJS interop
  • 7.13.0 Released: Records and Tuples, granular compiler assumptions, and top-level targets
  • 7.12.0 Released: TypeScript 4.1, strings as import/export names, and class static blocks
  • 7.11.0 Released: ECMAScript 2021 support in preset-env, TypeScript 4.0 support, printing config and the future of `babel-eslint`
  • The State of babel-eslint
  • 7.10.0 Released: Class Fields in preset-env, '#private in' checks and better React tree-shaking
  • 7.9.0 Released: Smaller preset-env output, Typescript 3.8 support and a new JSX transform
  • 7.8.0 Released: ECMAScript 2020, .mjs configuration files and @babel/cli improvements
  • Babel's Funding Plans
  • 7.7.0 Released: Error recovery and TypeScript 3.7
  • 7.6.0 Released: Private static accessors and V8 intrinsic syntax
  • 7.5.0 Released: dynamic import and F# pipelines
  • The Babel Podcast
  • 7.4.0 Released: core-js 3, static private methods and partial application
  • 7.3.0 Released: Named capturing groups, private instance accessors and smart pipelines
  • 7.2.0 发布:私有实例方法(Private Instance Methods)
  • 在 Babel 中支持 TC39 标准的装饰器
  • 7.1.0 Released: Decorators, Private Static Fields
  • Babel 7 发布
  • Removing Babel's Stage Presets
  • What's Happening With the Pipeline (|>) Proposal?
  • Announcing Babel's New Partnership with trivago!
  • On Consuming (and Publishing) ES2015+ Packages
  • Nearing the 7.0 Release
  • Babel Turns Three
  • Planning for 7.0
  • Zero-config code transformation with babel-plugin-macros
  • Contributing to Babel: Three Lessons to Remember
  • Personal Experiences at Babel #1 — A PR with Unusually High Number of Reviews
  • Babel and Summer of Code 2017
  • Upgrade to Babel 7 (moved)
  • Upgrade to Babel 7 for Tool Authors (WIP)
  • 6.23.0 Released
  • The State of Babel
  • 6.19.0 Released
  • 6.18.0 Released
  • 6.16.0 Released
  • Babili (babel-minify)
  • 6.14.0 Released
  • Babel Doctor
  • Setting up Babel 6
  • 6.0.0 Released
  • React on ES6+
  • Function Bind Syntax
  • 5.0.0 Released
  • Babel 喜爱 React
  • 并非出生而逐渐走向灭亡
  • 2to3
  • 6to5 + esnext

Setting up Babel 6

October 31, 2015

James Kyle

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.

Installing Babel

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

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.

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 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

Then add it to your .babelrc file like this:

{
  "plugins": ["transform-es2015-arrow-functions"]
}

Now if you run Babel on a file with arrow functions they will be compiled. Easy right? Not so fast.

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.

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"]
}

To Include all JavaScript versions:

$ npm install --save-dev babel-preset-env
{
  "presets": ["env"]
}

React also has it’s own preset:

$ npm install --save-dev babel-preset-react
{
  "presets": ["react"]
}

This is also what the stage option has been replaced with:

$ npm install --save-dev babel-preset-stage-2
{
  "presets": ["stage-2"]
}

Note: Stage presets include all the stages above them automatically (ie. stage 1 includes 2 and 3)

Recent Posts
  • Installing Babel
  • Adding Plugins and Presets
Babel 中文文档
文档
学习 ES2015
社区
视频用户Stack OverflowSlack 频道Twitter
更多
博客GitHub 组织GitHub 仓库Website 仓库旧版网址 6.x旧版网址 5.x