background

During development, there are occasional issues that need to be addressed by modifying the tripartite library.

In general, we will adopt the following ways to solve this situation:

1. Fork and modify package.json to point to the fork address.

2. Write a shell script and perform the script replacement after the package is pulled.

The first method is too heavy and cumbersome to update and merge the main library, requiring manual merging.

The second method involves writing shell scripts that have a low input/output ratio and are often forgotten to execute.

However, temporary changes to the NPM package are no longer an issue since the following method was discovered

usepatch-packageandpostinstall-postinstall

Integration of 1.

yarn add patch-package postinstall-postinstall -D
Copy the code

2. Download package

yarn
Copy the code

3. Modify the file

Find the file in node_modules that you want to modify and modify it directly

4. Create a pudding

After modifying the file, run the following command: package-name indicates the modified package name

npx patch-package package-name
Copy the code

After the execution is complete, a patches directory is generated under the root directory of the project, in which the package-name+version.patch file is generated

This file describes the modifications

5. Automatically perform the replacement each time you pull the package

Add scripts to package.json

"scripts": {
  "postinstall": "patch-package"
}
Copy the code

After yarn or NPM install is complete, the NPM package content is automatically modified according to the patch file.

Note, however, that the Lock file needs to be versioned, because the pudding specifies the version