Discover how to tackle the common issue of broken publishing when using Yarn, Lerna, and Angular libraries in a monorepo setup.
---
This video is based on the question https://stackoverflow.com/q/64056078/ asked by the user 'Charlie' ( https://stackoverflow.com/u/409053/ ) and on the answer https://stackoverflow.com/a/64147989/ provided by the user 'Amit' ( https://stackoverflow.com/u/1509905/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Yarn + Lerna + Angular Libs = broken publishing?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Solving Yarn, Lerna, and Angular Library Publishing Issues in Monorepos
When working with a monorepo that includes multiple Angular libraries managed by both Yarn and Lerna, you may encounter a frustrating issue during the publishing process. This concern typically arises when specific libraries are modified, making it challenging to properly build and publish them. In this guide, we’ll dissect this problem and provide effective solutions to ensure smooth publishing.
Understanding the Problem
In a typical monorepo setup using Yarn workspaces and Lerna, your package.json scripts are configured to build the libraries prior to publishing using the prepublishOnly command. However, complications surface when:
Only a Single Library is Modified - If only one library is changed, Lerna identifies this change and attempts to run the prepublishOnly command to publish. Unfortunately, at that point, the other libraries in node_modules may not have been built yet because they are symlinked to their source directory. This results in an attempt to publish incomplete packages, leading to failure.
All Libraries Must Be Built - To address this, you need to ensure that all libraries are built upon any publish attempt, regardless of whether they have changes or not.
Now, let's explore practical solutions for these issues.
Solution 1: Always Build All Packages During Publish
A straightforward workaround is to modify your root package.json to create a new script that builds all libraries before publishing them. Here’s how to set it up:
Step-by-Step Setup
In your root package.json: Add the following script:
[[See Video to Reveal this Text or Code Snippet]]
In each library's package.json: Ensure you have a build script configured:
[[See Video to Reveal this Text or Code Snippet]]
How it Works
Running yarn publish-ci from the root directory executes the build script for all libraries followed by the lerna publish -- contents dist command. This way, all packages are built and are ready for a successful publish.
Optionally, you can use the --from-package flag in lerna publish if you want to publish only the modified package while still ensuring they are built correctly.
Solution 2: Use Packages from NPM Registry During Build Process
If you only want to build and publish the modified packages without building every package, you can set up a custom script to handle this. Here’s how:
Custom Publishing Script
Modify your root package.json:
[[See Video to Reveal this Text or Code Snippet]]
Create a custom.publish.js file in the root directory:
[[See Video to Reveal this Text or Code Snippet]]
Explanation of the Script
This script first checks which packages have changed using yarn lerna changed.
It then iteratively publishes each changed package directly via npm publish, allowing you to work with the versions that exist in the NPM registry for the build process.
Conclusion
Dealing with publishing issues in a monorepo configuration is indeed challenging, but with the solutions provided above, you can effectively manage and publish Angular libraries using Yarn and Lerna. Whether you choose to build all packages every time or selectively publish modified ones, these strategies will help ensure smoother workflows and fewer headaches in your development process.
Now you can take control of your publishing process, making it more reliable and efficient! If you have any further questions or need examples, feel free to ask. Happy coding!
Информация по комментариям в разработке