ng-di-transpiler

raw JSON →
0.1.10 verified Fri May 01 auth: no javascript

ng-di-transpiler v0.1.10 is a dependency injection transpiler for Angular that generates platform-specific provider files at build time. It allows developers to define interfaces for tokens and create typed provider configurations, which are then transpiled into compiled provider files. The tool is designed for Angular 2+ applications and aims to reduce boilerplate and improve type safety in DI setup. It is still in early stages (v0.1.x) with experimental features like interface-based tokens. Alternatives include Angular's built-in provider factories or custom AOT compilation setups, but this package provides a dedicated transpilation step. Release cadence is sporadic with occasional bug fixes. Ships TypeScript types and uses a CLI command (ngdt) for integration.

error Cannot find name 'TokenProviders'
cause Missing import from ng-di-transpiler.
fix
Add import: import { TokenProviders } from 'ng-di-transpiler';
error Type 'typeof SomeClass' is not assignable to type 'TokenProvider<T>'
cause Passing class reference instead of instance to TokenProvider constructor.
fix
Instantiate the provider class: new SomeTokenProvider(ActualClass) instead of new SomeTokenProvider(ActualClass).
error ngdt: command not found
cause Package not installed or not in PATH.
fix
Install locally: npm install ng-di-transpiler --save-dev, then use npx ngdt or add script in package.json.
error No provider for TokenProviders
cause The token interface not extending TokenProviders or variable not typed correctly.
fix
Ensure your platform tokens interface extends TokenProviders and the exported const is typed with that interface.
gotcha Experimental interface-as-token feature (v0.1.0) lacks tests and may be unstable.
fix Stick to using OpaqueToken/InjectionToken for token types until feature is stable.
gotcha Type checking relies on the variable type annotation; incorrect type may cause transpilation to skip or generate wrong code.
fix Ensure exported const in provider files is explicitly typed with the interface extending TokenProviders.
gotcha Only files matching the glob pattern are transpiled; mismatched patterns result in no output.
fix Use explicit --files argument or configure ngdtOptions in tsconfig.json.
npm install ng-di-transpiler
yarn add ng-di-transpiler
pnpm add ng-di-transpiler

Shows how to define tokens, create a provider file, and run the CLI to transpile.

// 1. Define platform tokens interface
import { TokenProviders, TokenProvider } from 'ng-di-transpiler';
import { OpaqueToken } from '@angular/core';

interface ConsoleLike {
  log(msg: string): void;
}
export const CONSOLE_TOKEN = new OpaqueToken('console');

export interface PlatformTokens extends TokenProviders {
  console: ConsoleProvider;
}

export class ConsoleProvider extends TokenProvider<ConsoleLike> {
  provider: CONSOLE_TOKEN;
}

// 2. Create platform provider file: providers.browser.ts
import { PlatformTokens, ConsoleProvider } from './platform-tokens';
class BrowserLogger { log(msg: string) { console.log(msg); } }
export const TOKENS: PlatformTokens = {
  console: new ConsoleProvider(BrowserLogger)
};

// 3. Run CLI to compile: ngdt --files "src/**/providers.browser.ts" --postfix .compiled
// Generates providers.browser.compiled.ts