Skip to content

Commit 7edfd10

Browse files
committed
refactor: Correct a few minor bugs
1 parent c09ad45 commit 7edfd10

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/resolve/resolve-module-name.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
baseName, dirName, isBaseDir, isURL, joinPaths, maybeAddRelativeLocalPrefix, normalizeSlashes, relativePath,
44
removeSuffix
55
} from "../utils";
6-
import { getOutputExtension, Node, Pattern, removeFileExtension, ResolvedModuleFull, SourceFile } from "typescript";
7-
import { getOutputPathForSourceFile } from "../ts";
6+
import { Node, Pattern, removeFileExtension, ResolvedModuleFull, SourceFile } from "typescript";
7+
import { getOutputPathForSourceFile, getOutputExtension } from "../ts";
88
import { getOutputPathDetail, OutputPathDetail } from "./output-path-detail";
99
import { IndexFlags } from "./index-checker";
1010

@@ -63,6 +63,8 @@ function getReturnPath(ctx: GetReturnPathContext) {
6363
resolver,
6464
config: { outputExtensions, outputIndexes },
6565
compilerOptions,
66+
tsInstance,
67+
isDeclarationFile
6668
} = ctx.visitorContext;
6769
const { suppliedExt, resolvedPath, isImplicitExtension, indexDetail, isExternalLibraryImport } = ctx.pathDetail ?? {};
6870

@@ -74,7 +76,7 @@ function getReturnPath(ctx: GetReturnPathContext) {
7476
!isImplicitExtension && outputExtensions !== 'never'
7577
? suppliedExt
7678
: outputExtensions === 'always'
77-
? getOutputExtension(resolvedSourceFile!, compilerOptions)
79+
? getOutputExtension(tsInstance, compilerOptions, resolvedSourceFile!, isDeclarationFile)
7880
: void 0;
7981

8082
let usesStrippedIndex = false;

src/ts/ts-helpers.ts

+9
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,13 @@ export function checkTsSupport(tsInstance: typeof TS) {
137137
throw new Error(`The latest version of 'typescript-transform-paths' requires TS version 4.2.2 or higher. Either upgrade TS or use v3 of the plugin.`);
138138
}
139139

140+
export function getOutputExtension(
141+
tsInstance: typeof TS,
142+
compilerOptions: TS.CompilerOptions,
143+
sourceFile: TS.SourceFile,
144+
isDeclaration: boolean
145+
) {
146+
return isDeclaration ? '.d.ts' : tsInstance.getOutputExtension(sourceFile, compilerOptions);
147+
}
148+
140149
// endregion

src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export const maybeAddRelativeLocalPrefix = (p: string) => (p[0] === "." ? p : `.
3131
* Merge and normalize paths (preserves relative prefix – ie. `./my/path`)
3232
*/
3333
export function joinPaths(...paths: (string | undefined)[]): string {
34-
// path.join / ts.normalizePath cannot be used here, because they remove relative prefix
35-
return normalizePath((paths.filter(p => !!p) as string[]).join('/'))!;
34+
// path.join / normalizePath cannot be used here, because they remove relative prefix
35+
return normalizeSlashes((paths.filter(p => !!p) as string[]).join('/'))!;
3636
}
3737

3838
/**

0 commit comments

Comments
 (0)