Skip to content

Commit

Permalink
fix: regard file extensions during path resolution (dividab#133)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
katywings committed Jul 24, 2020
1 parent 7204659 commit 4d57893
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/filesystem.ts
Expand Up @@ -81,7 +81,3 @@ export function fileExistsAsync(
callback2(undefined, stats ? stats.isFile() : false);
});
}

export function removeExtension(path: string): string {
return path.substring(0, path.lastIndexOf(".")) || path;
}
7 changes: 1 addition & 6 deletions src/match-path-async.ts
Expand Up @@ -148,7 +148,6 @@ function findFirstExistingPath(
return doneCallback(err);
}
if (exists) {
// Not sure why we don't just return the full path? Why strip it?
return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
}
if (index === tryPaths.length - 1) {
Expand Down Expand Up @@ -180,11 +179,7 @@ function findFirstExistingPath(
return doneCallback(mainFieldErr);
}
if (mainFieldMappedFile) {
// Not sure why we don't just return the full path? Why strip it?
return doneCallback(
undefined,
Filesystem.removeExtension(mainFieldMappedFile)
);
return doneCallback(undefined, mainFieldMappedFile);
}

// No field in package json was a valid option. Continue with the next path.
Expand Down
4 changes: 1 addition & 3 deletions src/match-path-sync.ts
Expand Up @@ -118,7 +118,6 @@ function findFirstExistingPath(
tryPath.type === "index"
) {
if (fileExists(tryPath.path)) {
// Not sure why we don't just return the full path? Why strip it?
return TryPath.getStrippedPath(tryPath);
}
} else if (tryPath.type === "package") {
Expand All @@ -131,8 +130,7 @@ function findFirstExistingPath(
fileExists
);
if (mainFieldMappedFile) {
// Not sure why we don't just return the full path? Why strip it?
return Filesystem.removeExtension(mainFieldMappedFile);
return mainFieldMappedFile;
}
}
} else {
Expand Down
12 changes: 4 additions & 8 deletions src/try-path.ts
@@ -1,7 +1,6 @@
import * as path from "path";
import { MappingEntry } from "./mapping-entry";
import { dirname } from "path";
import { removeExtension } from "./filesystem";

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

// Not sure why we don't just return the full found path?
export function getStrippedPath(tryPath: TryPath): string {
return tryPath.type === "index"
? dirname(tryPath.path)
: tryPath.type === "file"
: tryPath.type === "file" ||
tryPath.type === "extension" ||
tryPath.type === "package"
? tryPath.path
: tryPath.type === "extension"
? removeExtension(tryPath.path)
: tryPath.type === "package"
? tryPath.path
: exhaustiveTypeException(tryPath.type);
: exhaustiveTypeException(tryPath.type);
}

export function exhaustiveTypeException(check: never): never {
Expand Down

0 comments on commit 4d57893

Please sign in to comment.