简答:
run npx babel-upgrade
(然后您可以查看package.json 以查看更改的内容)
长答案:
对于 RN 0.57.x,在阅读了 babel 和 babel-upgrade 文档后,我意识到在我的项目的 devDependencies 中包含所有旧的 babel 依赖项就足够了:
"dependencies": {
.........
"react": "16.3.1",
"react-native": "0.55.4",
},
"devDependencies": {
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-latest": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.24.1",
"react-native": "0.55.4",
"babel-eslint": "^8.2.2",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-react-native": "^4.0.0",
"babel-preset-react-native-stage-0": "^1.0.1",
.....
},
1) 我使用了npx 和babel-upgrade(npx 已经包含在npm 版本>= 5.2.0 中)
如果您有较旧的npm 版本,则必须全局安装npx。
npx 让您无需在本地安装即可运行babel-upgrade。
2)我跑了npx babel-upgrade(没有--write option)看看升级将如何影响我的package.json deps)
3) 我跑了npx babel-upgrade --write
4) 我将 RN 版本设置为 0.57.1,并将 babel-preset 依赖项从 "babel-preset-react-native": "^5" 更改为 "metro-react-native-babel-preset": "^0.45.0",并将 .babelrc 配置更改为:
{
"presets": ["module:metro-react-native-babel-preset"]
}
如 RN 更改日志说明中所述。
现在package.json 看起来像这样:
"dependencies": {
"react": "16.5.0",
"react-native": "0.57.1",
.......
}
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-proposal-do-expressions": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-function-bind": "^7.0.0",
"@babel/plugin-proposal-function-sent": "^7.0.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
"@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/plugin-syntax-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-preset-react-native-stage-0": "^1.0.1",
.....
}
我不确定是否需要 gradle-upgrade 添加的所有新依赖项,但该项目在 android 和 ios 上都可以构建和运行。
如果您对此 babel 更新找到更好的解决方案或改进,请添加评论或添加新答案,我很乐意更新我的答案或接受新的更好的答案。
来源:
https://github.com/react-native-community/react-native-releases/blob/master/CHANGELOG.md#057
https://github.com/babel/babel-upgrade
对于 RN 0.58.6,我注意到我不需要这么多 babel deps。我注意到这使用 react-native init 命令创建了一个新项目。
我的 package.json 文件现在看起来像这样:
{
"dependencies": {
"react": "16.6.3",
"react-native": "0.58.6",
// ....
},
"devDependencies": {
"@babel/core": "^7.0.0-0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "24.1.0",
"jest": "24.1.0",
"metro-react-native-babel-preset": "0.53.0",
"react-test-renderer": "16.6.3",
// ....
},
"jest": {
"preset": "react-native",
// ...
}
}
注意:
对于 IOS:我能够在 IOS 中构建它,而 pod 文件中没有任何 react/react-native deps。我在 Linked Frameworks and Libraries 部分中添加/重新添加了那些