Angular Modernizer
    Preparing search index...

    Path Alias Resolver

    Resolves TypeScript path aliases from tsconfig.json to prevent parser errors on valid import statements. Commonly used for custom path mappings like:

    • @shared/* → src/shared/*
    • @domain/* → src/domain/*
    • @features/* → src/features/*
    • @core/* → src/core/*
    1. Custom Path Aliases

      import { SharedModule } from '@shared/modules';
      // Parser error: Cannot find module '@shared/modules'
      // Resolution: @shared/* → src/shared/*
      // Result: import { SharedModule } from '../shared/modules';
    2. External Decorator Projects

      import { PageComponent } from '@shared/decorators/page';
      // Parser error: Cannot find module '@shared/decorators/page'
      // Resolution: @shared/* → src/shared/*
      // Result: import { PageComponent } from '../shared/decorators/page';
    3. Monorepo Packages

      import { UserService } from '@domain/user/services/user.service';
      // Parser error: Cannot find module '@domain/user/services/user.service'
      // Resolution: @domain/* → packages/domain/*
      // Result: import { UserService } from '../../domain/user/services/user.service';
    tsconfig.json → PathAliasResolver → Preprocessed Content → Parser
    ↓
    Alias Mappings
    (cached in memory)
    import { PathAliasResolver } from '@angular-modernizer/core';
    import { Project } from 'ts-morph';

    const project = new Project({ tsConfigFilePath: './tsconfig.json' });
    const resolver = new PathAliasResolver(project);

    // Preprocess file content before parsing
    const originalContent = fs.readFileSync('component.ts', 'utf-8');
    const preprocessed = resolver.preprocessFileContent(
    originalContent,
    'src/app/user/user.component.ts'
    );

    // Now parser can handle the resolved imports
    const sourceFile = project.createSourceFile('temp.ts', preprocessed);
    Index

    Constructors

    • Initialize the Path Alias Resolver

      Loads path mappings from tsconfig.json and prepares for resolution.

      Parameters

      • project: Project

        ts-morph Project instance with tsconfig loaded

      • Optionaloptions: PathAliasResolverOptions

        Optional configuration

      Returns PathAliasResolver

      const project = new Project({ tsConfigFilePath: './tsconfig.json' });
      const resolver = new PathAliasResolver(project);
      const resolver = new PathAliasResolver(project, {
      customAliases: [
      { alias: '@legacy/*', resolvedPath: 'legacy/src' }
      ]
      });

    Methods

    • Add a custom path alias at runtime

      Allows extending the alias set beyond tsconfig.json for dynamic scenarios.

      Parameters

      Returns void

      resolver.addAlias({
      alias: '@legacy',
      resolvedPath: 'legacy/src'
      });
    • Clear the resolution cache

      Useful when aliases are modified at runtime.

      Returns void

    • Get the count of registered aliases

      Returns number

      Number of aliases

    • Get cache statistics

      Returns { size: number }

      Cache size information

      • size: number

        Number of cached alias resolutions.

    • Preprocess file content to replace path aliases in import statements

      Scans file content for import/export statements and replaces alias-based paths with relative paths. This prevents parser errors on valid code.

      Parameters

      • content: string

        Original file content with path aliases

      • filePath: string

        Path to the file being preprocessed (for relative path calculation)

      Returns string

      Preprocessed content with resolved import paths

      const original = `
      import { SharedModule } from '@shared/modules';
      import { UserService } from '@domain/user/services/user.service';
      `;

      const preprocessed = resolver.preprocessFileContent(
      original,
      'src/app/user/user.component.ts'
      );

      // Result:
      // import { SharedModule } from '../../shared/modules';
      // import { UserService } from '../../../domain/user/services/user.service';
    • Resolve a single import path using registered aliases

      Converts alias-based imports to relative paths based on the target file location.

      Parameters

      • importPath: string

        Import path to resolve (e.g., "@shared/modules")

      • fromFile: string

        File path where the import is used (for relative path calculation)

      Returns string

      Resolved import path, or original if no alias matches

      const resolved = resolver.resolveImport(
      '@shared/modules/shared.module',
      'src/app/user/user.component.ts'
      );
      // Returns: '../../shared/modules/shared.module'