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 1 commit
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 a value which starts with "./",
* resolve it with given base URL.
*/
function resolveSpecifierMap(specifierMap: SpecifierMap, baseUrl: URL) {
return Object.fromEntries(
Object.entries(specifierMap).map((
[key, value],
) => [key, value.startsWith("./") ? new URL(value, baseUrl).href : value]),
aiotter marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
* If any specifier maps in the given import map has a value which starts with "./",
* resolve it with 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