|
1 | 1 | import { VisitorContext } from "../types";
|
2 | 2 | import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
|
3 | 3 | import * as path from "path";
|
| 4 | +import { realpathSync } from "fs"; |
4 | 5 | import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript";
|
5 | 6 | import { getOutputDirForSourceFile } from "./ts-helpers";
|
6 | 7 |
|
@@ -116,6 +117,18 @@ function getResolvedSourceFile(context: VisitorContext, fileName: string): Sourc
|
116 | 117 | // region: Utils
|
117 | 118 | /* ****************************************************************************************************************** */
|
118 | 119 |
|
| 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 | + |
119 | 132 | /**
|
120 | 133 | * Resolve a module name
|
121 | 134 | */
|
@@ -167,12 +180,12 @@ export function resolveModuleName(context: VisitorContext, moduleName: string):
|
167 | 180 |
|
168 | 181 | /* Remove base dirs to make relative to root */
|
169 | 182 | 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); |
172 | 185 | }
|
173 | 186 | }
|
174 | 187 |
|
175 |
| - const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir); |
| 188 | + const outputDir = nativeRelativePath(srcFileOutputDir, moduleFileOutputDir); |
176 | 189 |
|
177 | 190 | /* Compose final output path */
|
178 | 191 | const outputPath = maybeAddRelativeLocalPrefix(tsInstance.normalizePath(path.join(outputDir, outputBaseName)));
|
|
0 commit comments