Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babelHelpers.asyncToGenerator is not a function #27

Closed
zhouzhongyuan opened this issue Mar 10, 2017 · 2 comments
Closed

babelHelpers.asyncToGenerator is not a function #27

zhouzhongyuan opened this issue Mar 10, 2017 · 2 comments

Comments

@zhouzhongyuan
Copy link
Owner

zhouzhongyuan commented Mar 10, 2017

错误描述

错误信息

babelHelpers.asyncToGenerator is not a function

错误截图
1

讨论 "babelHelpers.asyncToGenerator is not a function" on React-Native 0.16.0 and 0.17.0

错误背景

我的的.babelrc

{
  "presets": [
    "react-native", "react", "stage-0"
  ],
  "plugins": [
    "transform-async-to-generator"
  ]
}

错误原因

如同讨论中skevy所说

into your .babelrc and get the latest and greatest JS features. That's why we created the preset...it makes it easy for people to get started with a custom babelrc. Unfortunately, the stage-3 babel preset contains "async-to-generator", which transforms async function blah() into function* blah(). This is not necessary for react-native (and as you all have discovered, actually breaks), as it already includes transform-regenerator, which allows for consistent use of generators and async functions on the versions of JSC that react-native uses.
我的理解:

  • stage-3包含async-to-generator, stage-0包含stage-3.
  • plugins中包含async-to-generator. 重复一次。
  • react-native中也包含async-to-generator. 重复两次。

解决方法

方法一

{ "presets": [ "react-native" ] }

方法二(添加一些可能会用到的plugin)

{
  "presets": [ "react-native" ],
  "plugins": [
    "transform-do-expressions",
    "transform-function-bind",
    "transform-class-constructor-call",
    "transform-export-extensions",
    "syntax-trailing-function-commas",
    "transform-exponentiation-operator"
  ]
}

方法三(与方法二原理相同,只是更加简单)

npm install babel-preset-react-native-stage-0 --save
{ "presets": ["react-native-stage-0"] }

延伸

根据方法三,加深了对与Babel的理解。

@zhouzhongyuan
Copy link
Owner Author

React Native项目的根目录下有文件夹src,src中有.babelrc.
内容如下

{
  "presets": ["es2015","react","stage-0"]
}

src/.babelrc对React Native的babel转换有影响吗?

答: 有。因为根目录下的.babelrc不能有"react","stage-0"src下同样不能有。

解决办法

修改src/.babelrc

{
  "presets": ["es2015"]
}

@zhouzhongyuan
Copy link
Owner Author

以下都是猜想

猜想:

React Native项目,项目名称为“AwesomeProject”, 目录结构如下。

├── android
├── app.json
├── index.android.js
├── index.ios.js
├── ios
├── node_modules
├── package.json
├── src
│   ├── node_modules
│   ├── package.json
│   └── tutorial.md
└── tests
├── index.android.js
└── index.ios.js
如上述结构所示,AwesomeProject文件夹下有package.json,AwesomeProject/src文件夹下也有package.json

  1. 这两个package.json文件都会用到吗?
    答: 会。就近使用原则,逐级向上查找,找到了就停止,然后使用。例如Login使用到了react,逐级向上查找,在AwesomeProject/srcnode_modules找到了,就是用此react,而不是使用AwesomeProjectnode_modules里面的react
  2. 两个.babelrc都会使用到吗?
    答: 同第一个答案。

以上都是猜想

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant