From 8474ef27a144d029d5715b7b838f888f8eb6c61e Mon Sep 17 00:00:00 2001 From: Max schwenk Date: Thu, 8 Dec 2022 10:14:55 -0500 Subject: [PATCH] Better trypath ordering --- src/try-path.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/try-path.ts b/src/try-path.ts index f6819fb..c11f89c 100644 --- a/src/try-path.ts +++ b/src/try-path.ts @@ -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; } @@ -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 = { + 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?