Skip to content

Commit 419dbe6

Browse files
authored
fix: fix osx root path issues
fixes so all paths dont get rewritten to `../../../../../../../users/...` on osx fixes LeDDGroup#167
1 parent 04fd73c commit 419dbe6

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/utils/resolve-module-name.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { VisitorContext } from "../types";
22
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
33
import * as path from "path";
4+
import { realpathSync } from "fs";
45
import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript";
56
import { getOutputDirForSourceFile } from "./ts-helpers";
67

@@ -116,6 +117,18 @@ function getResolvedSourceFile(context: VisitorContext, fileName: string): Sourc
116117
// region: Utils
117118
/* ****************************************************************************************************************** */
118119

120+
function tryRealpathNative(value) {
121+
try {
122+
return realpathSync.native(value)
123+
} catch {
124+
return value
125+
}
126+
}
127+
128+
function nativeRelativePath(from, to) {
129+
return path.relative(tryRealpathNative(from), tryRealpathNative(to))
130+
}
131+
119132
/**
120133
* Resolve a module name
121134
*/
@@ -167,12 +180,12 @@ export function resolveModuleName(context: VisitorContext, moduleName: string):
167180

168181
/* Remove base dirs to make relative to root */
169182
if (fileRootDir && moduleRootDir) {
170-
srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir);
171-
moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir);
183+
srcFileOutputDir = nativeRelativePath(fileRootDir, srcFileOutputDir);
184+
moduleFileOutputDir = nativeRelativePath(moduleRootDir, moduleFileOutputDir);
172185
}
173186
}
174187

175-
const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir);
188+
const outputDir = nativeRelativePath(srcFileOutputDir, moduleFileOutputDir);
176189

177190
/* Compose final output path */
178191
const outputPath = maybeAddRelativeLocalPrefix(tsInstance.normalizePath(path.join(outputDir, outputBaseName)));

0 commit comments

Comments
 (0)