Sub topic 16
Multi-project workspaces and libraries
This chapter assumes you can read angular.json (the workspace configuration file) and use ng generate. A single-application workspace has one entry under projects. A multi-project workspace has several entries in the same repository. They share one node_modules folder, one package.json, and one angular.json.
When to use more than one project
Common reasons to add projects:
- You want a reusable library of components or services that several applications consume.
- You have two or more applications, such as a customer site and an admin site, that share code.
- You want to develop and test a library next to an application that uses it.
Adding an application or a library
Run these commands from the workspace root:
ng generate application admin
ng generate library ui-kitThe CLI creates a projects folder and places each new project in its own subfolder. It also adds an entry for each project to angular.json.
my-app/
src/ original application
projects/
admin/src/ second application
ui-kit/src/ library source
lib/
public-api.ts
ui-kit/ng-package.json
angular.json
tsconfig.jsonOnce the workspace has a projects folder, you can generate the main application there as well. Whichever layout you choose, each project has its own root and sourceRoot values in angular.json.
Targeting a project
Most commands act on a single project. Name the project with a flag or an argument:
ng serve admin
ng build ui-kit
ng generate component button --project=ui-kitIf you leave the name out, the CLI uses the default project or asks you to choose one. Be explicit in scripts so they keep working when you add a project.
How a library differs from an application
A library has no main.ts and no bootstrapping. Its build target uses a different builder, @angular/build:ng-packagr, which compiles the code into a package that other projects can install. The file public-api.ts lists everything the library exports:
export * from './lib/button/button';
export * from './lib/ui-kit.service';Anything not exported from public-api.ts stays private to the library. Your component and service file names may differ, depending on the CLI version.
Using the library in an application
The CLI adds a path mapping to the root tsconfig.json, so applications import the library by name:
import { ButtonComponent } from 'ui-kit';Applications import the library's compiled output from dist/ui-kit. Build the library first with ng build ui-kit, or the import will not resolve. After you change library code, rebuild it. Run ng build ui-kit --watch to rebuild automatically.
Trade-offs
A shared workspace makes it easy to change a library and its consumers in one commit, and all projects use the same dependency versions. The cost is coupling. A single ng update affects every project, and a large workspace builds more slowly. If teams release on separate schedules, separate repositories with a published package may suit them better.
Exercise
You generated a library named ui-kit and your application imports from 'ui-kit', but the compiler reports that it cannot find the module. What is the most likely cause, and which command fixes it?
Show answer
The library has not been built, so dist/ui-kit does not exist. Run ng build ui-kit, or ng build ui-kit --watch while you develop.