@@ -26,15 +26,16 @@ export class ServerMockResolver {
26
26
options : { mock : 'spy' | 'factory' | 'auto' } ,
27
27
) : Promise < ServerMockResolution > {
28
28
const { id, fsPath, external } = await this . resolveMockId ( rawId , importer )
29
+ const resolvedUrl = this . normalizeResolveIdToUrl ( { id } ) . url
29
30
30
31
if ( options . mock === 'factory' ) {
31
32
const manifest = getViteDepsManifest ( this . server . config )
32
33
const needsInterop = manifest ?. [ fsPath ] ?. needsInterop ?? false
33
- return { mockType : 'manual' , resolvedId : id , needsInterop }
34
+ return { mockType : 'manual' , resolvedId : id , resolvedUrl , needsInterop }
34
35
}
35
36
36
37
if ( options . mock === 'spy' ) {
37
- return { mockType : 'autospy' , resolvedId : id }
38
+ return { mockType : 'autospy' , resolvedId : id , resolvedUrl }
38
39
}
39
40
40
41
const redirectUrl = findMockRedirect ( this . server . config . root , fsPath , external )
@@ -43,6 +44,7 @@ export class ServerMockResolver {
43
44
mockType : redirectUrl === null ? 'automock' : 'redirect' ,
44
45
redirectUrl,
45
46
resolvedId : id ,
47
+ resolvedUrl,
46
48
}
47
49
}
48
50
@@ -67,10 +69,14 @@ export class ServerMockResolver {
67
69
if ( ! resolved ) {
68
70
return null
69
71
}
72
+ return this . normalizeResolveIdToUrl ( resolved )
73
+ }
74
+
75
+ private normalizeResolveIdToUrl ( resolved : { id : string } ) {
70
76
const isOptimized = resolved . id . startsWith ( withTrailingSlash ( this . server . config . cacheDir ) )
71
77
let url : string
72
78
// normalise the URL to be acceptable by the browser
73
- // https://github.com/vitejs/vite/blob/e833edf026d495609558fd4fb471cf46809dc369 /packages/vite/src/node/plugins/importAnalysis.ts#L335
79
+ // https://github.com/vitejs/vite/blob/14027b0f2a9b01c14815c38aab22baf5b29594bb /packages/vite/src/node/plugins/importAnalysis.ts#L103
74
80
const root = this . server . config . root
75
81
if ( resolved . id . startsWith ( withTrailingSlash ( root ) ) ) {
76
82
url = resolved . id . slice ( root . length )
@@ -86,9 +92,9 @@ export class ServerMockResolver {
86
92
url = resolved . id
87
93
}
88
94
if ( url [ 0 ] !== '.' && url [ 0 ] !== '/' ) {
89
- url = id . startsWith ( VALID_ID_PREFIX )
90
- ? id
91
- : VALID_ID_PREFIX + id . replace ( '\0' , '__x00__' )
95
+ url = resolved . id . startsWith ( VALID_ID_PREFIX )
96
+ ? resolved . id
97
+ : VALID_ID_PREFIX + resolved . id . replace ( '\0' , '__x00__' )
92
98
}
93
99
return {
94
100
id : resolved . id ,
@@ -177,6 +183,7 @@ function withTrailingSlash(path: string): string {
177
183
export interface ServerMockResolution {
178
184
mockType : 'manual' | 'redirect' | 'automock' | 'autospy'
179
185
resolvedId : string
186
+ resolvedUrl : string
180
187
needsInterop ?: boolean
181
188
redirectUrl ?: string | null
182
189
}
0 commit comments