From 33a50bcfcc749fc13c9437378b97ecde67c8bf79 Mon Sep 17 00:00:00 2001 From: Tyler <26290074+tylersayshi@users.noreply.github.com> Date: Tue, 19 Aug 2025 23:26:23 -0700 Subject: [PATCH] more unified consistent extensions still fragile, but maybe ok --- src/path-alias/path-alias.ts | 40 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/path-alias/path-alias.ts b/src/path-alias/path-alias.ts index f441c6f..eb3f02d 100644 --- a/src/path-alias/path-alias.ts +++ b/src/path-alias/path-alias.ts @@ -41,6 +41,19 @@ function findTsConfigPath(filePath: string): string | null { return null; } +function pathWithExtension(path: string): string { + const finalPath = path.split("/"); + const lastPart = finalPath.at(-1); + if ( + lastPart?.includes(".") && + (!lastPart.endsWith(".ts") || !lastPart.endsWith(".tsx")) + ) { + finalPath[finalPath.length - 1] = lastPart.replace(/\./g, "") + ".ts"; + } + const joined = finalPath.join("/"); + return joined.startsWith(".") ? `${joined}.ts` : `./${joined}.ts`; +} + function resolvePathAlias( importPath: string, pathMappings: PathMapping, @@ -77,18 +90,7 @@ function resolvePathAlias( join(baseDir, resolvedPath), ); - if ( - !relativePath.endsWith(".ts") && !relativePath.endsWith(".tsx") && - !relativePath.endsWith(".js") && !relativePath.endsWith(".jsx") - ) { - return relativePath.startsWith(".") - ? `${relativePath}.ts` - : `./${relativePath}.ts`; - } - - return relativePath.startsWith(".") - ? relativePath - : `./${relativePath}`; + return pathWithExtension(relativePath); } } } @@ -196,13 +198,17 @@ function convertPathAliases(sourceFile: SourceFile): void { if (firstArg.getKind() === SyntaxKind.StringLiteral) { const importPath = (firstArg as StringLiteral).getLiteralValue(); - // Skip if it's already a relative path or external package + // Common external packages that shouldn't be transformed + // TODO use package.json to detect external packages + if (!importPath.includes("/")) { + return; + } + if ( - importPath.startsWith(".") || importPath.startsWith("/") || - !importPath.includes("/") || importPath.includes("node_modules") - // Common external packages that shouldn't be transformed - // TODO use package.json to detect external packages + importPath.startsWith(".") || + !importPath.includes("/") ) { + firstArg.replaceWithText(`"${pathWithExtension(importPath)}"`); return; } -- 2.51.2