Skip to main content

常见问题

为什么 for...of 的输出如此冗长和难看?

¥Why is the output of for...of so verbose and ugly?

为了符合规范,迭代器的返回方法必须在错误时调用。另一种方法是使用 Babel 7.13 中引入的 assumptions,例如 ArrayLikeIsIterableIterableIsArray,但请注意,如果你使用假设并且你愿意选择不遵守规范,则需要注意许多警告。

¥In order to comply with the specification, the iterator's return method must be called on errors. An alternative is to use assumptions introduced in Babel 7.13, such as ArrayLikeIsIterable and IterableIsArray, but please note that there are many caveats to be aware of if you use assumptions and that you're willingly choosing not to comply with the spec.

请参阅 babel/rfcs#5google/traceur-compiler#1773babel/babel#838 了解更多信息。

¥Please see babel/rfcs#5, google/traceur-compiler#1773 and babel/babel#838 for more information.

为什么 thisarguments 在箭头函数中被重新映射?

¥Why are this and arguments being remapped in arrow functions?

箭头函数与普通函数不同义。argumentsthis 内部箭头函数引用它们的外部函数,例如:

¥Arrow functions are not synonymous with normal functions. arguments and this inside arrow functions reference their outer function for example:

JavaScript
const user = {
firstName: "Sebastian",
lastName: "McKenzie",
getFullName: () => {
// whoops! `this` doesn't actually reference `user` here
return this.firstName + " " + this.lastName;
},
// use the method shorthand in objects
getFullName2() {
return this.firstName + " " + this.lastName;
},
};

请参阅 babel/babel#842babel/babel#814babel/babel#733babel/babel#730 了解更多信息。

¥Please see babel/babel#842, babel/babel#814, babel/babel#733 and babel/babel#730 for more information.

为什么 this 被重新映射到 undefined

¥Why is this being remapped to undefined?

Babel 假设所有输入代码都是一个 ES2015 模块。ES2015 模块是隐式严格模式,因此这意味着顶层 this 不是浏览器中的 window 也不是节点中的 exports

¥Babel assumes that all input code is an ES2015 module. ES2015 modules are implicitly strict mode so this means that top-level this is not window in the browser nor is it exports in node.

如果你不想要这种行为,那么你可以选择禁用 plugin-transform-modules-commonjs 中的 strict

¥If you don't want this behaviour then you have the option of disabling strict in the plugin-transform-modules-commonjs.

请注意:如果你这样做,你愿意偏离规范,这可能会导致未来的互操作问题。

¥PLEASE NOTE: If you do this you're willingly deviating from the spec and this may cause future interop issues.

帮助?!我只想像在 5.x 中一样使用 Babel!现在一切都太复杂了!

¥Help?! I just want to use Babel like it was in 5.x! Everything is too complicated now!

我们听到你的声音!Babel 6 需要一点点配置才能运行。我们认为这是最好的 并添加了 presets 来缓解这种过渡。

¥We hear you! Babel 6 requires a tiny bit of configuration in order to get going. We think this is for the best and have added presets to ease this transition.

从 Babel 5.x 升级到 Babel 6

¥Upgrading from Babel 5.x to Babel 6

Babel 6 的核心是 插件。你需要哪些插件完全取决于你的特定配置,但只需添加以下 配置文件 即可获得与 Babel 5 中相同的所有转换:

¥At the heart of Babel 6 are plugins. What plugins you need completely depends on your specific configuration but just add the following config file to get all the same transforms that were in Babel 5:

babel.config.json
{
"presets": ["env", "react", "stage-2"]
}
npm install babel-preset-env babel-preset-react babel-preset-stage-2 --save-dev

另请查看我们的 设置 Babel 6 博客文章。

¥Also check out our Setting up Babel 6 blog post.

所有文档都去哪儿了?!

¥Where did all the docs go?!

Babel 6 删除了很多支持 插件 的选项,因此很多文档不再适用。

¥Babel 6 removes a lot of the options in favor of plugins so a lot of the docs are no longer applicable.

对于每个删除的选项,都应该有一个插件。我们可能遗漏了什么,如果你认为是这种情况,请 打开一个问题

¥For every removed option there should be a plugin for it. It's possible we may have missed something, if you think this is the case, please open an issue!

Babel 是一个开源项目,我们感谢我们能得到的任何和所有贡献。如果可以的话,请通过向 babel.github.io 存储库提交拉取请求来帮助提供文档。

¥Babel is an open source project and we appreciate any and all contributions we can get. Please help out with documentation if you can by submitting a pull request to the babel.github.io repo.

我如何从源代码构建 babel?

¥How do I build babel from source?

构建说明

¥See build instructions.

我如何为 Babel 做贡献?

¥How do I contribute to Babel?

contributing

¥See contributing.

为什么我会收到语法错误/意外令牌?

¥Why am I getting a Syntax Error/Unexpected Token?

你很可能没有包含支持该功能的插件/预设。(也可能是解析器中的错误,或者实际上是语法错误)。

¥It's most likely the case that you didn't include a plugin/preset that supports that feature. (It's also possible it's a bug in the parser, or that it actually is a syntax error).

为什么某个 babel-x 包没有更新?

¥Why isn't a certain babel-x package updated?

我们目前使用 Lerna 的固定版本控制 系统。

¥We currently use Lerna's fixed versioning system.

我们为所有软件包提供全局版本。当我们发布时,唯一得到更新的包是实际发生变化的包(我们在那个文件夹上做了一个 git diff)。

¥We have a global version for all packages. When we do a release, the only packages that get updated are the packages that actually had changes (we do a git diff on that folder).

如果我们只将 babel-plugin-transform-exponentiation-operator 更新到 6.x.x,目前我们不会为所有包发布新版本,因为其他依赖正在使用 ^

¥If we only update babel-plugin-transform-exponentiation-operator to 6.x.x, currently we don't publish a new version for all packages since the other dependencies are using ^.

例如,Babel v6.6.0 发布 并不意味着所有包现在都是 6.6.0。

¥For example, the Babel v6.6.0 release doesn't mean all packages are now 6.6.0.

提示

为确保你使用的是最新的软件包版本,你可能需要再次删除 node_modulesnpm install

¥To make sure you are using the latest package versions, you may need to remove node_modules and npm install again.