Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 9ce2a98

Browse files
committed
Resolve html link/script url with the import maps
1 parent a11734e commit 9ce2a98

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

server/html.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import type { Comment, DocumentEnd, Element } from "https://deno.land/x/lol_html
33
import initLolHtml, { HTMLRewriter } from "https://deno.land/x/[email protected]/mod.js";
44
import decodeLolHtmlWasm from "https://deno.land/x/[email protected]/wasm.js";
55
import util from "../lib/util.ts";
6-
import { getAlephPkgUri, getDeploymentId, toLocalPath } from "./helpers.ts";
6+
import { applyImportMap, getAlephPkgUri, getDeploymentId, toLocalPath } from "./helpers.ts";
7+
import type { ImportMap } from "./types.ts";
78

89
await initLolHtml(decodeLolHtmlWasm());
910

1011
type LoadOptions = {
1112
isDev: boolean;
13+
importMap: ImportMap;
1214
ssr?: { suspense?: boolean };
1315
hmrWebSocketUrl?: string;
1416
};
@@ -90,7 +92,7 @@ async function loadIndexHtml(): Promise<{ html: Uint8Array; hasSSRBody: boolean
9092
}
9193

9294
function fixIndexHtml(html: Uint8Array, hasSSRBody: boolean, options: LoadOptions): Uint8Array {
93-
const { isDev, ssr, hmrWebSocketUrl } = options;
95+
const { isDev, importMap, ssr, hmrWebSocketUrl } = options;
9496
const alephPkgUri = getAlephPkgUri();
9597
const chunks: Uint8Array[] = [];
9698
const rewriter = new HTMLRewriter("utf8", (chunk: Uint8Array) => chunks.push(chunk));
@@ -100,15 +102,19 @@ function fixIndexHtml(html: Uint8Array, hasSSRBody: boolean, options: LoadOption
100102
element: (el: Element) => {
101103
let href = el.getAttribute("href");
102104
if (href) {
105+
href = applyImportMap(href, importMap);
103106
const isHttpUrl = util.isLikelyHttpURL(href);
104107
if (!isHttpUrl) {
105108
href = util.cleanPath(href);
106109
if (deployId) {
107110
href += (href.includes("?") ? "&v=" : "?v=") + deployId;
108111
}
109112
el.setAttribute("href", href);
113+
} else {
114+
href = toLocalPath(href);
110115
}
111-
if (href.endsWith(".css") && !isHttpUrl && isDev) {
116+
el.setAttribute("href", href);
117+
if (isDev && !isHttpUrl && href.split("?")[0].endsWith(".css")) {
112118
const specifier = `.${href}`;
113119
el.setAttribute("data-module-id", specifier);
114120
el.after(
@@ -125,10 +131,16 @@ function fixIndexHtml(html: Uint8Array, hasSSRBody: boolean, options: LoadOption
125131
rewriter.on("script", {
126132
element: (el: Element) => {
127133
let src = el.getAttribute("src");
128-
if (src && !util.isLikelyHttpURL(src)) {
129-
src = util.cleanPath(src);
130-
if (deployId) {
131-
src += (src.includes("?") ? "&v=" : "?v=") + deployId;
134+
if (src) {
135+
src = applyImportMap(src, importMap);
136+
if (!util.isLikelyHttpURL(src)) {
137+
src = util.cleanPath(src);
138+
if (deployId) {
139+
src += (src.includes("?") ? "&v=" : "?v=") + deployId;
140+
}
141+
el.setAttribute("src", src);
142+
} else {
143+
src = toLocalPath(src);
132144
}
133145
el.setAttribute("src", src);
134146
}

server/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export const serve = (options: ServerOptions = {}) => {
292292
try {
293293
indexHtml = await loadAndFixIndexHtml({
294294
isDev,
295+
importMap: await importMapPromise,
295296
ssr: typeof ssr === "function" ? {} : ssr,
296297
hmrWebSocketUrl: options.hmrWebSocketUrl,
297298
});

0 commit comments

Comments
 (0)