CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ngtools--webpack

Webpack plugin that AoT compiles your Angular components and modules.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

code-transformations.mddocs/

Code Transformations

TypeScript transformers for Angular-specific code modifications including resource replacement, import elision, and JIT support removal.

Capabilities

Resource Replacement Transformer

Transforms Angular component decorators to replace template and style URLs with webpack imports or direct content.

/**
 * Creates transformer to replace Angular component resources
 * Replaces templateUrl and styleUrls with webpack imports or direct content
 * @param shouldTransform - Function to determine if file should be transformed
 * @param getTypeChecker - Function to get TypeScript type checker
 * @param inlineStyleFileExtension - Optional file extension for inline styles
 * @returns TypeScript transformer factory
 */
function replaceResources(
  shouldTransform: (fileName: string) => boolean,
  getTypeChecker: () => ts.TypeChecker,
  inlineStyleFileExtension?: string
): ts.TransformerFactory<ts.SourceFile>;

Usage Example:

import { replaceResources } from '@ngtools/webpack';

const transformer = replaceResources(
  (fileName) => fileName.endsWith('.component.ts'),
  () => typeChecker,
  'scss'
);

// Original component
@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss']
})

// Transformed (with directTemplateLoading: false)
@Component({
  selector: 'app-example',
  template: require('./example.component.html?ngResource'),
  styles: [require('./example.component.scss?ngResource')]
})

Import Elision Transformer

Removes unused imports from TypeScript source files after other transformations have removed references.

/**
 * Removes imports for which all identifiers have been removed
 * Works with type checker to identify unused imports after transformations
 * @param sourceFile - TypeScript source file to process
 * @param removedNodes - Array of nodes that have been removed by other transformers
 * @param getTypeChecker - Function to get TypeScript type checker
 * @param compilerOptions - TypeScript compiler options
 * @returns Set of import nodes to remove
 */
function elideImports(
  sourceFile: ts.SourceFile,
  removedNodes: ts.Node[],
  getTypeChecker: () => ts.TypeChecker,
  compilerOptions: ts.CompilerOptions
): Set<ts.Node>;

Usage Example:

import { elideImports } from '@ngtools/webpack';

// After other transformers have removed code
const removedImports = elideImports(
  sourceFile,
  removedNodes,
  () => typeChecker,
  compilerOptions
);

// Original file
import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';

// If OnInit and ViewChild were removed by other transformers
// Result after elision:
import { Component } from '@angular/core';
// HttpClient import removed if unused

JIT Support Removal Transformer

Removes Ivy JIT support calls from compiled Angular applications for production builds.

/**
 * Removes Ivy JIT support calls from compiled code
 * Optimizes production builds by removing development-only code
 * @param shouldTransform - Function to determine if file should be transformed
 * @returns TypeScript transformer factory
 */
function removeIvyJitSupportCalls(
  shouldTransform: (fileName: string) => boolean
): ts.TransformerFactory<ts.SourceFile>;

Usage Example:

import { removeIvyJitSupportCalls } from '@ngtools/webpack';

const transformer = removeIvyJitSupportCalls(
  (fileName) => !fileName.includes('node_modules')
);

// Removes calls like:
// ɵɵsetClassMetadata, ɵɵsetNgModuleScope, etc.

Image Domain Discovery Transformer

Discovers and collects image domains from Angular applications for optimization purposes.

/**
 * Finds and collects image domains from Angular applications  
 * Populates the global imageDomains set during compilation
 * @param imageDomains - Set to populate with discovered domains
 * @returns TypeScript transformer factory
 */
function findImageDomains(imageDomains: Set<string>): ts.TransformerFactory<ts.SourceFile>;

Usage Example:

import { findImageDomains, imageDomains } from '@ngtools/webpack';

const transformer = findImageDomains(imageDomains);

// Automatically discovers domains from:
// <img src="https://example.com/image.jpg">
// backgroundImage: 'url(https://cdn.example.com/bg.png)'

AOT Transformer Creation

Creates complete set of transformers for Angular AOT compilation.

/**
 * Creates transformers for Angular AOT compilation
 * Includes all necessary transformers for production AOT builds
 * @param compilerHost - Angular compiler host
 * @param compilerOptions - Angular compiler options  
 * @param program - TypeScript program
 * @param angularCompilerOptions - Angular-specific compiler options
 * @returns Object containing before and after transformers
 */
