Skip to content

Commit

Permalink
hot: improve import map validating
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Feb 3, 2024
1 parent fcc9053 commit d43b172
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions hot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,27 +294,43 @@ function parseImportMap() {
return importMap;
}
const script = doc.querySelector("script[type=importmap]");
let json = null;
if (script) {
try {
json = parse(script.textContent!);
const json = parse(script.textContent!);
if (isObject(json)) {
const { imports, scopes } = json;
if (isObject(imports)) {
validateImports(imports);
importMap.imports = imports;
}
if (isObject(scopes)) {
validateScopes(scopes);
importMap.scopes = scopes;
}
}
} catch (err) {
console.error("Invalid importmap", err[kMessage]);
}
}
if (isObject(json)) {
const { imports, scopes } = json;
for (const k in imports) {
const url = imports[k];
if (url) {
importMap.imports[k] = url;
}
return importMap;
}

function validateScopes(imports: Record<string, unknown>) {
for (const [k, v] of Object.entries(imports)) {
if (isObject(v)) {
validateImports(v);
} else {
delete imports[k];
}
if (isObject(scopes)) {
importMap.scopes = scopes;
}
}

function validateImports(imports: Record<string, unknown>) {
for (const [k, v] of Object.entries(imports)) {
if (!v || typeof v !== "string") {
delete imports[k];
}
}
return importMap;
}

/** create a cache proxy object. */
Expand Down

0 comments on commit d43b172

Please sign in to comment.