Full answer
Creating one: ng generate library my-ui creates a project in projects/my-ui with ng-package.json, package.json, and src/public-api.ts. ng build my-ui runs ng-packagr and writes to dist/my-ui. Publish that folder, not the source.
Key ideas:
- Partial compilation: the library is compiled to "partial Ivy" output, which is stable across Angular minor versions. The application's build links it to the exact Angular version. This is why a library built with Angular 19 works in 20 (within the supported range).
- Public API: only symbols exported from
public-api.tsare available to users. Import paths into internals are blocked by the packageexportsfield. Treat every export as a commitment: exporting less is safer. - Peer dependencies: list
@angular/coreand@angular/commonaspeerDependencieswith a range such as^19.0.0 || ^20.0.0. Do not make them regular dependencies, or users get two copies of Angular and DI breaks. Put small runtime helpers independencies. - Secondary entry points: a folder with its own
ng-package.jsonandpublic-api.tsbecomesmy-ui/table. Users importmy-ui/table, so unused parts are not bundled, and heavy optional dependencies stay optional. - Standalone components and provide functions: export standalone components, directives and pipes, and
provideMyUi(...)functions. Avoid requiring a module. - Styling: use CSS custom properties for themes and
ViewEncapsulationcarefully, since consumers cannot easily overrideEmulatedstyles. - Versioning: semantic versioning. Breaking a public signature is a major release. Use
@deprecatednotes for a migration period, and provideng updateschematics for big changes. - Quality: compile with
strictandstrictTemplates, test with the harness approach, ship harnesses for components, document each public symbol, and test the built package in a real app (npm packorng buildthen link).
During development, path mappings in tsconfig.json let an app import the library source without building. Before release, build and test the real output, since problems appear only in the packaged form.
typescript
// file: public-api.ts
export { ButtonComponent } from './lib/button.component';
export { provideMyUi, withTheme } from './lib/provide-my-ui';
export type { MyUiConfig } from './lib/config';
// file: lib/config.ts
import { InjectionToken } from '@angular/core';
export interface MyUiConfig {
theme: 'light' | 'dark';
}
export const MY_UI_CONFIG = new InjectionToken<MyUiConfig>('MY_UI_CONFIG', {
factory: () => ({ theme: 'light' }),
});
// file: lib/provide-my-ui.ts
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';
import { MY_UI_CONFIG, MyUiConfig } from './config';
export type MyUiFeature = Partial<MyUiConfig>;
export function withTheme(theme: MyUiConfig['theme']): MyUiFeature {
return { theme };
}
export function provideMyUi(...features: MyUiFeature[]): EnvironmentProviders {
const config: MyUiConfig = Object.assign({ theme: 'light' } as MyUiConfig, ...features);
return makeEnvironmentProviders([{ provide: MY_UI_CONFIG, useValue: config }]);
}
// file: lib/button.component.ts
import { Component, inject, input } from '@angular/core';
import { MY_UI_CONFIG } from './config';
@Component({
selector: 'my-ui-button',
template: `<button type="button" [class.dark]="theme === 'dark'"><ng-content /></button>`,
})
export class ButtonComponent {
readonly disabled = input(false);
protected readonly theme = inject(MY_UI_CONFIG).theme;
}
// file: package.json
{
"name": "my-ui",
"version": "1.0.0",
"peerDependencies": {
"@angular/common": "^19.0.0 || ^20.0.0",
"@angular/core": "^19.0.0 || ^20.0.0"
},
"dependencies": {
"tslib": "^2.3.0"
},
"sideEffects": false
}Angular 20.3, the TypeScript of the public API is type-checked. ng-packagr is not installed here, so the library build itself was not run. The JSON files are configuration examples.
Key points
- `ng generate library` and `ng build` use ng-packagr. Publish `dist`, not source.
- Partial Ivy output is linked by the consuming app.
- One public API, Angular as a peer dependency, optional features as secondary entry points.
- Semantic versioning, deprecation periods, and test the packaged output.
Common mistakes
- Listing `@angular/core` under `dependencies`, causing two copies of Angular.
- Exporting internals "for convenience" and then being unable to change them.
- Testing only against library source, and missing packaging errors.
Follow-ups
- Why is a library compiled in partial mode?
- How would you support Angular 19 and 20 with one version?
- How do you ship a theme that apps can customise?
Tips
- Explain partial compilation in one sentence. It shows depth.
- Mention peer dependencies and the two-copies problem.
- Talk about the cost of every public export.
Evaluation rubric
- Knows the build tool and output.
- Designs the public API, entry points, and dependencies correctly.
- Covers versioning and testing the package.