首页 > 解决方案 > npm install 后 React Native 自动更新第三方

问题描述

我是反应原生的新手。是否可以在 npm 安装后自动为第三方库设置正确的 sdk 版本或其他内容?因为有时当我搞砸项目并且代码无法回滚时,我会删除它并从 git 克隆项目。但这就是问题所在,因为android库sdk版本和ios库搜索路径不正确,需要我自己更正。

标签: react-nativereact-native-androidreact-native-ios

解决方案


添加postinstall到 package.json 脚本中。这将在安装运行后自动运行:

"postinstall": "./edit_modules.sh",

并在项目根目录中创建一个edit_modules.sh文件。像这样的东西:

#!/bin/bash
if [[ "$OSTYPE" == "darwin"* ]]; then
    SED_CMD="sed -i ''"
else
    SED_CMD="sed -i"
fi

$SED_CMD 's/<pattern to find>/<replace with>/' <path to file relative to root>

if/else forsed是因为它在 macOS 和 linux 上具有不同的签名。

以及我们项目中的示例 sed:

$SED_CMD 's/#import <fishhook\/fishhook.h>/#import "fishhook.h"/' ./node_modules/react-native/Libraries/WebSocket/RCTReconnectingWebSocket.m

推荐阅读