@@ -68,38 +68,41 @@ export function htmlScriptToVirtual(
68
68
{
69
69
name : `md:dev-html-prerender` ,
70
70
apply : `build` ,
71
- async transform ( code , id ) {
72
- if (
73
- server == null
74
- || ! id . endsWith ( `.html` )
75
- ) {
76
- return
77
- }
78
- const { document } = parseHTML ( code )
79
- // Replace inline script with virtual module served via dev server.
80
- // Extension CSP blocks inline scripts, so that's why we're pulling them out.
81
- const promises : Promise < void > [ ] = [ ]
82
- const inlineScripts = document . querySelectorAll ( `script[src^=http]` )
83
- inlineScripts . forEach ( async ( script ) => {
84
- promises . push ( new Promise < void > ( ( resolve ) => {
85
- const url = script . getAttribute ( `src` ) ?? ``
86
- doFetch ( url ) . then ( ( textContent ) => {
87
- const hash = murmurHash ( textContent )
88
- inlineScriptContents [ hash ] = textContent
89
- script . setAttribute ( `src` , `${ server . origin } /@id/${ virtualInlineScript } ?${ hash } ` )
90
- if ( script . hasAttribute ( `id` ) ) {
91
- script . setAttribute ( `type` , `module` )
71
+ transformIndexHtml : {
72
+ order : `post` ,
73
+ async handler ( html ) {
74
+ if ( server == null ) {
75
+ return html
76
+ }
77
+ const { document } = parseHTML ( html )
78
+ // Replace inline script with virtual module served via dev server.
79
+ // Extension CSP blocks inline scripts, so that's why we're pulling them out.
80
+ const promises : Promise < void > [ ] = [ ]
81
+ const inlineScripts = document . querySelectorAll ( `script[src^=http]` )
82
+ inlineScripts . forEach ( async ( script ) => {
83
+ promises . push ( new Promise < void > ( ( resolve ) => {
84
+ const url = script . getAttribute ( `src` ) ?? ``
85
+ if ( url ?. startsWith ( `http://localhost` ) ) {
86
+ resolve ( )
87
+ return
92
88
}
93
- resolve ( )
94
- } )
95
- } ) )
96
- } )
97
- await Promise . all ( promises )
98
- const newHtml = document . toString ( )
99
- config . logger . debug ( `transform ${ id } ` )
100
- config . logger . debug ( `Old HTML:\n${ code } ` )
101
- config . logger . debug ( `New HTML:\n${ newHtml } ` )
102
- return newHtml
89
+ doFetch ( url ) . then ( ( textContent ) => {
90
+ const hash = murmurHash ( textContent )
91
+ inlineScriptContents [ hash ] = textContent
92
+ script . setAttribute ( `src` , `${ server . origin } /@id/${ virtualInlineScript } ?${ hash } ` )
93
+ if ( script . hasAttribute ( `id` ) ) {
94
+ script . setAttribute ( `type` , `module` )
95
+ }
96
+ resolve ( )
97
+ } )
98
+ } ) )
99
+ } )
100
+ await Promise . all ( promises )
101
+ const newHtml = document . toString ( )
102
+ config . logger . debug ( `\nhtmlScriptToVirtual Old HTML:\n${ html } ` )
103
+ config . logger . debug ( `\nhtmlScriptToVirtual New HTML:\n${ newHtml } ` )
104
+ return newHtml
105
+ } ,
103
106
} ,
104
107
} ,
105
108
{
@@ -140,7 +143,7 @@ export function htmlScriptToLocal(
140
143
name : `md:build-html-prerender` ,
141
144
apply : `build` ,
142
145
transformIndexHtml : {
143
- order : `pre ` ,
146
+ order : `post ` ,
144
147
async handler ( html ) {
145
148
const { document } = parseHTML ( html )
146
149
const promises : Promise < void > [ ] = [ ]
@@ -156,7 +159,10 @@ export function htmlScriptToLocal(
156
159
}
157
160
const textContent = await doFetch ( url )
158
161
const hash = murmurHash ( textContent )
159
- const jsName = url . match ( / \/ ( [ ^ / ] + ) \. j s $ / ) ?. [ 1 ] ?? `.js`
162
+ let jsName = url . match ( / \/ ( [ ^ / ] + ) \. j s $ / ) ?. [ 1 ] ?? `.js`
163
+ if ( url . indexOf ( `?` ) > 0 ) {
164
+ jsName = `${ url . substring ( url . indexOf ( `?` ) + 1 ) } .js`
165
+ }
160
166
const fileName = `${ jsName . split ( `.` ) [ 0 ] } -${ hash } .js`
161
167
// write to file
162
168
const outFile = path . resolve ( wxt . config . outDir , `./${ fileName } ` )
@@ -194,8 +200,8 @@ export function htmlScriptToLocal(
194
200
}
195
201
await Promise . all ( promises )
196
202
const newHtml = document . toString ( )
197
- wxt . config . logger . debug ( `Old HTML:\n${ html } ` )
198
- wxt . config . logger . debug ( `New HTML:\n${ newHtml } ` )
203
+ wxt . config . logger . debug ( `\nhtmlScriptToLocal Old HTML:\n${ html } ` )
204
+ wxt . config . logger . debug ( `\nhtmlScriptToLocal New HTML:\n${ newHtml } ` )
199
205
return newHtml
200
206
} ,
201
207
} ,
0 commit comments