@@ -31,9 +31,10 @@ import {
31
31
joinUrlSegments ,
32
32
normalizePath ,
33
33
processSrcSetSync ,
34
+ stripBase ,
35
+ unwrapId ,
34
36
wrapId ,
35
37
} from '../../utils'
36
- import type { ModuleGraph } from '../moduleGraph'
37
38
38
39
interface AssetNode {
39
40
start : number
@@ -87,12 +88,12 @@ const processNodeUrl = (
87
88
config : ResolvedConfig ,
88
89
htmlPath : string ,
89
90
originalUrl ?: string ,
90
- moduleGraph ?: ModuleGraph ,
91
+ server ?: ViteDevServer ,
91
92
) => {
92
93
let url = attr . value || ''
93
94
94
- if ( moduleGraph ) {
95
- const mod = moduleGraph . urlToModuleMap . get ( url )
95
+ if ( server ?. moduleGraph ) {
96
+ const mod = server . moduleGraph . urlToModuleMap . get ( url )
96
97
if ( mod && mod . lastHMRTimestamp > 0 ) {
97
98
url = injectQuery ( url , `t=${ mod . lastHMRTimestamp } ` )
98
99
}
@@ -102,14 +103,19 @@ const processNodeUrl = (
102
103
// prefix with base (dev only, base is never relative)
103
104
const fullUrl = path . posix . join ( devBase , url )
104
105
overwriteAttrValue ( s , sourceCodeLocation , fullUrl )
106
+ if ( server ) preTransformRequest ( server , fullUrl , devBase )
105
107
} else if (
106
108
url [ 0 ] === '.' &&
107
109
originalUrl &&
108
110
originalUrl !== '/' &&
109
111
htmlPath === '/index.html'
110
112
) {
111
113
// prefix with base (dev only, base is never relative)
112
- const replacer = ( url : string ) => path . posix . join ( devBase , url )
114
+ const replacer = ( url : string ) => {
115
+ const fullUrl = path . posix . join ( devBase , url )
116
+ if ( server ) preTransformRequest ( server , fullUrl , devBase )
117
+ return fullUrl
118
+ }
113
119
114
120
// #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
115
121
// path will add `/a/` prefix, it will caused 404.
@@ -194,6 +200,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
194
200
node . sourceCodeLocation ! . endOffset ,
195
201
`<script type="module" src="${ modulePath } "></script>` ,
196
202
)
203
+ preTransformRequest ( server ! , modulePath , base )
197
204
}
198
205
199
206
await traverseHtml ( html , filename , ( node ) => {
@@ -213,7 +220,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
213
220
config ,
214
221
htmlPath ,
215
222
originalUrl ,
216
- moduleGraph ,
223
+ server ,
217
224
)
218
225
} else if ( isModule && node . childNodes . length ) {
219
226
addInlineModule ( node , 'js' )
@@ -306,3 +313,15 @@ export function indexHtmlMiddleware(
306
313
next ( )
307
314
}
308
315
}
316
+
317
+ function preTransformRequest ( server : ViteDevServer , url : string , base : string ) {
318
+ if ( ! server . config . server . preTransformRequests ) return
319
+
320
+ url = unwrapId ( stripBase ( url , base ) )
321
+
322
+ // transform all url as non-ssr as html includes client-side assets only
323
+ server . transformRequest ( url ) . catch ( ( e ) => {
324
+ // Unexpected error, log the issue but avoid an unhandled exception
325
+ server . config . logger . error ( e )
326
+ } )
327
+ }
0 commit comments