@@ -4,7 +4,7 @@ import vm from 'vm'
4
4
import { dirname , extname , isAbsolute , resolve } from 'pathe'
5
5
import { isNodeBuiltin } from 'mlly'
6
6
import createDebug from 'debug'
7
- import { isPrimitive , mergeSlashes , normalizeModuleId , normalizeRequestId , slash , toFilePath } from './utils'
7
+ import { cleanUrl , isPrimitive , normalizeModuleId , normalizeRequestId , slash , toFilePath } from './utils'
8
8
import type { HotContext , ModuleCache , ViteNodeRunnerOptions } from './types'
9
9
import { extractSourceMap } from './source-map'
10
10
@@ -221,39 +221,43 @@ export class ViteNodeRunner {
221
221
222
222
Object . defineProperty ( request , 'callstack' , { get : ( ) => callstack } )
223
223
224
- const resolveId = async ( dep : string , callstackPosition = 1 ) => {
224
+ const resolveId = async ( dep : string , callstackPosition = 1 ) : Promise < [ dep : string , id : string | undefined ] > => {
225
225
if ( this . options . resolveId && this . shouldResolveId ( dep ) ) {
226
- let importer = callstack [ callstack . length - callstackPosition ]
226
+ let importer : string | undefined = callstack [ callstack . length - callstackPosition ]
227
+ if ( importer && ! dep . startsWith ( '.' ) )
228
+ importer = undefined
227
229
if ( importer && importer . startsWith ( 'mock:' ) )
228
230
importer = importer . slice ( 5 )
229
- const { id } = await this . options . resolveId ( dep , importer ) || { }
230
- dep = id && isAbsolute ( id ) ? mergeSlashes ( `/@fs/ ${ id } ` ) : id || dep
231
+ const resolved = await this . options . resolveId ( normalizeRequestId ( dep ) , importer )
232
+ return [ dep , resolved ?. id ]
231
233
}
232
234
233
- return dep
235
+ return [ dep , undefined ]
234
236
}
235
237
238
+ const [ dep , resolvedId ] = await resolveId ( id , 2 )
239
+
236
240
const requestStubs = this . options . requestStubs || DEFAULT_REQUEST_STUBS
237
241
if ( id in requestStubs )
238
242
return requestStubs [ id ]
239
243
240
244
// eslint-disable-next-line prefer-const
241
- let { code : transformed , externalize, file } = await this . options . fetchModule ( id )
245
+ let { code : transformed , externalize } = await this . options . fetchModule ( resolvedId || dep )
242
246
243
247
// in case we resolved fsPath incorrectly, Vite will return the correct file path
244
248
// in that case we need to update cache, so we don't have the same module as different exports
245
249
// but we ignore fsPath that has custom query, because it might need to be different
246
- if ( file && ! fsPath . includes ( '?' ) && fsPath !== file ) {
247
- if ( this . moduleCache . has ( file ) ) {
248
- mod = this . moduleCache . get ( file )
250
+ if ( resolvedId && ! fsPath . includes ( '?' ) && fsPath !== resolvedId ) {
251
+ if ( this . moduleCache . has ( resolvedId ) ) {
252
+ mod = this . moduleCache . get ( resolvedId )
249
253
this . moduleCache . set ( fsPath , mod )
250
254
if ( mod . promise )
251
255
return mod . promise
252
256
if ( mod . exports )
253
257
return mod . exports
254
258
}
255
259
else {
256
- this . moduleCache . set ( file , mod )
260
+ this . moduleCache . set ( resolvedId , mod )
257
261
}
258
262
}
259
263
@@ -267,8 +271,10 @@ export class ViteNodeRunner {
267
271
if ( transformed == null )
268
272
throw new Error ( `[vite-node] Failed to load ${ id } ` )
269
273
274
+ const file = cleanUrl ( resolvedId || fsPath )
275
+ // console.log('file', file)
270
276
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
271
- const url = pathToFileURL ( file || fsPath ) . href
277
+ const url = pathToFileURL ( file ) . href
272
278
const meta = { url }
273
279
const exports = Object . create ( null )
274
280
Object . defineProperty ( exports , Symbol . toStringTag , {
0 commit comments