๐Ÿ“ฆ colinhacks / zshy

๐Ÿ“„ tx-paths-resolver.ts ยท 189 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189import * as fs from "node:fs";
import * as path from "node:path";
import * as ts from "typescript";

export interface PathsConfig {
  baseUrl?: string;
  paths?: Record<string, string[]>;
  tsconfigDir: string;
  rootDir: string;
}

function matchPathPattern(moduleSpecifier: string, pattern: string): { matched: boolean; captured?: string } {
  // Handle wildcard patterns
  if (pattern.includes("*")) {
    // Convert pattern to regex
    const regexPattern = pattern
      .replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
      .replace(/\\\*/g, "(.*)");
    const regex = new RegExp(`^${regexPattern}$`);
    const match = moduleSpecifier.match(regex);
    
    if (match) {
      return { matched: true, captured: match[1] };
    }
  } else if (moduleSpecifier === pattern) {
    // Exact match
    return { matched: true };
  }
  
  return { matched: false };
}

function resolvePaths(
  moduleSpecifier: string,
  config: PathsConfig
): string | null {
  // Skip relative imports
  if (moduleSpecifier.startsWith("./") || moduleSpecifier.startsWith("../")) {
    return null;
  }
  
  // Skip node modules
  if (!moduleSpecifier.startsWith("@") && !moduleSpecifier.includes("/") && !config.paths?.[moduleSpecifier]) {
    // Check if this could be a paths alias
    const hasMatchingPattern = config.paths && Object.keys(config.paths).some(pattern => {
      const cleanPattern = pattern.replace("*", "");
      return moduleSpecifier.startsWith(cleanPattern);
    });
    
    if (!hasMatchingPattern) {
      return null;
    }
  }
  
  if (!config.paths) return null;
  
  // Try to match against path patterns
  for (const [pattern, replacements] of Object.entries(config.paths)) {
    const { matched, captured } = matchPathPattern(moduleSpecifier, pattern);
    
    if (matched) {
      // Try each replacement in order
      for (const replacement of replacements) {
        let resolvedPath = replacement;
        
        // Replace wildcard with captured content
        if (captured !== undefined && replacement.includes("*")) {
          resolvedPath = replacement.replace("*", captured);
        }
        
        // Resolve relative to baseUrl or tsconfig directory
        const basePath = config.baseUrl 
          ? path.resolve(config.tsconfigDir, config.baseUrl)
          : config.tsconfigDir;
        
        const absolutePath = path.resolve(basePath, resolvedPath);
        
        // Check if the path exists (try with various extensions)
        const extensions = [".ts", ".tsx", ".js", ".jsx", "/index.ts", "/index.tsx", "/index.js", "/index.jsx"];
        
        // First try exact path
        if (fs.existsSync(absolutePath)) {
          // Check if it's a directory with index file
          if (fs.statSync(absolutePath).isDirectory()) {
            for (const ext of ["/index.ts", "/index.tsx", "/index.js", "/index.jsx"]) {
              if (fs.existsSync(absolutePath + ext)) {
                // Return relative path from the source file location
                return absolutePath + ext;
              }
            }
          } else {
            return absolutePath;
          }
        }
        
        // Try with extensions
        for (const ext of extensions) {
          if (fs.existsSync(absolutePath + ext)) {
            return absolutePath + ext;
          }
        }
      }
    }
  }
  
  return null;
}

export const createPathsResolverTransformer =
  (config: PathsConfig): ts.TransformerFactory<ts.SourceFile | ts.Bundle> =>
  (context) => {
    return (sourceFile) => {
      const visitor = (node: ts.Node): ts.Node => {
        const isImport = ts.isImportDeclaration(node);
        const isExport = ts.isExportDeclaration(node);
        const isDynamicImport = ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
        
        let originalText: string;
        if (isImport || isExport || isDynamicImport) {
          if (isImport || isExport) {
            if (!node.moduleSpecifier || !ts.isStringLiteral(node.moduleSpecifier)) {
              return ts.visitEachChild(node, visitor, context);
            }
            
            originalText = node.moduleSpecifier.text;
          } else if (isDynamicImport) {
            const arg = node.arguments[0]!;
            if (!ts.isStringLiteral(arg)) {
              return ts.visitEachChild(node, visitor, context);
            }
            originalText = arg.text;
          } else {
            return ts.visitEachChild(node, visitor, context);
          }
          
          // Try to resolve using paths
          const resolvedPath = resolvePaths(originalText, config);
          
          if (resolvedPath && ts.isSourceFile(sourceFile)) {
            // Convert absolute path to relative path from current source file
            const sourceFileDir = path.dirname(sourceFile.fileName);
            let relativePath = path.relative(sourceFileDir, resolvedPath);
            
            // Ensure it starts with ./ or ../
            if (!relativePath.startsWith(".")) {
              relativePath = "./" + relativePath;
            }
            
            // Convert to posix path
            relativePath = relativePath.replace(/\\/g, "/");
            
            // Remove extension for TypeScript files
            if (relativePath.endsWith(".ts") || relativePath.endsWith(".tsx")) {
              relativePath = relativePath.replace(/\.(ts|tsx)$/, "");
            }
            
            // Update the node with resolved path
            if (isImport) {
              return ts.factory.updateImportDeclaration(
                node,
                node.modifiers,
                node.importClause,
                ts.factory.createStringLiteral(relativePath),
                node.assertClause
              );
            } else if (isExport) {
              return ts.factory.updateExportDeclaration(
                node,
                node.modifiers,
                node.isTypeOnly,
                node.exportClause,
                ts.factory.createStringLiteral(relativePath),
                node.assertClause
              );
            } else if (isDynamicImport) {
              return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
                ts.factory.createStringLiteral(relativePath),
                ...node.arguments.slice(1),
              ]);
            }
          }
        }
        
        return ts.visitEachChild(node, visitor, context);
      };
      
      return ts.visitNode(sourceFile, visitor) as ts.SourceFile;
    };
  };