@@ -3,12 +3,14 @@ import type { Comment, DocumentEnd, Element } from "https://deno.land/x/lol_html
3
3
import initLolHtml , { HTMLRewriter } from "https://deno.land/x/[email protected] /mod.js" ;
4
4
import decodeLolHtmlWasm from "https://deno.land/x/[email protected] /wasm.js" ;
5
5
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" ;
7
8
8
9
await initLolHtml ( decodeLolHtmlWasm ( ) ) ;
9
10
10
11
type LoadOptions = {
11
12
isDev : boolean ;
13
+ importMap : ImportMap ;
12
14
ssr ?: { suspense ?: boolean } ;
13
15
hmrWebSocketUrl ?: string ;
14
16
} ;
@@ -90,7 +92,7 @@ async function loadIndexHtml(): Promise<{ html: Uint8Array; hasSSRBody: boolean
90
92
}
91
93
92
94
function fixIndexHtml ( html : Uint8Array , hasSSRBody : boolean , options : LoadOptions ) : Uint8Array {
93
- const { isDev, ssr, hmrWebSocketUrl } = options ;
95
+ const { isDev, importMap , ssr, hmrWebSocketUrl } = options ;
94
96
const alephPkgUri = getAlephPkgUri ( ) ;
95
97
const chunks : Uint8Array [ ] = [ ] ;
96
98
const rewriter = new HTMLRewriter ( "utf8" , ( chunk : Uint8Array ) => chunks . push ( chunk ) ) ;
@@ -100,15 +102,19 @@ function fixIndexHtml(html: Uint8Array, hasSSRBody: boolean, options: LoadOption
100
102
element : ( el : Element ) => {
101
103
let href = el . getAttribute ( "href" ) ;
102
104
if ( href ) {
105
+ href = applyImportMap ( href , importMap ) ;
103
106
const isHttpUrl = util . isLikelyHttpURL ( href ) ;
104
107
if ( ! isHttpUrl ) {
105
108
href = util . cleanPath ( href ) ;
106
109
if ( deployId ) {
107
110
href += ( href . includes ( "?" ) ? "&v=" : "?v=" ) + deployId ;
108
111
}
109
112
el . setAttribute ( "href" , href ) ;
113
+ } else {
114
+ href = toLocalPath ( href ) ;
110
115
}
111
- if ( href . endsWith ( ".css" ) && ! isHttpUrl && isDev ) {
116
+ el . setAttribute ( "href" , href ) ;
117
+ if ( isDev && ! isHttpUrl && href . split ( "?" ) [ 0 ] . endsWith ( ".css" ) ) {
112
118
const specifier = `.${ href } ` ;
113
119
el . setAttribute ( "data-module-id" , specifier ) ;
114
120
el . after (
@@ -125,10 +131,16 @@ function fixIndexHtml(html: Uint8Array, hasSSRBody: boolean, options: LoadOption
125
131
rewriter . on ( "script" , {
126
132
element : ( el : Element ) => {
127
133
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 ) ;
132
144
}
133
145
el . setAttribute ( "src" , src ) ;
134
146
}
0 commit comments