From 76d99552004cde1b4b50cd38509ff4352564d0b6 Mon Sep 17 00:00:00 2001 From: hasundue Date: Mon, 29 Jul 2024 16:50:13 +0900 Subject: [PATCH] fix(core): `invalid URL` error on relative imports --- core/refs.ts | 24 +++++++++++++++--------- core/refs_test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/core/refs.ts b/core/refs.ts index c4e53437..ca976cc8 100644 --- a/core/refs.ts +++ b/core/refs.ts @@ -90,22 +90,28 @@ async function fromImportMap( ): Promise[]> { const json = await readImportMapJson(path); - const refs: DependencyRef<"import_map">[] = Object.entries(json.imports ?? {}) - .map(( - [key, value], - ) => ({ + const refs: DependencyRef<"import_map">[] = []; + + Object.entries(json.imports ?? {}).forEach(([key, value]) => { + const dep = tryParse(value); + if (!dep) return; + refs.push({ dependency: parse(value), source: { path, kind: "import_map", key }, - })); + }); + }); Object.entries(json.scopes ?? {}).forEach(([scope, imports]) => - refs.push( - ...Object.entries(imports).map(([key, value]) => ({ + Object.entries(imports).map(([key, value]) => { + const dep = tryParse(value); + if (!dep) return; + refs.push({ dependency: parse(value), source: { path, kind: "import_map", key, scope }, - } as DependencyRef<"import_map">)), - ) + }); + }) ); + return refs; } diff --git a/core/refs_test.ts b/core/refs_test.ts index c7bac376..acf442bc 100644 --- a/core/refs_test.ts +++ b/core/refs_test.ts @@ -53,6 +53,17 @@ describe("collect", () => { ]); }); + it("should ignore relative imports in an ES module", async () => { + await Deno.writeTextFile( + "a.ts", + dedent` + import { assert } from "./assert.ts"; + `, + ); + const actual = await collect("a.ts"); + assertEquals(actual, []); + }); + it("should collect dependencies from an import map", async () => { await Deno.writeTextFile( "a.json", @@ -140,6 +151,21 @@ describe("collect", () => { }, ]); }); + + it("should ignore relative imports in an import map", async () => { + await Deno.writeTextFile( + "a.json", + dedent` + { + "imports": { + "assert": "./assert.ts", + } + } + `, + ); + const actual = await collect("a.json"); + assertEquals(actual, []); + }); }); describe("rewrite", () => {