Flo*_*bre 12 babeljs 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",
.....
},
Run Code Online (Sandbox Code Playgroud)
您需要执行哪些步骤才能进行此更新?新的依赖项应该如何?
对于我(在阅读了babel doc之后),我应该做些什么才能进行此升级,运行命令以及应该在依赖项中添加什么以及devDependencies中的内容.
另外,对于我来说,babel 6和babel 7之间的区别在于反应原生项目中的JS代码发生了什么.
请不要只回复babel doc或react-native 0.57更改日志的链接.
我需要至少一些基本步骤来进行此升级以及基于babel 7的RN项目的package.json示例.
简短答案:
run npx babel-upgrade
(然后您可以package.json
查看一下更改内容)
长答案:
在阅读了babel和babel升级文档之后,对于RN 0.57.x,我意识到将所有旧的babel依赖项包含在我的项目的devDependencies中就足够了:
"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",
.....
},
Run Code Online (Sandbox Code Playgroud)
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"]
}
Run Code Online (Sandbox Code Playgroud)
如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",
.....
}
Run Code Online (Sandbox Code Playgroud)
我不确定是否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",
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
对于IOS:我能够在IOS中构建它,而没有react/react-native
Pod文件中的任何内容。我在链接框架和库部分中添加/重新添加了这些内容
babel-upgrade
。您可以尝试使用babel-upgrade
自动升级 Babel 依赖项。
即使无需全局安装,它也非常易于使用。
我建议有一个干净的工作目录(没有未暂存的更改)并只需运行以下命令:
npx babel-upgrade --write
Run Code Online (Sandbox Code Playgroud)
这将使用正确的 Babel 版本和软件包更新您package.json
和您的文件。.babelrc
该--write
命令只是告诉工具将更改保存到磁盘。这就是为什么我建议有一个干净的工作目录,以便您可以使用git diff
. 如果省略,--write
它将仅在控制台中显示所需的更改。
您可以在https://github.com/babel/babel-upgrade查看更多信息。
归档时间: |
|
查看次数: |
4527 次 |
最近记录: |