当前位置:网站首页>Three simple tips for accelerating yarn install

Three simple tips for accelerating yarn install

2022-06-23 15:30:00 Code Taoist

Improving the productivity and quality of life of developers is my passion . Accelerating the use of code is not only interesting , It is also essential to build products quickly . Happier and faster developers are equal to happier customers , Because they can get features and bug fixes faster !

Based on npm The ecosystem and its myriad packages , Installation time of the dependency , Especially in large projects , It may be too long . Here are three simple tips , Can help you save oneortwo minutes , Sometimes it can even reduce yarn install Half the time

bloated yarn.lock problem

If your project is in yarn Up operation , Then some of your dependencies are likely to be copied , Even if they satisfy semver Conditions . Even though yarn promise Data deduplication is not required , But it's not true .

Imagine this : You are putting @awesome/tools Add libraries to dependencies , It also depends on [email protected]^1.0.0 library , This is the latest version of it . install @awesome/tools after , You will be in yarn.lock In the file :

"@awesome/[email protected]^1.2.3":
    version "1.0.0"
    dependencies:
        "utils" "^1.0.0"
"[email protected]^1.0.0":
    version "1.0.0"

After a few months , You want to add another library that depends on these utilities , for instance @simple/button.Utils The library also released some bug fixes and features , The latest version of it is 1.5.1, also @simple/button Depend on it . If you just run yarn add @simple/button, Then you will be in yarn.lock see :

"@awesome/[email protected]^1.2.3":
    version "1.0.0"
    dependencies:
        "utils" "^1.0.0"
"@simple/[email protected]":
    version "1.0.0"
    dependencies:
        "utils" "^1.5.1"
"[email protected]^1.0.0":
    version "1.0.0"
"[email protected]^1.5.1":
    version "1.5.1"

Even if 1.5.1 and 1.0.0 The version is semver Compatible ,yarn Nor will they be merged into one as you would expect , You'll end up in repo Get two identical versions of utils.

If you have several different libraries that depend on 1.0.0, The others depend on 1.5.1,yarn One of the versions will be promoted to the root of the folder node_modules Catalog , But the other will have nowhere to go ( Only one version can be located in the root directory ) , And they will be installed as copies in node_modules Use their libraries in the folder . You will eventually get this folder structure :

/project 
   /node_modules 
      / [email protected]
       /@simple/button 
         /node_modules/ [email protected]
       /@simple/form 
         /node_modules/ [email protected]
       /@simple/select 
         /node_modules/ [email protected]

Although it seems that you only have 2 Versions utils library , But actually , It can be an infinite number of copies that exist in your project , All of this needs to be copied to their location , All this will steal your yarn install Time for .

Solution

duplicate removal ! Or manually make sure that all semver Compatible libraries are resolved to one version , Or use yarn-deduplicate And so on . This is what you want yarn.lock

"@awesome/[email protected]^1.2.3":
    version "1.0.0"
    dependencies:
        "utils" "^1.0.0"
"@simple/[email protected]":
    version "1.0.0"
    dependencies:
        "utils" "^1.5.1"
"[email protected]^1.0.0", "[email protected]^1.5.1":
    version "1.5.1"

For example , In a project , I am completely deduplicating all dependencies , take yarn install The time from 3 Minutes to 1.5 minute .

monorepo and workspaces The dark side of

If you use monorepo and yarn work area ( or Lerna, It uses the following workspace ) To manage your package , You can easily find that your dependencies in the local package are out of sync with each other . Similar to the above , If one of your packages depends on [email protected]^1.0.0, Other packages depend on [email protected]^1.5.1, And these packages are not deduplicated in the root directory ,yarn Multiple copies of versions that are not promoted to the root directory will be created , And install them in your workspace node_modules In the folder . under these circumstances , You may not only end up in the root node_modules Multiple copies of the same dependency in , And there are also scattered in repo Copy of the same dependency in :

/project 
   /node_modules 
      / [email protected]
    / packages
       /one 
         /node_modules/ [email protected]
       /two 
         /node_modules/ [email protected]

In a very large project I'm working on , It has 600 Multiple workspaces , If we allow only 10% Dependencies of are out of sync ,yarn install It will take an hour , instead of 5 minute .

Solution

If you move to yarn2 Or anything based on pnpm It's not a good choice ( They solved this particular problem ), Then the only other option is to keep the dependencies and roots in the workspace package.json Strict synchronization .

Nuke node_modules

For some reason , When updated yarn When something strange happens after a dependency in , The first solution everyone recommends is nuke nodemodules Folder , And execute a new yarn install. This is usually the last suggestion , Because it miraculously fixes 90% The strange situation of . After a while , You will form a habit , Each time you check the main branch and start developing new functionality, you execute `rm -rf nodemodules && yarn install`.

If only I could speed up a little more , At least speed up 50%……

Solution

At your root node_modules There is a secret file in the folder .yarn-integrity,yarn Write down all the information it needs to know about your repository and its installed dependencies . If the contents of this file are similar to repo The situation in does not match ,yarn It will be updated and the installed packages will be refreshed . If the file is missing , It will be based on yarn.lock Generate it , And update the node_modules The content in . So if most of the content is already correct , Then it just replaces the incorrect content .

We didn't execute rm -rf **node_modules** && yarn install, But to perform rm -rf **node_modules/.yarn-integrity** && yarn install, It can be effectively reduced by half for yarn install Time for .

original text :https://adevnadia.medium.com/three-simple-tricks-to-speed-up-yarn-install-9cae57d159db

原网站

版权声明
本文为[Code Taoist]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231336145221.html