@@ -321,7 +321,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
321
321
url : string ,
322
322
pos : number ,
323
323
forceSkipImportAnalysis : boolean = false ,
324
- ) : Promise < [ string , string ] > => {
324
+ ) : Promise < [ string , string | null ] > => {
325
325
url = stripBase ( url , base )
326
326
327
327
let importerFile = importer
@@ -355,7 +355,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
355
355
if ( ! resolved || resolved . meta ?. [ 'vite:alias' ] ?. noResolved ) {
356
356
// in ssr, we should let node handle the missing modules
357
357
if ( ssr ) {
358
- return [ url , url ]
358
+ return [ url , null ]
359
359
}
360
360
// fix#9534, prevent the importerModuleNode being stopped from propagating updates
361
361
importerModule . isSelfAccepting = false
@@ -396,32 +396,35 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
396
396
url = injectQuery ( url , versionMatch [ 1 ] )
397
397
}
398
398
}
399
+ }
399
400
401
+ try {
402
+ // delay setting `isSelfAccepting` until the file is actually used (#7870)
403
+ // We use an internal function to avoid resolving the url again
404
+ const depModule = await moduleGraph . _ensureEntryFromUrl (
405
+ unwrapId ( url ) ,
406
+ canSkipImportAnalysis ( url ) || forceSkipImportAnalysis ,
407
+ resolved ,
408
+ )
400
409
// check if the dep has been hmr updated. If yes, we need to attach
401
410
// its last updated timestamp to force the browser to fetch the most
402
411
// up-to-date version of this module.
403
- try {
404
- // delay setting `isSelfAccepting` until the file is actually used (#7870)
405
- // We use an internal function to avoid resolving the url again
406
- const depModule = await moduleGraph . _ensureEntryFromUrl (
407
- unwrapId ( url ) ,
408
- canSkipImportAnalysis ( url ) || forceSkipImportAnalysis ,
409
- resolved ,
410
- )
411
- if ( depModule . lastHMRTimestamp > 0 ) {
412
- url = injectQuery ( url , `t=${ depModule . lastHMRTimestamp } ` )
413
- }
414
- } catch ( e : any ) {
415
- // it's possible that the dep fails to resolve (non-existent import)
416
- // attach location to the missing import
417
- e . pos = pos
418
- throw e
412
+ if (
413
+ environment . config . consumer === 'client' &&
414
+ depModule . lastHMRTimestamp > 0
415
+ ) {
416
+ url = injectQuery ( url , `t=${ depModule . lastHMRTimestamp } ` )
419
417
}
420
-
421
- // prepend base
422
- if ( ! ssr ) url = joinUrlSegments ( base , url )
418
+ } catch ( e : any ) {
419
+ // it's possible that the dep fails to resolve (non-existent import)
420
+ // attach location to the missing import
421
+ e . pos = pos
422
+ throw e
423
423
}
424
424
425
+ // prepend base
426
+ if ( ! ssr ) url = joinUrlSegments ( base , url )
427
+
425
428
return [ url , resolved . id ]
426
429
}
427
430
@@ -547,7 +550,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
547
550
}
548
551
549
552
// normalize
550
- const [ url , resolvedId ] = await normalizeUrl ( specifier , start )
553
+ let [ url , resolvedId ] = await normalizeUrl ( specifier , start )
554
+ resolvedId = resolvedId || url
551
555
552
556
// record as safe modules
553
557
// safeModulesPath should not include the base prefix.
@@ -751,9 +755,40 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
751
755
// normalize and rewrite accepted urls
752
756
const normalizedAcceptedUrls = new Set < string > ( )
753
757
for ( const { url, start, end } of acceptedUrls ) {
754
- const [ normalized ] = await moduleGraph . resolveUrl ( toAbsoluteUrl ( url ) )
758
+ let [ normalized , resolvedId ] = await normalizeUrl ( url , start ) . catch (
759
+ ( ) => [ ] ,
760
+ )
761
+ if ( resolvedId ) {
762
+ const mod = moduleGraph . getModuleById ( resolvedId )
763
+ if ( ! mod ) {
764
+ this . error (
765
+ `module was not found for ${ JSON . stringify ( resolvedId ) } ` ,
766
+ start ,
767
+ )
768
+ return
769
+ }
770
+ normalized = mod . url
771
+ } else {
772
+ try {
773
+ // this fallback is for backward compat and will be removed in Vite 7
774
+ const [ resolved ] = await moduleGraph . resolveUrl ( toAbsoluteUrl ( url ) )
775
+ normalized = resolved
776
+ if ( resolved ) {
777
+ this . warn ( {
778
+ message :
779
+ `Failed to resolve ${ JSON . stringify ( url ) } from ${ importer } .` +
780
+ ' An id should be written. Did you pass a URL?' ,
781
+ pos : start ,
782
+ } )
783
+ }
784
+ } catch {
785
+ this . error ( `Failed to resolve ${ JSON . stringify ( url ) } ` , start )
786
+ return
787
+ }
788
+ }
755
789
normalizedAcceptedUrls . add ( normalized )
756
- str ( ) . overwrite ( start , end , JSON . stringify ( normalized ) , {
790
+ const hmrAccept = normalizeHmrUrl ( normalized )
791
+ str ( ) . overwrite ( start , end , JSON . stringify ( hmrAccept ) , {
757
792
contentOnly : true ,
758
793
} )
759
794
}
0 commit comments