Skip to main content

常见问题

for...of 的输出为什么这么啰嗦又难看?​

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

为了符合规范,在出现错误时必须调用迭代器的 return 方法。一个替代方法是使用 Babel 7.13 引入的 假设,比如 ArrayLikeIsIterable 和 IterableIsArray,但请注意,如果你使用假设,有很多注意事项,而且这意味着你自愿选择不遵守规范。

🌐 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#5、google/traceur-compiler#1773 和 babel/babel#838。

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

为什么在箭头函数中 this 和 arguments 会被重新映射?​

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

箭头函数并不等同于普通函数。箭头函数中的 arguments 和 this 会引用它们的_外部函数_,例如:

🌐 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#842、babel/babel#814、babel/babel#733 和 babel/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,在 Node 中也不是 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.

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

救命?!我只想像5.x那样使用Babel!现在一切都太复杂了!​

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

我们听到了!Babel 6 需要做一点小配置才能开始使用。 我们认为这是最好的做法,并且已经添加了预设来简化这一过渡。

🌐 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 是一个开源项目,我们非常感谢任何形式的贡献。如果你能帮忙撰写文档,请通过提交 pull request 到 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?

请参阅 贡献。

🌐 See contributing.

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

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

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

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

tip

为了确保你使用的是最新的包版本,你可能需要再次删除 node_modules 和 npm install。