Skip to content

Commit

Permalink
fix(core): invalid URL error on relative imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jul 29, 2024
1 parent 85fb9d7 commit 76d9955
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
24 changes: 15 additions & 9 deletions core/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,28 @@ async function fromImportMap(
): Promise<DependencyRef<"import_map">[]> {
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;
}

Expand Down
26 changes: 26 additions & 0 deletions core/refs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 76d9955

Please sign in to comment.