|
1 | 1 | # TSConfig Support |
2 | 2 |
|
3 | | -TODO |
| 3 | +TSConfig support is a critical component for TypeScript projects in the import resolution system. It processes TypeScript configuration files (tsconfig.json) to correctly resolve module paths and dependencies. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +The TSConfig support system serves these purposes: |
| 8 | + |
| 9 | +1. **Path Mapping**: Resolves custom module path aliases defined in the tsconfig.json file. |
| 10 | +1. **Base URL Resolution**: Handles non-relative module imports using the baseUrl configuration. |
| 11 | +1. **Project References**: Manages dependencies between TypeScript projects using the references field. |
| 12 | +1. **Directory Structure**: Respects rootDir and outDir settings for maintaining proper directory structures. |
| 13 | + |
| 14 | +## Core Components |
| 15 | + |
| 16 | +### TSConfig Class |
| 17 | + |
| 18 | +The `TSConfig` class represents a parsed TypeScript configuration file. It: |
| 19 | + |
| 20 | +- Parses and stores the configuration settings from tsconfig.json |
| 21 | +- Handles inheritance through the "extends" field |
| 22 | +- Provides methods for translating between import paths and absolute file paths |
| 23 | +- Caches computed values for performance optimization |
| 24 | + |
| 25 | +## Configuration Processing |
| 26 | + |
| 27 | +### Configuration Inheritance |
| 28 | + |
| 29 | +TSConfig files can extend other configuration files through the "extends" field: |
| 30 | + |
| 31 | +1. Base configurations are loaded and parsed first |
| 32 | +1. Child configurations inherit and can override settings from their parent |
| 33 | +1. Path mappings, base URLs, and other settings are merged appropriately |
| 34 | + |
| 35 | +### Path Mapping Resolution |
| 36 | + |
| 37 | +The system processes the "paths" field in tsconfig.json to create a mapping between import aliases and file paths: |
| 38 | + |
| 39 | +1. Path patterns are normalized (removing wildcards, trailing slashes) |
| 40 | +1. Relative paths are converted to absolute paths |
| 41 | +1. Mappings are stored for efficient lookup during import resolution |
| 42 | + |
| 43 | +### Project References |
| 44 | + |
| 45 | +The "references" field defines dependencies between TypeScript projects: |
| 46 | + |
| 47 | +1. Referenced projects are identified and loaded |
| 48 | +1. Their configurations are analyzed to determine import paths |
| 49 | +1. Import resolution can cross project boundaries using these references |
| 50 | + |
| 51 | +## Import Resolution Process |
| 52 | + |
| 53 | +### Path Translation |
| 54 | + |
| 55 | +When resolving an import path in TypeScript: |
| 56 | + |
| 57 | +1. Check if the path matches any path alias in the tsconfig.json |
| 58 | +1. If a match is found, translate the path according to the mapping |
| 59 | +1. Apply baseUrl resolution for non-relative imports |
| 60 | +1. Handle project references for cross-project imports |
| 61 | + |
| 62 | +### Optimization Techniques |
| 63 | + |
| 64 | +The system employs several optimizations: |
| 65 | + |
| 66 | +1. Caching computed values to avoid redundant processing |
| 67 | +1. Early path checking for common patterns (e.g., paths starting with "@" or "~") |
| 68 | +1. Hierarchical resolution that respects the configuration inheritance chain |
| 69 | + |
| 70 | +## Integration with Import Resolution |
| 71 | + |
| 72 | +The TSConfig support integrates with the broader import resolution system: |
| 73 | + |
| 74 | +1. Each TypeScript file is associated with its nearest tsconfig.json |
| 75 | +1. Import statements are processed using the file's associated configuration |
| 76 | +1. Path mappings are applied during the module resolution process |
| 77 | +1. Project references are considered when resolving imports across project boundaries |
4 | 78 |
|
5 | 79 | ## Next Step |
6 | 80 |
|
|
0 commit comments