Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/try-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { MappingEntry } from "./mapping-entry";
import { dirname } from "path";
import { removeExtension } from "./filesystem";

type TryPathType = "file" | "extension" | "index" | "package"

export interface TryPath {
readonly type: "file" | "extension" | "index" | "package";
readonly type: TryPathType;
readonly path: string;
}

Expand Down Expand Up @@ -52,7 +54,18 @@ export function getPathsToTry(
}
}
}
return pathsToTry.length === 0 ? undefined : pathsToTry;

// Cluster the try paths by type, and order them by most likely to get a hit
const sortWeights: Record<TryPathType, number> = {
file: 3,
extension: 1,
index: 2,
package: 4
}

return pathsToTry.length === 0 ? undefined : pathsToTry.sort((a: TryPath, b: TryPath) =>
sortWeights[a.type] - sortWeights[b.type]
);
}

// Not sure why we don't just return the full found path?
Expand Down