CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-parcel--transformer-typescript-types

A Parcel transformer that processes TypeScript files to generate declaration files (.d.ts) with tree-shaking and module graph optimization

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

module-graph.mddocs/

Module Graph Management

System for tracking and managing dependencies between TypeScript modules, enabling efficient tree-shaking and symbol resolution across the entire module graph.

Capabilities

TSModuleGraph

Manages the dependency graph between TypeScript modules for tree-shaking and symbol resolution.

/**
 * Manages dependency relationships between TypeScript modules
 */
class TSModuleGraph {
  /**
   * Creates a new module graph with a main module
   * @param mainModuleName - Name of the entry point module
   */
  constructor(mainModuleName: string);
  
  /**
   * Adds a module to the graph
   * @param name - Module name
   * @param module - TSModule instance
   */
  addModule(name: string, module: TSModule): void;
  
  /**
   * Retrieves a module by name
   * @param name - Module name
   * @returns TSModule instance or null if not found
   */
  getModule(name: string): TSModule | null;
  
  /**
   * Marks a symbol as used and propagates usage to dependencies
   * @param module - Module containing the symbol
   * @param name - Symbol name
   * @param context - TypeScript transform context
   */
  markUsed(module: TSModule, name: string, context: any): void;
  
  /**
   * Resolves export information for a module export
   * @param module - Module containing the export
   * @param exportObj - Export definition
   * @returns Resolved export information or null
   */
  getExport(module: TSModule, exportObj: Export): ResolvedExport | null;
  
  /**
   * Resolves import to its original export
   * @param module - Module containing the import
   * @param local - Local import name
   * @param imported - Imported symbol name (optional)
   * @returns Resolved import information or null
   */
  resolveImport(module: TSModule, local: string, imported?: string): ResolvedImport | null;
  
  /**
   * Resolves export by name in a module
   * @param module - Module to search
   * @param name - Export name
   * @returns Resolved export information or null
   */
  resolveExport(module: TSModule, name: string): ResolvedExport | null;
  
  /**
   * Gets all exports from a module
   * @param module - Module to get exports from (defaults to main module)
   * @param excludeDefault - Whether to exclude default exports
   * @returns Array of resolved exports
   */
  getAllExports(module?: TSModule, excludeDefault?: boolean): ResolvedExport[];
  
  /**
   * Gets all external imports across all modules
   * @returns Map of specifier to import mappings
   */
  getAllImports(): Map<string, Map<string, string>>;
  
  /**
   * Propagates usage information and assigns unique names
   * @param context - TypeScript transform context
   * @returns Map of exported names to their modules
   */
  propagate(context: any): Map<string, TSModule>;
}

TSModule

Represents a single TypeScript module with its imports, exports, and local bindings.

/**
 * Represents a TypeScript module with imports, exports, and bindings
 */
class TSModule {
  /**
   * Map of local names to import information
   */
  imports: Map<string, Import>;
  
  /**
   * Array of export definitions
   */
  exports: Export[];
  
  /**
   * Map of binding names to AST node sets
   */
  bindings: Map<string, Set<any>>;
  
  /**
   * Map of original names to renamed names
   */
  names: Map<string, string>;
  
  /**
   * Set of used symbol names
   */
  used: Set<string>;
  
  /**
   * Creates a new empty module
   */
  constructor();
  
  /**
   * Adds an import to the module
   * @param local - Local binding name
   * @param specifier - Module specifier (e.g., './other-module')
   * @param imported - Imported symbol name ('default', '*', or named export)
   */
  addImport(local: string, specifier: string, imported: string): void;
  
  /**
   * Adds an export to the module
   * @param name - Export name
   * @param imported - Local symbol being exported
   * @param specifier - Re-export specifier (optional)
   */
  addExport(name: string, imported: string, specifier?: string): void;
  
  /**
   * Adds a wildcard export (export * from 'module')
   * @param specifier - Module specifier
   */
  addWildcardExport(specifier: string): void;
  
  /**
   * Adds a local binding to the module
   * @param name - Binding name
   * @param node - TypeScript AST node
   */
  addLocal(name: string, node: any): void;
  
  /**
   * Gets the renamed version of a symbol
   * @param name - Original symbol name
   * @returns Renamed symbol name or original if not renamed
   */
  getName(name: string): string;
  
  /**
   * Checks if a binding exists in the module
   * @param name - Binding name
   * @returns True if binding exists
   */
  hasBinding(name: string): boolean;
}

Type Definitions

/**
 * Import definition for a symbol
 */
type Import = {
  specifier: string;  // Module path
  imported: string;   // Symbol name in source module
};

/**
 * Export definition - either named export or wildcard
 */
type Export = 
  | {
      name: string;           // Export name
      imported: string;       // Local symbol name
      specifier?: string;     // Re-export source (optional)
    }
  | {
      specifier: string;      // Wildcard export source
    };

/**
 * Resolved export information
 */
type ResolvedExport = {
  imported: string;     // Symbol name in source module
  module: TSModule;     // Source module
  name: string;         // Export name
};

/**
 * Resolved import information
 */
type ResolvedImport = {
  imported: string;     // Symbol name
  module: TSModule;     // Source module
  name: string;         // Local name
};

Usage Examples:

import { TSModuleGraph, TSModule } from '@parcel/transformer-typescript-types';

// Create module graph for project with entry point 'index'
const graph = new TSModuleGraph('index');

// Create and configure the main module
const mainModule = new TSModule();
mainModule.addImport('Button', './button', 'default');    // import Button from './button'
mainModule.addImport('utils', './utils', '*');            // import * as utils from './utils'
mainModule.addImport('React', 'react', 'default');        // import React from 'react'
mainModule.addExport('Button', 'Button');                 // export { Button }
mainModule.addLocal('config', configNode);                // const config = { ... }
graph.addModule('index', mainModule);

// Create button module with re-exports
const buttonModule = new TSModule();
buttonModule.addImport('Component', 'react', 'Component'); // import { Component } from 'react'
buttonModule.addLocal('Button', buttonClassNode);          // class Button extends Component
buttonModule.addExport('default', 'Button');              // export default Button
graph.addModule('./button', buttonModule);

// Track usage starting from exports
graph.markUsed(mainModule, 'Button', context);

// Propagate usage and get final exported names
const exportedNames = graph.propagate(context);
console.log(exportedNames.get('Button')); // mainModule

// Get all external imports needed in final output
const externalImports = graph.getAllImports();
// Map { 'react' => Map { 'React' => 'default', 'Component' => 'Component' } }

// Resolve specific imports
const resolved = graph.resolveImport(mainModule, 'Button');
console.log(resolved); // { imported: 'Button', module: mainModule, name: 'Button' }

Graph Operations

Symbol Resolution

The module graph provides sophisticated symbol resolution that:

  • Follows import chains across multiple modules
  • Resolves re-exports to their original sources
  • Handles namespace imports and qualified name references
  • Tracks external module dependencies

Usage Propagation

When a symbol is marked as used:

  1. If it's an import, marks the symbol as used in the source module
  2. Recursively marks dependencies in the original binding
  3. Visits all AST nodes associated with the binding
  4. Marks any referenced types as used

Name Collision Resolution

The propagation phase assigns unique names to avoid conflicts:

  • Preserves exported names for public API
  • Renames internal symbols that would conflict
  • Handles namespace collisions across modules
  • Updates import statements for external dependencies

Install with Tessl CLI

npx tessl i tessl/npm-parcel--transformer-typescript-types

docs

ast-processing.md

index.md

module-graph.md

transformer.md

ts-compatibility.md

tile.json