Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fails to load import map with module URL starting with "./" #162

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { Exception } from "./core/errors.ts";
const { join } = posix;
const baseUrl = new URL(".", import.meta.url).href;

type SpecifierMap = Record<string, string>;

interface ImportMap {
imports: Record<string, string>;
scopes?: Record<string, Record<string, string>>;
imports: SpecifierMap;
scopes?: Record<string, SpecifierMap>;
}

export function checkDenoVersion(): void {
Expand Down Expand Up @@ -38,6 +40,32 @@ async function ensureUrl(maybeUrl: string) {
}
}

/**
* If a given specifier map has relative paths,
* resolve them with a given base URL.
*/
function resolveSpecifierMap(specifierMap: SpecifierMap, baseUrl: URL) {
return Object.fromEntries(
Object.entries(specifierMap).map((
[key, value],
) => [key, new URL(value, baseUrl).href]),
);
}

/**
* If any specifier maps in a given import map have relative paths,
* resolve it with a given base URL.
*/
function resolveImportMap(importMap: ImportMap, baseUrl: URL) {
const imports = resolveSpecifierMap(importMap.imports, baseUrl);
const scopes = Object.fromEntries(
Object.entries(importMap.scopes ?? []).map((
[scopeSpecifier, specifierMap],
) => [scopeSpecifier, resolveSpecifierMap(specifierMap, baseUrl)]),
);
return { imports, scopes };
}

/**
* Return a data url with the import map of Lume
* Optionally merge it with a custom import map from the user
Expand All @@ -56,7 +84,8 @@ export async function getImportMap(mapFile?: string) {
const url = await ensureUrl(mapFile);
const file = await (await fetch(url)).text();
const parsedMap = JSON.parse(file) as ImportMap;
map.imports = { ...map.imports, ...parsedMap.imports };
const resolvedMap = resolveImportMap(parsedMap, url);
map.imports = { ...map.imports, ...resolvedMap.imports };
map.scopes = parsedMap.scopes;
} catch (cause) {
throw new Exception("Unable to load the import map file", {
Expand Down