Modify node manually_ Patch package in modules

Time:2021-11-29

background

First of all, the problem is that when using the pdfvuer third-party plug-in in the project, it is necessary to display the PDF that has been successfully sealed. At this time, it is found that the article can be well displayed, but the company’s signature can not be displayed. At this time, a solution has finally been found!! However, the modification code showing the signature depends on the package node_ Modules path. After all, the modification is in node_ The files under modules need to be modified manually every time NPM install. In an instant, I began to feel uncomfortable. It’s hard to feel uncomfortable, but I still have to find a way to solve it
Modify node manually_ Patch package in modules

There are two options in mind at first:

  1. Fork other people’s code to your own warehouse. After modification, install the plug-in from your own warehouse.
  2. Download other people’s code locally, put it in the SRC directory, and import it manually after modification. There are also many inconveniences. For example, it is cumbersome and time-consuming to package components alone, and the project will look bloated.

However, either of the above methods is too complex. I changed one or two lines of code in a file, but I have to copy the whole project so bloated. In the end, I may forget where to modify, and it is troublesome to update. We need to update the code manually every time, so we can’t update synchronously with the plug-in.
In this case, I turned around and started my quest for knowledge… Then I found a tall solution to usepatch-packageModify node_ Dependent packages in modules.

Solution

  1. Install patch package by command
    npm install patch-package --save-dev
  2. Modify the package.json file in the root directory of the project
    Add “postinstall”: “patch package” to the scripts in the package.json file
    Modify node manually_ Patch package in modules
  3. Modify node manually_ Modules depends on the source code in the package
    Modify node manually_ Patch package in modules
  4. Manually execute the command to create the NPX patch package package name patch file
    npx patch-package package-name
    Where package name refers to the name of the dependent package to be modified. After executing this command, a patches folder will be automatically created in the project root directory, and a patch file of package name + version.patch will appear in this folder. As shown in the figure:
    Modify node manually_ Patch package in modules
    For example, I want to modify the file under pdfjs dist, so the command I execute is: NPX patch package pdfjs dist
    Modify node manually_ Patch package in modules
  5. Test whether the patch package is valid
    Manually delete nodes in the project_ Modules file (forcibly delete the node_modules folder: rimraf node_modules), and re execute the NPM install command to install node_ Modules depends on the package. After the installation is successful, check your previous modifications   node_ Modules depends on the files in the package to check whether the modified code still exists. If the previously modified code still exists, the patch file has taken effect. If the previously modified code does not exist, the patch file has not taken effect.

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]