Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
fix: import map with trailing slash (#142)
Browse files Browse the repository at this point in the history
* fix import map with trailing slash

* fix test

* fix test

* fix test

* add tests for trailing slash order

* ensure order of trailing slash imports

* add comment for test

Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com>
  • Loading branch information
shian15810 and axetroy committed Apr 14, 2020
1 parent ef5dadf commit d5ecc7e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions __test__/import_maps/import_map_with_trailing_slash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"imports": {
"demo": "https://example.com/demo/",
"haha": "https://example.com/haha",
"demo/": "https://example.com/demo/",
"haha/": "https://example.com/haha",
"hoho": "https://example.com/hoho/mod.ts",
"hoho/": "https://example.com/hoho/"
}
}
36 changes: 36 additions & 0 deletions core/import_map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,39 @@ test("core / import_map if imports field not an Object", async () => {

expect(importMap.toJSON()).toEqual({});
});

test("core / import_map if imports field ends with a trailing slash", async () => {
const mockWorkspaceDir = path.join(TEST_DIR, "import_maps");

const filepath = path.join(
mockWorkspaceDir,
"import_map_with_trailing_slash.json"
);

const importMap = ImportMap.create(filepath);

const imports = importMap.toJSON();

// invalid entries should be filtered
expect(imports).toEqual({
"demo/": "https://example.com/demo/",
"hoho/": "https://example.com/hoho/",
haha: "https://example.com/haha",
hoho: "https://example.com/hoho/mod.ts",
});

// keys with trailing slash must be sorted at front
expect(Object.keys(imports)).toEqual(["demo/", "hoho/", "haha", "hoho"]);

expect(importMap.resolveModule("hoho")).toEqual(
"https://example.com/hoho/mod.ts"
);

expect(importMap.resolveModule("hoho/mod.ts")).toEqual(
"https://example.com/hoho/mod.ts"
);

expect(importMap.resolveModule("hoho/hoho.ts")).toEqual(
"https://example.com/hoho/hoho.ts"
);
});
11 changes: 11 additions & 0 deletions core/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ export class ImportMap implements ImportMapInterface {
}
}

const imports = Object.entries(importMap.imports)
.filter(
([key, val]) =>
(key.endsWith("/") && val.endsWith("/")) ||
(!key.endsWith("/") && !val.endsWith("/"))
)
.sort(([key1], [key2]) => key2.lastIndexOf("/") - key1.lastIndexOf("/"))
.reduce((imports, [key, value]) => ({ ...imports, [key]: value }), {});

importMap.imports = imports;

return new ImportMap(importMap, importMapFilepath);
}
toJSON() {
Expand Down

0 comments on commit d5ecc7e

Please sign in to comment.