開發(fā)的時候,用到這么多的第三方包,難免有需要修改他們內(nèi)部源碼的時候,當(dāng)你修改之后,下次yarn、npm install之后,代碼又成為未修改之前的狀態(tài)了,每次都要修改一次,非常麻煩,介紹一個可以一勞永逸的方法。
社區(qū)提供了一個工具:patch-package,專門用來處理修改 node_modules 包源碼的問題。
用法
修改package.json,添加最后一行: "postinstall": "patch-package",postinstall 是 npm 的鉤子,會在依賴包被 install 之后被執(zhí)行。
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint .",
"postinstall": "patch-package"
},
之后安裝patch-package這個庫,
npm i patch-package -D
或者
yarn add patch-package postinstall-postinstall -D
注意二者是有區(qū)別的。
配置、安裝好了之后,我們就可以直接修改第三方包的內(nèi)容了,修改完之后,運行:
npx patch-package [package-name]
或者
yarn patch-package [package-name]
修改完之后,項目根目錄下就多出來一個patches文件夾

image.png
這樣之后,以后再npm install或者yarn,都會直接用patch之后的文件,真正的一勞永逸。