From 0e8398f0892f1a705cff6c4f8721e7b3f71dc948 Mon Sep 17 00:00:00 2001 From: axetroy Date: Fri, 27 Mar 2020 19:53:16 +0800 Subject: [PATCH] fix: importmap not work when set to a relative path. close #103 --- __test__/import_maps/import_map.json | 3 ++- __test__/import_maps/src/relative/path/to/mod.ts | 0 core/import_map.test.ts | 7 +++++++ core/import_map.ts | 9 +++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 __test__/import_maps/src/relative/path/to/mod.ts diff --git a/__test__/import_maps/import_map.json b/__test__/import_maps/import_map.json index 5b1d4f7..e2fec3e 100644 --- a/__test__/import_maps/import_map.json +++ b/__test__/import_maps/import_map.json @@ -1,5 +1,6 @@ { "imports": { - "demo/": "https://example.com/demo/" + "demo/": "https://example.com/demo/", + "core": "./src/relative/path/to" } } diff --git a/__test__/import_maps/src/relative/path/to/mod.ts b/__test__/import_maps/src/relative/path/to/mod.ts new file mode 100644 index 0000000..e69de29 diff --git a/core/import_map.test.ts b/core/import_map.test.ts index 460e6da..2db4f14 100644 --- a/core/import_map.test.ts +++ b/core/import_map.test.ts @@ -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( @@ -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); diff --git a/core/import_map.ts b/core/import_map.ts index 212c3f3..b549b48 100644 --- a/core/import_map.ts +++ b/core/import_map.ts @@ -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; } }