From bf91e917952d62d6c3553466b6b41ddb2e5ced48 Mon Sep 17 00:00:00 2001 From: Bright Lee Date: Thu, 7 Oct 2021 16:27:35 +0200 Subject: [PATCH 1/2] fix root path --- src/try-path.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/try-path.ts b/src/try-path.ts index bf72304..f7ebede 100644 --- a/src/try-path.ts +++ b/src/try-path.ts @@ -20,12 +20,7 @@ export function getPathsToTry( absolutePathMappings: ReadonlyArray, requestedModule: string ): ReadonlyArray | undefined { - if ( - !absolutePathMappings || - !requestedModule || - requestedModule[0] === "." || - requestedModule[0] === path.sep - ) { + if (!absolutePathMappings || !requestedModule || requestedModule[0] === ".") { return undefined; } From 5932f578534cac286db2ae80b233389eba31751d Mon Sep 17 00:00:00 2001 From: Bright Lee Date: Sun, 13 Mar 2022 21:30:10 +0100 Subject: [PATCH 2/2] add test --- src/__tests__/try-path.test.ts | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/__tests__/try-path.test.ts b/src/__tests__/try-path.test.ts index f681362..deb69a0 100644 --- a/src/__tests__/try-path.test.ts +++ b/src/__tests__/try-path.test.ts @@ -10,6 +10,16 @@ describe("mapping-entry", () => { { pattern: "pre/fix/*", paths: [join("/absolute", "base", "url", "foo3")] }, { pattern: "*", paths: [join("/absolute", "base", "url", "foo1")] }, ]; + const abosolutePathMappingsStarstWithSlash = [ + { + pattern: "/opt/*", + paths: [join("/absolute", "src", "aws-layer")], + }, + { + pattern: "*", + paths: [join("/absolute", "src")], + }, + ]; it("should return no paths for relative requested module", () => { const result = getPathsToTry( [".ts", "tsx"], @@ -125,6 +135,50 @@ describe("mapping-entry", () => { }, ]); }); + + it("should resolve paths starting with a slash", () => { + const result = getPathsToTry( + [".ts"], + abosolutePathMappingsStarstWithSlash, + "/opt/utils" + ); + expect(result).toEqual([ + // "opt/*" + { + path: join("/absolute", "src", "aws-layer"), + type: "file", + }, + { + path: join("/absolute", "src", "aws-layer.ts"), + type: "extension", + }, + { + path: join("/absolute", "src", "aws-layer", "package.json"), + type: "package", + }, + { + path: join("/absolute", "src", "aws-layer", "index.ts"), + type: "index", + }, + // "*" + { + path: join("/absolute", "src"), + type: "file", + }, + { + path: join("/absolute", "src.ts"), + type: "extension", + }, + { + path: join("/absolute", "src", "package.json"), + type: "package", + }, + { + path: join("/absolute", "src", "index.ts"), + type: "index", + }, + ]); + }); }); // describe("match-star", () => {