← Back to Angular, page 5

angular-0095 · Hard senior

How do you build and publish a reusable Angular library, and what do public API design, peer dependencies and secondary entry points mean?

Angular libraries are built with ng-packagr (ng build for a library project) into a partial-Ivy package that each consuming app compiles again. Control the public API through one public-api.ts, declare Angular as a peer dependency, split optional features into secondary entry points, expose provide functions, and follow semantic versioning.

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:

  1. 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).
  2. Public API: only symbols exported from public-api.ts are available to users. Import paths into internals are blocked by the package exports field. Treat every export as a commitment: exporting less is safer.
  3. Peer dependencies: list @angular/core and @angular/common as peerDependencies with 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 in dependencies.
  4. Secondary entry points: a folder with its own ng-package.json and public-api.ts becomes my-ui/table. Users import my-ui/table, so unused parts are not bundled, and heavy optional dependencies stay optional.
  5. Standalone components and provide functions: export standalone components, directives and pipes, and provideMyUi(...) functions. Avoid requiring a module.
  6. Styling: use CSS custom properties for themes and ViewEncapsulation carefully, since consumers cannot easily override Emulated styles.
  7. Versioning: semantic versioning. Breaking a public signature is a major release. Use @deprecated notes for a migration period, and provide ng update schematics for big changes.
  8. Quality: compile with strict and strictTemplates, test with the harness approach, ship harnesses for components, document each public symbol, and test the built package in a real app (npm pack or ng build then 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.