Compile and package Angular libraries in Angular Package Format (APF)
npx @tessl/cli install tessl/npm-ng-packagr@20.2.0ng-packagr is a comprehensive build tool specifically designed for compiling and packaging Angular libraries according to the Angular Package Format (APF). It automates the complex process of creating distribution-ready npm packages from TypeScript sources, handling bundling, TypeScript definitions, template/stylesheet processing, and ensuring compatibility with modern Angular build systems.
npm install ng-packagrimport { ngPackagr, NgPackagr, NgPackagrOptions } from "ng-packagr";
import { build, execute, version } from "ng-packagr";For CommonJS:
const { ngPackagr, build, execute, version } = require("ng-packagr");import { ngPackagr } from "ng-packagr";
// Basic build
await ngPackagr()
.forProject('./ng-package.json')
.build();
// Build with options
await ngPackagr()
.forProject('./ng-package.json')
.build({
watch: false,
cacheEnabled: true
});
// Watch mode
ngPackagr()
.forProject('./ng-package.json')
.watch()
.subscribe();# Basic build
ng-packagr -p ng-package.json
# Watch mode
ng-packagr -p ng-package.json -w
# Custom TypeScript config
ng-packagr -p ng-package.json -c tsconfig.lib.jsonng-packagr is built around several key architectural components:
injection-js for modular, testable architectureCore programmatic interface for building Angular libraries programmatically from Node.js applications or build scripts.
function ngPackagr(): NgPackagr;
class NgPackagr {
forProject(project: string): NgPackagr;
withOptions(options: NgPackagrOptions): NgPackagr;
withTsConfig(defaultValues: ParsedConfiguration | string): NgPackagr;
withProviders(providers: Provider[]): NgPackagr;
withBuildTransform(transform: InjectionToken<Transform>): NgPackagr;
build(options?: NgPackagrOptions): Promise<void>;
watch(options?: NgPackagrOptions): Observable<void>;
buildAsObservable(): Observable<void>;
}Command interface for executing build operations with unified error handling and promise-based execution.
interface Command<Arguments, Result> {
(args?: Arguments): Result | Promise<Result>;
}
function execute<A, R>(command: Command<A, R>, args?: A): Promise<R>;
const build: Command<CliArguments, void>;
const version: Command<any, Promise<void>>;Comprehensive configuration system supporting ng-package.json and ng-entrypoint.json files with JSON schema validation.
interface NgPackagrOptions {
watch?: boolean;
cacheEnabled?: boolean;
cacheDirectory?: string;
poll?: number;
}
interface CliArguments {
project: string;
watch?: boolean;
config?: string;
poll?: number;
}