Goal: create a reusable Angular library
The idea is to create Angular workspace which consists of a library and test application.
The library is code which could be published to NPM, while test application is an environment with documentation and example usage of the library.
Test subjects:
- Library name: turbo-lib
- Test application name: turbo-tester
This repository contains sample project in the turbo-lib
directory which could be used as a reference.
- Make sure that Angular CLI will use the latest stable version of Angular
- Create Angular workspace with
ng new turbo-lib --create-application=false
- Create library project
cd turbo-lib
ng generate library turbo-lib --prefix=turbo
- Create test application project
ng generate application turbo-tester
- End-to-end tests for test application are located in the
projects/turbo-tester-e2e/
folder
Keep in mind that configuration for both projects is located in the angular.json
file.
- Build library with
ng build turbo-lib
- Build test application with
ng build turbo-tester --prod
- Serve application with
ng serve turbo-tester
- Run library tests with
ng test turbo-lib
- Run test application tests with
ng test turbo-tester
The goal is to achieve following:
/* Import whole library */
import * as TurboLib from "turbo-lib"
/* Import specific components and services from library */
import { TurboComponent, TurboService } from "turbo-lib"
When using published library, i.e. on NPM or Git repository, one can easily use aforementioned imports. On the other hand, while developing library, import paths should be added to tsconfig.json
file.
That is, if we want to use turbo-lib
inside turbo-tester
application, tsconfig.json
file must have the following lines:
"paths": {
"turbo-lib": [
"dist/turbo-lib"
]
}
Library source files are located in the turbo-lib/projects/turbo-lib/src
directory.
Keep in mind that package.json
in that directory is going to be published on NPM.
On the other hand, public_api.ts
is the entry file. It specifies what parts of library are visible externally.
This section describes how to modify, i.e. create, custom library component, and how to use it in the test application.
Due to specific requirements, this example modifies default Angular library which was generated by CLI tool.
- Set up a listener which will build new version of library on every change
ng build turbo-lib --watch
- Rename
projects/turbo-lib/src/lib/turbo-lib.component.ts
- Rename file to
heading.component.ts
- Rename file to
- Modify
heading.component.ts
- Modify selector to
turbo-heading
- Change template to something more appropriate, i.e.
`<h1>TURBO HEADING</h1>`
- Rename class name to
HeadingComponent
- Modify selector to
- Rename
projects/turbo-lib/src/lib/turbo-lib.component.spec.ts
- Rename file to
heading.component.spec.ts
- Replace all
TurboLibComponent
occurences withHeadingComponent
- Rename file to
- Modify
heading.component.spec.ts
- Modify import to import
HeadingComponent
fromheading.component
- Modify import to import
- Modify
projects/turbo-lib/src/lib/turbo-lib.module.ts
- Modify component import to
import { HeadingComponent } from './heading.component';
- Replace all
TurboLibComponent
occurences withHeadingComponent
- Modify component import to
- Define component in
projects/turbo-lib/src/public_api.ts
file- Remove
export * from './lib/turbo-lib.component';
- Add
export * from './lib/heading.component';
- Remove
When creating new component from scratch, use ng generate component component-name --project=turbo-lib
.
- Serve test application with
ng serve turbo-tester
- Import
TurboLibModule
in main application module, i.e.projects/turbo-tester/src/app/app.module.ts
- Add line
import { TurboLibModule } from "turbo-lib";
- Add line
- Use sample component in
projects/turbo-tester/src/app/app.component.html
- Add line
<turbo-heading></turbo-heading>
- Add line