function createAotTransformers(
  compilerHost: CompilerHost,
  compilerOptions: CompilerOptions,
  program: ts.Program,
  angularCompilerOptions: AngularCompilerOptions
): {
  before: ts.TransformerFactory<ts.SourceFile>[];
  after: ts.TransformerFactory<ts.SourceFile>[];
};

JIT Transformer Creation

Creates transformers for Angular JIT compilation mode.

/**
 * Creates transformers for Angular JIT compilation
 * Includes transformers needed for JIT development builds
 * @param compilerHost - Angular compiler host
 * @param compilerOptions - Angular compiler options
 * @param program - TypeScript program
 * @param angularCompilerOptions - Angular-specific compiler options
 * @returns Object containing before and after transformers
 */
function createJitTransformers(
  compilerHost: CompilerHost,
  compilerOptions: CompilerOptions,
  program: ts.Program,
  angularCompilerOptions: AngularCompilerOptions
): {
  before: ts.TransformerFactory<ts.SourceFile>[];
  after: ts.TransformerFactory<ts.SourceFile>[];
};

Transformer Merging

Utility for merging multiple transformer collections.

/**
 * Merges multiple transformer collections into a single collection
 * Combines before and after transformers from multiple sources
 * @param transformers - Array of transformer collections to merge
 * @returns Merged transformer collection
 */
function mergeTransformers(
  transformers: Array<{
    before?: ts.TransformerFactory<ts.SourceFile>[];
    after?: ts.TransformerFactory<ts.SourceFile>[];
  }>
): {
  before: ts.TransformerFactory<ts.SourceFile>[];
  after: ts.TransformerFactory<ts.SourceFile>[];
};

Bootstrap Replacement

Transforms application bootstrap code for different compilation modes.

/**
 * Replaces bootstrap code in Angular applications
 * Modifies main.ts and similar bootstrap files for different compilation modes
 * @param shouldTransform - Function to determine if file should be transformed
 * @param getEntryModule - Function to get the entry module information
 * @param getTypeChecker - Function to get TypeScript type checker
 * @returns TypeScript transformer factory
 */
function replaceBootstrap(
  shouldTransform: (fileName: string) => boolean,
  getEntryModule: () => { path: string; className: string } | null,
  getTypeChecker: () => ts.TypeChecker
): ts.TransformerFactory<ts.SourceFile>;

Complete Transformation Pipeline Example

Example showing how transformers are used together:

import {
  createAotTransformers,
  createJitTransformers,
  mergeTransformers,
  replaceResources,
  elideImports,
  removeIvyJitSupportCalls,
  findImageDomains,
  imageDomains
} from '@ngtools/webpack';

// Create base transformers based on compilation mode
const baseTransformers = jitMode 
  ? createJitTransformers(host, options, program, angularOptions)
  : createAotTransformers(host, options, program, angularOptions);

// Create additional transformers
const resourceTransformer = replaceResources(
  (fileName) => fileName.endsWith('.component.ts'),
  () => typeChecker,
  'scss'
);

const domainTransformer = findImageDomains(imageDomains);

const jitRemovalTransformer = removeIvyJitSupportCalls(
  (fileName) => !fileName.includes('node_modules')
);

// Merge all transformers
const allTransformers = mergeTransformers([
  baseTransformers,
  {
    before: [resourceTransformer, domainTransformer],
    after: [jitRemovalTransformer]
  }
]);

// Apply transformers during TypeScript compilation
const result = ts.transpileModule(sourceCode, {
  compilerOptions,
  transformers: allTransformers
});

Transformation Order

Transformers are applied in specific order for optimal results:

  1. Before transformers (applied before TypeScript emit):

    • Resource replacement (replaceResources)
    • Image domain discovery (findImageDomains)
    • Angular AOT/JIT transformers
    • Bootstrap replacement (replaceBootstrap)
  2. After transformers (applied after TypeScript emit):

    • JIT support removal (removeIvyJitSupportCalls)
    • Import elision (elideImports)

Performance Considerations

Transformers include optimizations for build performance:

// Selective transformation based on file type
const shouldTransform = (fileName: string) => {
  return fileName.endsWith('.component.ts') || 
         fileName.endsWith('.module.ts') ||
         fileName.includes('/src/');
};

// Use cached type checker
let cachedTypeChecker: ts.TypeChecker;
const getTypeChecker = () => {
  return cachedTypeChecker || (cachedTypeChecker = program.getTypeChecker());
};

docs

code-transformations.md

index.md

plugin-configuration.md

resource-management.md

typescript-compilation.md

webpack-loader.md

tile.json