PNPM principle

Need some operating system knowledge to understand how PNPM works

1. The nature of the document

In an operating system, a file is actually a pointer, except instead of a memory address, it points to an external storage address (in this case, a hard disk, a USB drive, or even the network)

When we delete a file, we’re actually deleting Pointers, so no matter how big the file is, it’s very fast. For example, the files in our U disk and hard disk seem to have been deleted, but in fact, the data recovery company can recover, because the data still exists, as long as there are no other files stored after deleting files can be restored, so the real deletion of a file isCan save can delete

2. Copy of files

If you copy a file, you copy the file pointer to the content, and then create a new file pointing to the new content.

3, Hard link

The concept of hard link comes from the Unix operating system. It refers to copying one pointer to file A to another pointer to file B. File B is the hard link of file A.

With hard links, there is no additional disk footprint, and both files can find the same disk content.

There is no limit to the number of hard links, and multiple hard links can be generated for the same file.

The Windows Vista operating system supports the operation of creating a hard link. You can create a hard link using the following command in CMD.

Mklink /h Link name target fileCopy the code

Example: Create a hard connection

Create an article. TXT file in the temp folder

2. Next, I will create a hard link at the root of the Temp folder (below the PNPM package manager)

  • Press Window +R to bring up the window, run it as administrator, and type CMD, enter

  • Since the PNPM package manager folder is on drive F, first switch to drive F and enter the PNPM package manager address

  • Input: mklink /h link.txt temp\article.txtenter

  • At this point, you can see the newly created hard link link.text in the editorarticle.txt andlink.txtIt’s the same thing

  • When you modify link. TXT, the article. TXT will also change because they point to the same disk space

☛ :

  1. Hard links cannot be created for folders (directories) because folders (directories) do not contain file contents
  2. In Windows, you should not create hard links across drive letters

4, Symbol link

A symbolic link is also called A soft link. If A symbolic link B is created for A file or folder A, B points to A.

To create symbolic links, use the following command in CMD:

Mklink /d Link name Object file # /d indicates that a symbolic link is created for the directory. If no symbolic link is created for the fileCopy the code

Earlier Windows systems did not support symlinks, but provided a tool called Junction to achieve similar functionality.

5. The difference between symbolic and hard links

  1. Hard links can only link files, whereas symbolic links can link directories
  2. Hard links are only associated with the content of the file after the link is completed, and there is no relationship with the previous linked file. Symbolic links, however, are always associated with the previously linked file, and are not directly related to the file content.

6. Shortcuts

Shortcuts are similar to symbolic links, which have been supported in Windows since the early days. It is not just a pointer to another file or directory, but also contains all kinds of information: permissions, compatible boot mode, and other properties. Because shortcuts are unique to Windows systems, they are generally not used in cross-platform applications.

7. Node environment for hard links and symbolic links

Hard links:

A hard link is an actual file that Node does not make any special treatment of or discriminate against, and in fact node has no way of knowing if the file is a hard link at all

Symbolic links:

Since a symbolic link points to a different file or directory, node uses the original path when executing the JS file under the symbolic link. For example: I installed LOL on drive D and created an LOL shortcut on the desktop, which is equivalent to a symbolic link. Double-click the shortcut to run the game and run the game according to the original LOL path (drive D path).

8. Principle of PNPM

PNPM uses symbolic and hard links to build the node_modules directory

Here is an example to illustrate how it is built

Suppose two packages A and B, with a dependent on B:

Assuming that our project is PROJ and directly depends on A, PNPM will do the following during installation:

  1. Query dependencies through package.json to get the final packages to install: A and B
  2. Check whether a and B have been cached in the project proj root directory. If not, download them to the cache. If yes, go to the next step
  3. Create the node_modules directory in proj and structurally initialize the directory

4. Use a hard link from the corresponding package in the cache to place files in the corresponding package code directory5. Use symbolic links to place the direct dependencies of each package in its own directory

The purpose of this is to ensure that a’s code can read their direct dependencies during execution

  1. The new version of PNPM addresses the problem of some poorly written packages (read indirect dependencies) and adds all project indirect dependencies using symbolic links to.pnpm/node_modules. If B depends on C, A will use C directly, this non-standard usage is now supported by PNPM. But there may be no way to support wacky writing that uses absolute paths.

  2. Use symbolic links in the node_modules directory of the project to place direct dependencies

📌 finally

If there are any mistakes in the webpack compilation principle, please give me your comments and be sure to listen to your comments

Finally, you can also follow my public account: “Front-end Hunter”, or add my wechat (wKavin) to communicate privately.