diff --git a/src/utils/general-utils.ts b/src/utils/general-utils.ts index 89652cb..2456ffa 100755 --- a/src/utils/general-utils.ts +++ b/src/utils/general-utils.ts @@ -1,14 +1,30 @@ import url from "url"; import path from "path"; +import { realpathSync } from "fs"; /* ****************************************************************************************************************** * * General Utilities & Helpers * ****************************************************************************************************************** */ export const isURL = (s: string): boolean => !!s && (!!url.parse(s).host || !!url.parse(s).hostname); + export const cast = (v: any): T => v; + export const isBaseDir = (baseDir: string, testDir: string): boolean => { const relative = path.relative(baseDir, testDir); return relative ? !relative.startsWith("..") && !path.isAbsolute(relative) : true; }; + export const maybeAddRelativeLocalPrefix = (p: string) => (p[0] === "." ? p : `./${p}`); + +export function tryRealpathNative(value: string) { + try { + return realpathSync.native(value); + } catch { + return value; + } +} + +export function nativeRelativePath(from: string, to: string) { + return path.relative(tryRealpathNative(from), tryRealpathNative(to)); +} diff --git a/src/utils/resolve-module-name.ts b/src/utils/resolve-module-name.ts index b31158b..7abf35d 100755 --- a/src/utils/resolve-module-name.ts +++ b/src/utils/resolve-module-name.ts @@ -1,5 +1,5 @@ import { VisitorContext } from "../types"; -import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils"; +import { isBaseDir, isURL, maybeAddRelativeLocalPrefix, nativeRelativePath } from "./general-utils"; import * as path from "path"; import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript"; import { getOutputDirForSourceFile } from "./ts-helpers"; @@ -167,12 +167,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)));