Skip to content

Commit

Permalink
Merge pull request #162 from aiotter/master
Browse files Browse the repository at this point in the history
fix: Fails to load import map with module URL starting with "./"
  • Loading branch information
oscarotero committed Jan 12, 2022
2 parents 5f0d950 + 44d7a4e commit c7bde37
Showing 1 changed file with 32 additions and 3 deletions.
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

0 comments on commit c7bde37

Please sign in to comment.