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

Commit

Permalink
fix: importmap not work when set to a relative path. close #103
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 27, 2020
1 parent 1809e70 commit 0e8398f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion __test__/import_maps/import_map.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"imports": {
"demo/": "https://example.com/demo/"
"demo/": "https://example.com/demo/",
"core": "./src/relative/path/to"
}
}
Empty file.
7 changes: 7 additions & 0 deletions core/import_map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test("core / import_map", async () => {

expect(ImportMap.create(validImportMapFilepath).toJSON()).toEqual({
"demo/": "https://example.com/demo/",
core: "./src/relative/path/to",
});

expect(
Expand All @@ -22,6 +23,12 @@ test("core / import_map", async () => {
ImportMap.create(validImportMapFilepath).resolveModule("demo1/mod.ts")
).toEqual("demo1/mod.ts");

expect(
ImportMap.create(validImportMapFilepath).resolveModule("core/mod.ts")
).toEqual(
path.join(mockWorkspaceDir, "src", "relative", "path", "to", "mod.ts")
);

expect(ImportMap.create(invalidImportMapFilepath).toJSON()).toEqual({});

const importMap = await ImportMap.create(validImportMapFilepath);
Expand Down
9 changes: 9 additions & 0 deletions core/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export class ImportMap implements ImportMapInterface {
const reg = new RegExp("^" + escapeRegExp(prefix));
if (reg.test(moduleName)) {
moduleName = moduleName.replace(reg, mapModule);

// if module name is a relative path
if (moduleName.startsWith(".") && this.filepath) {
moduleName = path.resolve(
path.dirname(this.filepath),
normalizeFilepath(moduleName)
);
}

return moduleName;
}
}
Expand Down

0 comments on commit 0e8398f

Please sign in to comment.