Sitemap
Pooja

Pooja

Angular Library

--

An Angular library is an Angular project that differs from an app in that it cannot run on its own. A library must be imported and used in an app. Angular added ability to create libraries with angular CLI.

Angular libraries are reusable code modules that can be shared across multiple Angular projects. They can contain components, services, directives, and other types of code, and can be easily imported and used in other projects as needed.

If you have developed functionality that is suitable for reuse, you can create your libraries. These libraries can be used locally in your workspace, or you can publish them as npm packages to share with other projects or other Angular developers

Why should we use library?

It will enhance code reusability. We can import libraries in multiple Apps.

It will avoid various bad practices or architectural mistakes that can make it difficult to decouple and reuse code in the future

Some of the examples are:
1. ReactiveFormsModule from the @angular/forms library is used to add reactive forms to an Angular app. We add this library package by using ng add @angular/forms and then importing the ReactiveFormsModule from the @angular/forms.

2. To turn an Angular App into a progressive application we add the service worker library. This turns our code into a Progressive Web App (PWA).

3. A very large and general-purpose library is the Angular Material. It is a library or a component that helps in constructing attractive, sophisticated, functional web app and reusable, and adaptable UI components.

Get Pooja’s stories in your inbox

Join Medium for free to get updates from this writer.

And many more.

The Advantages of Implementing Angular Libraries:

By implementing Angular libraries, we can achieve several advantages, including:

  • Improved maintainability
    Because we are no longer duplicating code across multiple projects, it is easier to maintain and update our code over time. We can make changes to a component or service in a single library and have those changes automatically propagate to all of the projects that used that library.
  • Faster development
    By reusing common code and functionality across multiple projects, we are able to develop new applications faster and with fewer errors. We can focus on building the application’s features, rather than spending time duplicating code and functionality.
  • Consistency
    By using the same set of libraries across multiple projects, we are able to ensure that our applications are consistent in terms of functionality and declaration. This helps us build a more cohesive set of applications.

How can we use library?

In this article, we will cover steps to create a library and how to use it in multiple Apps.

Creating a Custom Angular Library

  • Create a workspace project that includes configuration files and the necessary node_modules
    With the workspace set up, we can then generate a new library project using the following command
  • If we want our library components to be distinct then we can use the --prefix flag. If we don’t, Angular CLI will use lib by default
  • That will be created a Boilerplate code for library project. And having library module, services, components, etc. you can see below changes made in library project.
custom library project
  • Angular.json will get updated with projectType = “library”.
  • To make library code reusable you must define a public API for it. public-api.ts file contains exported files which is public and ready to reuse in different Angular Apps.
public-api.ts
  • Then make a functionality into your library. Your library is ready to use. Build your library with ng-build command

Rebuilding our library

After making the changes, we need to rebuild our library with:

Press enter or click to view image in full size

This creates an my-lib directory in our workspace’s dist directory.

Publishing your library:

Based on the Angular documentation, there are 2 types of distribution formats in publishing a library, Partial-Ivy and Full-Ivy. Partial-Ivy is the recommended format because it will be supported by different Angular versions across projects which use Ivy, while Full-Ivy will require the same Angular version between the library and the project that consumes it.

Initially, Angular CLI will use Partial-Ivy. We can see the configuration in tsconfig.lib.prod.json, the “compilationMode” value is “partial”. If we want to use Full-Ivy, we can change it to “compilationMode”: “full”.

We publish the library to make the library available on npm. For publishing the library, all we need to do is create a production build, and then run npm publish command from the library project’s dist directory.
The Syntax is as follows:

If you have not published anything before, you will need to create an npm account first and log in into your npm account and then publish your library.

Conclusion:

Implementing Angular libraries can offer numerous benefits for developers and organizations alike. By reusing code across multiple projects, we can reduce duplication and ensure consistency while also saving time and effort.

--

--