From 419dbe6074b1e1fbc09038a532a5de18e8711212 Mon Sep 17 00:00:00 2001 From: Braden Napier Date: Thu, 15 Dec 2022 15:36:37 -0500 Subject: [PATCH] fix: fix osx root path issues fixes so all paths dont get rewritten to `../../../../../../../users/...` on osx fixes #167 --- src/utils/resolve-module-name.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/utils/resolve-module-name.ts b/src/utils/resolve-module-name.ts index b31158b..e07db56 100755 --- a/src/utils/resolve-module-name.ts +++ b/src/utils/resolve-module-name.ts @@ -1,6 +1,7 @@ import { VisitorContext } from "../types"; import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils"; import * as path from "path"; +import { realpathSync } from "fs"; import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript"; import { getOutputDirForSourceFile } from "./ts-helpers"; @@ -116,6 +117,18 @@ function getResolvedSourceFile(context: VisitorContext, fileName: string): Sourc // region: Utils /* ****************************************************************************************************************** */ +function tryRealpathNative(value) { + try { + return realpathSync.native(value) + } catch { + return value + } +} + +function nativeRelativePath(from, to) { + return path.relative(tryRealpathNative(from), tryRealpathNative(to)) +} + /** * Resolve a module name */ @@ -167,12 +180,12 @@ export function resolveModuleName(context: VisitorContext, moduleName: string): /* Remove base dirs to make relative to root */ if (fileRootDir && moduleRootDir) { - srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir); - moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir); + srcFileOutputDir = nativeRelativePath(fileRootDir, srcFileOutputDir); + moduleFileOutputDir = nativeRelativePath(moduleRootDir, moduleFileOutputDir); } } - const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir); + const outputDir = nativeRelativePath(srcFileOutputDir, moduleFileOutputDir); /* Compose final output path */ const outputPath = maybeAddRelativeLocalPrefix(tsInstance.normalizePath(path.join(outputDir, outputBaseName)));