1

如何优雅的修改node_module三方包

 2 years ago
source link: https://www.fly63.com/article/detial/11313
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
更新日期: 2022-04-02阅读量: 151标签: npm分享

扫一扫分享

相信有过一些前端开发经验的同学都有遇到过使用的npm包有bug,或者npm包只要再修改一点点就能满足自己的需求这样尴尬的情况。如果给包作者提需求,作者一般也不会马上给你修改,这时候就需要使用各种修改npm包源码的骚操作了。

修改别人的源码往往有这几个方式:

  1. 直接在项目的node_modules下找到插件的源码直接修改;

  • 优点:简单直接、快速见效
  • 缺点:不能持久化,一旦重新安装就失效;不方便团队成员使用修改后的代码

去github上fork代码到自己的仓库进行修改,并将自己修改过后的代码发布到npm上使用;

  • 优点:团队成员都可以使用到这份修改的代码
  • 缺点:麻烦、十分麻烦

显而易见,上面这两种方法既不优雅,也不可靠。作为程序员的我们岂能被这事儿给难住,开源社区早已给我们准备好了解决方案:patch-package

patch-package

1、简介:有个功能需要修改node_modules里面的代码,就发现patch-package可以立即修改并保留对 npm 依赖项的修改。

2、安装patch-package

npm i patch-package --save-dev

3、修改完依赖后,运行patch-package创建patch文件

npx patch-package 依赖名称
例子: npx patch-package element-ui

4、修改package.json的内容,在scripts中加入"postinstall": "patch-package",这个是为了npm install的时候自动为依赖包打上我们改过的依赖

"scripts": {
  ...
  "postinstall": "patch-package"
}

链接: https://www.fly63.com/article/detial/11313


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK