目 录CONTENT

文章目录

pnpm patch 修改第三方库代码

Hello!你好!我是村望~!
2023-02-07 / 0 评论 / 0 点赞 / 137 阅读 / 1,126 字
温馨提示:
我不想探寻任何东西的意义,我只享受当下思考的快乐~

pnpm patch 修改第三方库代码

首先我们先执行 pnpm patch <pkg> 去 patch 这个包!

  ╭─    ~/CunWangOwn/FE/vite-project  on   eslintPrettier !8 ?12 ────────────────────────────────────────────────────── ✔  took 14m 4s   at 14:57:39  ─╮
  ╰─ pnpm patch react-virtualized                                                                                                                                   
  You can now edit the following folder: /private/var/folders/wc/lq3tvptn19l7963dcqrznj7h0000gp/T/7ac53f1cb2a262be3e66cc3b63f21fc8/user

然后他就在这个地方/private/var/folders/wc/lq3tvptn19l7963dcqrznj7h0000gp/T/926607bb2ec5423174e11d839641f620/user给你生成了这个包的临时目录

然后你可以去这里修改任何你想去修改的代码!接着,我们进入第三方库的代码中去进行修改那个有问题的代码,先删掉无用的 import 语句,在随便加点其他的代码,再在命令行输入:

+ console.log(111111111111111111111111111);
- import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";

修改完并且保存后!还需要提交补丁! patch-commit

╭─    ~/CunWangOwn/FE/vite-project  on   eslintPrettier !8 ?13 ────────────────────────────────────────────────────────────────────── ✔  at 15:04:24  ─╮
╰─ pnpm patch-commit /private/var/folders/wc/lq3tvptn19l7963dcqrznj7h0000gp/T/7ac53f1cb2a262be3e66cc3b63f21fc8/user                                               ─╯
 WARN  deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility
 WARN  deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
 WARN  deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
 WARN  deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
 WARN  deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
Packages: -1
-
Progress: resolved 701, reused 680, downloaded 0, added 1, done

> vite-project@0.0.0 prepare /Users/codehope/CunWangOwn/FE/vite-project
> husky install

husky - Git hooks installed
 ERR_PNPM_PEER_DEP_ISSUES  Unmet peer dependencies

.
├─┬ @amatlash/vite-plugin-stylelint 1.2.0
│ ├── ✕ missing peer rollup@^2.60.0
│ └── ✕ unmet peer vite@^2.6.14: found 3.2.4
├─┬ vite-plugin-svgr 2.2.2
│ └─┬ @rollup/pluginutils 5.0.2
│   └── ✕ missing peer rollup@^1.20.0||^2.0.0||^3.0.0
├─┬ stylelint-config-standard-scss 6.1.0
│ ├── ✕ missing peer postcss@^8.3.3
│ └─┬ stylelint-config-recommended-scss 8.0.0
│   ├── ✕ missing peer postcss@^8.3.3
│   └─┬ postcss-scss 4.0.6
│     └── ✕ missing peer postcss@^8.4.19
└─┬ react-virtualized 9.22.3
  ├── ✕ unmet peer react@"^15.3.0 || ^16.0.0-alpha": found 18.2.0
  └── ✕ unmet peer react-dom@"^15.3.0 || ^16.0.0-alpha": found 18.2.0
Peer dependencies that should be installed:
  postcss@">=8.4.19 <9.0.0"  rollup@">=2.60.0 <3.0.0"   

hint: If you want peer dependencies to be automatically installed, add "auto-install-peers=true" to an .npmrc file at the root of your project.
hint: If you don't want pnpm to fail on peer dependency issues, add "strict-peer-dependencies=false" to an .npmrc file at the root of your project.

然后你再去运行一下你的项目!

image-20221211151644271

可以看到运行了我们自定义的代码!说明我们已经给第三方包成功的打了补丁代码!

然后有个疑问,那么是不是安装依赖后,每次都要去做一次patch commit呢?答案肯定是不是的!首先当你commit 补丁之后,当前项目目录下会出现一个patches的文件夹!

├── patches
│   └── react-virtualized@9.22.3.patch

下面每一个文件记录者你对所有包的补丁操作记录

// react-virtualized@9.22.3.patch
diff --git a/dist/es/WindowScroller/utils/onScroll.js b/dist/es/WindowScroller/utils/onScroll.js
index d00f0f18c6596e4e57f4f762f91fed4282610c91..dc7fc078a8b578c8fbef74eb54e7e0b13c7208f9 100644
--- a/dist/es/WindowScroller/utils/onScroll.js
+++ b/dist/es/WindowScroller/utils/onScroll.js
@@ -71,4 +71,5 @@ export function unregisterScrollListener(component, element) {
     }
   }
 }
-import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";
\ No newline at end of file
+console.log(111111111111111111111111111);
+// import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";
\ No newline at end of file

然后package.json 也会多出这样的配置:

  "pnpm": {
    "patchedDependencies": {
      "react-virtualized@9.22.3": "patches/react-virtualized@9.22.3.patch"
    }
  }

这样我们在每次执行 pnpm install 的时候,pnpm 会根据 patches 的补丁记录 自动帮我们把第三方打上补丁!

0

评论区