@@ -8,16 +8,22 @@ import { getAlephPkgUri } from "./config.ts";
8
8
9
9
await initLolHtml ( decodeLolHtmlWasm ( ) ) ;
10
10
11
+ type LoadOptions = {
12
+ isDev : boolean ;
13
+ ssr ?: { suspense ?: boolean } ;
14
+ hmrWebSocketUrl ?: string ;
15
+ } ;
16
+
11
17
// laod the `index.html`
12
18
// - fix relative url to absolute url of `src` and `href`
13
19
// - add `./framework/core/hmr.ts` when in `development` mode
14
20
// - add `./framework/core/nomodule.ts`
15
21
// - check the `<head>` and `<body>` elements
16
22
// - check the `<ssr-body>` element if the ssr is enabled
17
23
// - add `data-suspense` attribute to `<body>` if using suspense ssr
18
- export async function loadAndFixIndexHtml ( isDev : boolean , ssr ?: { suspense ?: boolean } ) : Promise < Uint8Array > {
24
+ export async function loadAndFixIndexHtml ( options : LoadOptions ) : Promise < Uint8Array > {
19
25
const { html, hasSSRBody } = await loadIndexHtml ( ) ;
20
- return fixIndexHtml ( html , { isDev , ssr , hasSSRBody } ) ;
26
+ return fixIndexHtml ( html , hasSSRBody , options ) ;
21
27
}
22
28
23
29
async function loadIndexHtml ( ) : Promise < { html : Uint8Array ; hasSSRBody : boolean } > {
@@ -84,11 +90,8 @@ async function loadIndexHtml(): Promise<{ html: Uint8Array; hasSSRBody: boolean
84
90
}
85
91
}
86
92
87
- function fixIndexHtml (
88
- html : Uint8Array ,
89
- options : { isDev : boolean ; ssr ?: { suspense ?: boolean } ; hasSSRBody : boolean } ,
90
- ) : Uint8Array {
91
- const { isDev, ssr, hasSSRBody } = options ;
93
+ function fixIndexHtml ( html : Uint8Array , hasSSRBody : boolean , options : LoadOptions ) : Uint8Array {
94
+ const { isDev, ssr, hmrWebSocketUrl } = options ;
92
95
const alephPkgUri = getAlephPkgUri ( ) ;
93
96
const chunks : Uint8Array [ ] = [ ] ;
94
97
const rewriter = new HTMLRewriter ( "utf8" , ( chunk : Uint8Array ) => chunks . push ( chunk ) ) ;
@@ -157,6 +160,15 @@ function fixIndexHtml(
157
160
} ,
158
161
} ) ;
159
162
}
163
+ if ( isDev && hmrWebSocketUrl ) {
164
+ rewriter . on ( "head" , {
165
+ element ( el : Element ) {
166
+ el . append ( `<script>window.__hmrWebSocketUrl=${ JSON . stringify ( hmrWebSocketUrl ) } ;</script>` , {
167
+ html : true ,
168
+ } ) ;
169
+ } ,
170
+ } ) ;
171
+ }
160
172
161
173
try {
162
174
rewriter . write ( html ) ;
0 commit comments