Skip to content

Commit 4d57893

Browse files
committed
fix: regard file extensions during path resolution (dividab#133)
Originally during the file path resolution, file extensions were removed without explicit reason. This commit changes the resolution logic to keep file extensions, with the goal to add support for modern non-js extensions like cjs. The change however keeps /xyz/index resolves as is as they still should resolve to /xyz. Refs 847d314
1 parent 7204659 commit 4d57893

File tree

4 files changed

+6
-21
lines changed

4 files changed

+6
-21
lines changed

src/filesystem.ts

-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,3 @@ export function fileExistsAsync(
8181
callback2(undefined, stats ? stats.isFile() : false);
8282
});
8383
}
84-
85-
export function removeExtension(path: string): string {
86-
return path.substring(0, path.lastIndexOf(".")) || path;
87-
}

src/match-path-async.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ function findFirstExistingPath(
148148
return doneCallback(err);
149149
}
150150
if (exists) {
151-
// Not sure why we don't just return the full path? Why strip it?
152151
return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
153152
}
154153
if (index === tryPaths.length - 1) {
@@ -180,11 +179,7 @@ function findFirstExistingPath(
180179
return doneCallback(mainFieldErr);
181180
}
182181
if (mainFieldMappedFile) {
183-
// Not sure why we don't just return the full path? Why strip it?
184-
return doneCallback(
185-
undefined,
186-
Filesystem.removeExtension(mainFieldMappedFile)
187-
);
182+
return doneCallback(undefined, mainFieldMappedFile);
188183
}
189184

190185
// No field in package json was a valid option. Continue with the next path.

src/match-path-sync.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ function findFirstExistingPath(
118118
tryPath.type === "index"
119119
) {
120120
if (fileExists(tryPath.path)) {
121-
// Not sure why we don't just return the full path? Why strip it?
122121
return TryPath.getStrippedPath(tryPath);
123122
}
124123
} else if (tryPath.type === "package") {
@@ -131,8 +130,7 @@ function findFirstExistingPath(
131130
fileExists
132131
);
133132
if (mainFieldMappedFile) {
134-
// Not sure why we don't just return the full path? Why strip it?
135-
return Filesystem.removeExtension(mainFieldMappedFile);
133+
return mainFieldMappedFile;
136134
}
137135
}
138136
} else {

src/try-path.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from "path";
22
import { MappingEntry } from "./mapping-entry";
33
import { dirname } from "path";
4-
import { removeExtension } from "./filesystem";
54

65
export interface TryPath {
76
readonly type: "file" | "extension" | "index" | "package";
@@ -60,17 +59,14 @@ export function getPathsToTry(
6059
return pathsToTry.length === 0 ? undefined : pathsToTry;
6160
}
6261

63-
// Not sure why we don't just return the full found path?
6462
export function getStrippedPath(tryPath: TryPath): string {
6563
return tryPath.type === "index"
6664
? dirname(tryPath.path)
67-
: tryPath.type === "file"
65+
: tryPath.type === "file" ||
66+
tryPath.type === "extension" ||
67+
tryPath.type === "package"
6868
? tryPath.path
69-
: tryPath.type === "extension"
70-
? removeExtension(tryPath.path)
71-
: tryPath.type === "package"
72-
? tryPath.path
73-
: exhaustiveTypeException(tryPath.type);
69+
: exhaustiveTypeException(tryPath.type);
7470
}
7571

7672
export function exhaustiveTypeException(check: never): never {

0 commit comments

Comments
 (0)