babel vs babel-core vs babel-runtime

babel vs babel-core vs babel-runtime

我的node webpack项目使用了三个babel库。它们之间有什么区别以及如何使用它们?

"dependencies": {
  "babel-runtime": "^5.8.24"
}

"dev-dependencies": {
  "babel": "^5.8.23",
  "babel-core": "^5.8.23"
}

babel-core就是API。对于 v5,babel 包是 CLI 并且依赖于 babel-core。对于 v6,babel-cli 包是 CLI(尽管 CLI bin 命令仍然是 babel)并且 babel 包不执行任何操作。 babel-runtime 我猜只是运行时(polyfill 和助手)支持已经转换的代码。

TL;DR 这里要比较的是:

  1. babel(用于 5.x.x)与 babel-cli+babel-core(为 6.x.x 选择一个)
  2. babel-polyfill(用于非库)vs babel-runtime+babel-plugin-transform-runtime(用于库)

来自https://babeljs.io/blog/2015/10/31/setting-up-babel-6

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.

babel-runtime 只允许不污染全局 space 的 polyfill,不像 babel-polyfill 会污染你的全局 space。来自 http://babeljs.io/docs/plugins/transform-runtime/:

[babel-runtime] automatically polyfills your code without polluting globals. (This plugin is recommended in a library/tool)

如果你使用 babel-runtime,你还应该

npm install --save-dev babel-plugin-transform-runtime

In most cases, you should install babel-plugin-transform-runtime as a development dependency (with --save-dev) and babel-runtime as a production dependency (with --save).

The transformation plugin is typically used only in development, but the runtime itself will be depended on by your deployed/published code.

此外,babel-runtime+babel-plugin-transform-runtime 和 babel-polyfill 通常是互斥的——这意味着您应该只使用其中之一。来自这里的评论 http://jamesknelson.com/the-six-things-you-need-to-know-about-babel-6/:

You should be using either babel-polyfill or babel-runtime. They are mutually exclusive—unless of course you know what you are doing. But they are essentially the same thing. These are just helpers. babel-polyfill achieves the same goal by mutating globals whereas babel-runtime does so modularly. Unless you are developing a library, I’d recommend you use the polyfill.

The Six Things You Need To Know About Babel 6解释的很好,引用

The babel npm package no longer exists. Instead, Babel has been split into multiple packages:

babel-cli, which contains the babel command line interface babel-core, which contains the Node API and require hook babel-polyfill, which when required, sets you up with a full ES2015-ish environment To avoid accidental conflicts, make sure to remove any previous Babel packages like babel, babel-core, etc. from your package.json, and then npm uninstall them.