From 1a986e89f4e7e4157478a911e4b9e803fe62176d Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 27 Nov 2024 17:21:24 +0100 Subject: [PATCH] fix: mapped directories in import map not resolved (#25) * fix: mapped directories in import map not resolved * chore: simplify check --- src/resolver.ts | 5 +++-- tests/fixture/alias-mapped.ts | 3 +++ tests/fixture/deno.json | 3 ++- tests/fixture/mapped/dep.ts | 1 + tests/fixture/mapped/main.ts | 3 +++ tests/fixture/vite.config.ts | 1 + tests/plugin.test.ts | 4 ++++ 7 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 tests/fixture/alias-mapped.ts create mode 100644 tests/fixture/mapped/dep.ts create mode 100644 tests/fixture/mapped/main.ts diff --git a/src/resolver.ts b/src/resolver.ts index 8fadfdf..7750c81 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1,5 +1,6 @@ import { exec } from "node:child_process"; import process from "node:process"; +import { fileURLToPath } from "node:url"; import { execAsync } from "./utils.js"; export type DenoMediaType = @@ -149,8 +150,8 @@ export async function resolveViteSpecifier( // Check if we need to continue resolution id = found.code.specifier; - if (!id.startsWith("http://") && !id.startsWith("https://")) { - return found.code.specifier; + if (id.startsWith("file://")) { + return fileURLToPath(id); } } diff --git a/tests/fixture/alias-mapped.ts b/tests/fixture/alias-mapped.ts new file mode 100644 index 0000000..1af08e6 --- /dev/null +++ b/tests/fixture/alias-mapped.ts @@ -0,0 +1,3 @@ +import { value } from "mapped/main.ts"; + +console.log(value); diff --git a/tests/fixture/deno.json b/tests/fixture/deno.json index 139bcab..868b9e5 100644 --- a/tests/fixture/deno.json +++ b/tests/fixture/deno.json @@ -10,6 +10,7 @@ "@std/path": "jsr:@std/path@^1.0.6", "import-map-alias": "./alias-target.ts", "preact": "npm:preact@^10.24.0", - "preact-http": "https://esm.sh/preact" + "preact-http": "https://esm.sh/preact", + "mapped/": "mapped/" } } diff --git a/tests/fixture/mapped/dep.ts b/tests/fixture/mapped/dep.ts new file mode 100644 index 0000000..f78ea6b --- /dev/null +++ b/tests/fixture/mapped/dep.ts @@ -0,0 +1 @@ +export const dep = "it works"; diff --git a/tests/fixture/mapped/main.ts b/tests/fixture/mapped/main.ts new file mode 100644 index 0000000..8ba95b5 --- /dev/null +++ b/tests/fixture/mapped/main.ts @@ -0,0 +1,3 @@ +import { dep } from "./dep.ts"; + +export const value = dep; diff --git a/tests/fixture/vite.config.ts b/tests/fixture/vite.config.ts index 619ddcd..64686c6 100644 --- a/tests/fixture/vite.config.ts +++ b/tests/fixture/vite.config.ts @@ -8,6 +8,7 @@ export default defineConfig({ formats: ["es"], entry: { importMapAlias: "alias.ts", + importMapAliasMapped: "alias-mapped.ts", importMapNpm: "npm.ts", importMapJsr: "jsr.ts", importMapHttp: "http.ts", diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index 60952c3..5b95f66 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -23,6 +23,10 @@ describe("Deno plugin", () => { await runTest(`importMapAlias.js`); }); + it("resolves alias mapped", async () => { + await runTest(`importMapAliasMapped.js`); + }); + it("resolves npm:", async () => { await runTest(`importMapNpm.js`); });