w3resource

Pruning an Offline Mirror in Yarn for optimized builds


In the previous tutorial we showed you how to configure an offline mirror. Having configured the offline mirror, you can now set up a build system without connecting to network, it also automatically adds new package tarballs to the mirror. However, yarn will not remove tarballs that are no longer referenced in the yarn.lock file. For instance, running yarn remove on a dependency will remove the dependency but the tarball will remain in the mirror, even when you don't have any dependency that have it as a sub-dependency.

Although this behavior is desirable in a setting where you have many projects that share the same mirror, but this is not the case most times, usually you will want to remove all unnecessary tarballs.

If you want to enable automatic pruning, you will have to set yarn-offline-mirror-pruning to true in your .yarnrc:

yarn config set yarn-offline-mirror-pruning true

Then yarn watches your dependencies and removes tarballs as appropriate. The result of this is your package.json, node_modules, yarn.lock, and the offline mirror will all remain perfectly in sync whenever you change your projects dependencies.

A few tips and tricks

When working with the offline mirror, here are some tricks that can become handy.

Updating your package

You can follow these steps to ensure that you have a clean cache module:

  • You should remove the package first. Then, you ensure that you have "yarn-offline-mirror-pruning" set to true in your .yarnrc file
  • Next you will have to clear the yarn cache using "yarn cache clean" before adding the updated version of the package
  • Add your package

The "yarn-offline-mirror-pruning" helps clean up any unlinked dependencies. Whenever you add the updated package, yarn will check the yarn cache first and then pull any missing dependencies from there. Doing this, prevents yarn from adding new tarball back with the updated package. You will want to ensure that the yarn cache is all clean before you do any adding for cache module.

Previous: Configuring an Offline Mirror with Yarn for reliable builds.
Next: Creating your first Yarn Package with Git integration.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/yarn/pruning-an-offline-mirror.php