在现有的react native项目中,从babel 6升级到babel 7的步骤是什么?

这些是旧的依赖关系:

 "dependencies": {
    .........
    "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",
    "prop-types": "^15.5.10",
    "react": "16.3.1",
    "react-native": "0.55.4",
    "react-redux": "5.0.7",
    "redux": "^4.0.0",
    "redux-actions": "^2.6.1",
    "redux-mock-store": "^1.5.1",
    "redux-persist": "^5.10.0",
    "redux-thunk": "^2.1.0",
  },
  "devDependencies": {
    "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",
    "eslint": "^4.18.1",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-plugin-flowtype": "^2.46.1",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "6.1.1",
    "eslint-plugin-react": "^7.4.0",
    "gulp": "^3.9.0",
    "gulp-eslint": "4.0.2",
    "gulp-mocha": "6.0.0",
    "jest": "^23.5.0",
    .....
  },

要进行此更新,您必须遵循哪些步骤?

我(在阅读了babel文档之后)不太清楚我应该做什么来进行升级,运行命令,应该在依赖项中添加什么,在devdependences中添加什么.

我也不太清楚babel 6和babel 7在react-native 项目的JS代码中有什么不同.

请不要只回复babel doc或react native 0.57更改日志(log)的链接.

我需要至少一些基本的步骤来进行升级,并提供一个包的示例.基于巴别塔7的RN项目的json.

推荐答案

Short answer:

run npx babel-upgrade

(然后你可以查看package.json中的内容,以判断发生了什么变化)

Long answer:

对于RN 0.57.x在阅读了babel和babel升级文档后,我意识到在我的项目的devdependences中包含所有旧的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) I used npx and babel-upgrade (npx is already included in npm versions >= 5.2.0) If you have older npm versions you have to install npx globally.

npx允许您运行babel-upgrade,而无需在本地安装它.

2)我运行了npx babel-upgrade(没有--write option)以查看升级将如何影响我的包.json(deps)

3)我跑了npx babel-upgrade --write

4) 我将RN版本设置为0.57.1,并将babel预设依赖项从"babel-preset-react-native": "^5"更改为"metro-react-native-babel-preset": "^0.45.0",将.babelrc配置更改为:

{
    "presets": ["module:metro-react-native-babel-preset"]
}

如RN变更日志(log)说明中所述.

现在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上都可以构建和运行.

如果你发现了一个更好的解决方案或巴别塔更新的改进,请添加 comments 或添加一个新的答案,我将很高兴更新我的答案或接受一个新的更好的.

资料来源:

https://github.com/react-native-community/react-native-releases/blob/master/CHANGELOG.md#057

https://github.com/babel/babel-upgrade

对于RN 0.58.6个我注意到我不需要那么多巴别塔德普.我注意到这是在用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", 
   // ...
  }

}

NOTE:

React-native相关问答推荐

React Native在同一页面上使用多个不同的导航器

嵌套的FlatList内的Incorrect Padding

react-native useEffect / useFocusEffect / useCallback 没有正确更新

React Native 应用程序没有在之前的工作代码中执行任何操作就无法运行

在 React Native 中从 Modal 打开 Modal

在选项卡更改时react-navigation

create-react-native-app myproject和react-native init myproject之间的真正区别是什么

TouchableOpacity 作为 ListView 中的 Item 仅在 TextInput 失go 焦点后做出响应

自从升级到 Xcode 10.2 我不能再通过 cli 运行 react-native run-ios

如何使用 React Navigation 使 TabNavigator 按钮推送模态屏幕

react-native 从右到左

React Native 有全局作用scope域吗?如果没有,我该如何模仿它?

以编程方式在 React Native 中添加组件

区别需要MainQueueSetup 和dispatch_get_main_queue?

尽管 TypeScript 编译器错误,为什么我的 React Native 应用程序构建成功?

使用 React Native 运行多个 iOS 模拟器?

React Native 模块中的依赖注入

react-native构建错误:package android.support.annotation does not exist

在 React Native 中检测向左滑动

React Native + Redux 基本认证