Skip to content

Commit

Permalink
Fix the root path (#180)
Browse files Browse the repository at this point in the history
* fix root path

* add test
  • Loading branch information
benevbright authored Mar 13, 2022
1 parent 60829c8 commit 02c37bc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
54 changes: 54 additions & 0 deletions src/__tests__/try-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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", () => {
Expand Down
7 changes: 1 addition & 6 deletions src/try-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ export function getPathsToTry(
absolutePathMappings: ReadonlyArray<MappingEntry>,
requestedModule: string
): ReadonlyArray<TryPath> | undefined {
if (
!absolutePathMappings ||
!requestedModule ||
requestedModule[0] === "." ||
requestedModule[0] === path.sep
) {
if (!absolutePathMappings || !requestedModule || requestedModule[0] === ".") {
return undefined;
}

Expand Down

0 comments on commit 02c37bc

Please sign in to comment.