visit
After careful consideration of these bullets, we are ready to start with an actual setup. It's pretty simple to do with the Angular CLI (ensure that you have installed @angular/cli
on your computer). Open a folder where you want to place your lib and run these commands:
ng new my-workspace --no-create-application
cd my-workspace
ng generate library lib-name
These commands will create an angular workspace and your lib in projects/lib
, which contains your library project's logic and data.
For the code in your library to be reusable, it's crucial to establish a public API. This "user layer" outlines the features accessible to your library's users. Users should be able to access public functions (such as NgModules, service providers, and general utility functions) via a single import path. The public-api.ts
file in your library folder is where your library's public API is maintained. When your library is incorporated into an application, everything exported from this file becomes publicly accessible.
For our demonstration purposes, this setup will be enough. Let's move further to publishing. But before this, you need to build the library. Update the version of the lib in projects/lib/package.json
and run the build
command in the root package.json
.
As an output, you will get the dist
folder with compiled code. Since you have built your code, you need to choose where you want to publish your library: either a public registry or a private one. Let's explore both options.
Public registry. By default, the npm public registry is //registry.npmjs.org. It means that all of your downloading packages are coming from this registry. Hence, when you want to publish something, you will publish it here. One thing that you need is to be logged in to the npm.
//{organization_link}/projects/{project_id}/packages/npm/
Once you find this link, you need to create a .npmrc
file, and put the necessary changes here. This file is used when you need to provide some settings to NPM. In our case, we will change the registry. Here is an example of how this file can be configured:
{lib-name}:registry=//{organization_link}/projects/{project_id}/packages/npm/
{organization_link}/projects/{project_id}/packages/npm/:_authToken={your_personal_access_token}
You should pay attention to the token part. You can generate it in your profile account, copy it, and paste it into the .npmrc
file. Since it's your personal token, it must not be published into a remote repository. So, make sure that .npmrc
is in .gitignore
If you don't want to use the .npmrc
approach, you can simply run this command: npm config set registry <registry url>
. This is a pretty straightforward approach and isn't suitable if you have more than one library with different registries. For better scalability and a better experience, I recommend the .npmrc
approach
Copy the .npmrc
file, and put it into the dist folder, on the same level as the package.json
file.
Run npm publish
Create a .npmrc.template
file (actually, it can be named whatever you want).
Put there all of your config from .npmrc
but replace the token section with ${CI_TOKEN}
. This is a placeholder where the actual token will be inserted in the CI environment. But remember, it can be various in different vendors and CI strategies. The name of this token is up to you;
publish:
image: node
stage: publish
dependencies:
- build
script:
- mv .npmrc.template .npmrc
- cp .npmrc dist/lib-name
- cd dist/lib-name
- npm publish
This step allows you to automate your publishing. Here, we rename our .npmrc.template
into a .npmrc
file, then copy it into the dist folder, navigate here, and run npm publish. Now, you don't have to do it manually each time when you want to update your library.