@@ -191,7 +191,7 @@ export class ViteNodeRunner {
191
191
async directRequest ( id : string , fsPath : string , _callstack : string [ ] ) {
192
192
const callstack = [ ..._callstack , fsPath ]
193
193
194
- const mod = this . moduleCache . get ( fsPath )
194
+ let mod = this . moduleCache . get ( fsPath )
195
195
196
196
const request = async ( dep : string ) => {
197
197
const depFsPath = toFilePath ( normalizeRequestId ( dep , this . options . base ) , this . root )
@@ -222,11 +222,6 @@ export class ViteNodeRunner {
222
222
Object . defineProperty ( request , 'callstack' , { get : ( ) => callstack } )
223
223
224
224
const resolveId = async ( dep : string , callstackPosition = 1 ) => {
225
- // probably means it was passed as variable
226
- // and wasn't transformed by Vite
227
- // or some dependency name was passed
228
- // runner.executeFile('@scope/name')
229
- // runner.executeFile(myDynamicName)
230
225
if ( this . options . resolveId && this . shouldResolveId ( dep ) ) {
231
226
let importer = callstack [ callstack . length - callstackPosition ]
232
227
if ( importer && importer . startsWith ( 'mock:' ) )
@@ -238,14 +233,30 @@ export class ViteNodeRunner {
238
233
return dep
239
234
}
240
235
241
- id = await resolveId ( id , 2 )
242
-
243
236
const requestStubs = this . options . requestStubs || DEFAULT_REQUEST_STUBS
244
237
if ( id in requestStubs )
245
238
return requestStubs [ id ]
246
239
247
240
// eslint-disable-next-line prefer-const
248
- let { code : transformed , externalize } = await this . options . fetchModule ( id )
241
+ let { code : transformed , externalize, file } = await this . options . fetchModule ( id )
242
+
243
+ // in case we resolved fsPath incorrectly, Vite will return the correct file path
244
+ // in that case we need to update cache, so we don't have the same module as different exports
245
+ // 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 )
249
+ this . moduleCache . set ( fsPath , mod )
250
+ if ( mod . promise )
251
+ return mod . promise
252
+ if ( mod . exports )
253
+ return mod . exports
254
+ }
255
+ else {
256
+ this . moduleCache . set ( file , mod )
257
+ }
258
+ }
259
+
249
260
if ( externalize ) {
250
261
debugNative ( externalize )
251
262
const exports = await this . interopedImport ( externalize )
@@ -257,19 +268,16 @@ export class ViteNodeRunner {
257
268
throw new Error ( `[vite-node] Failed to load ${ id } ` )
258
269
259
270
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
260
- const url = pathToFileURL ( fsPath ) . href
271
+ const url = pathToFileURL ( file || fsPath ) . href
261
272
const meta = { url }
262
- const exports : any = Object . create ( null )
273
+ const exports = Object . create ( null )
263
274
Object . defineProperty ( exports , Symbol . toStringTag , {
264
275
value : 'Module' ,
265
276
enumerable : false ,
266
277
configurable : false ,
267
278
} )
268
279
// this prosxy is triggered only on exports.name and module.exports access
269
280
const cjsExports = new Proxy ( exports , {
270
- get ( _ , p , receiver ) {
271
- return Reflect . get ( exports , p , receiver )
272
- } ,
273
281
set ( _ , p , value ) {
274
282
if ( ! Reflect . has ( exports , 'default' ) )
275
283
exports . default = { }
@@ -289,8 +297,7 @@ export class ViteNodeRunner {
289
297
} ,
290
298
} )
291
299
292
- Object . assign ( mod , { code : transformed , exports, evaluated : false } )
293
-
300
+ Object . assign ( mod , { code : transformed , exports } )
294
301
const __filename = fileURLToPath ( url )
295
302
const moduleProxy = {
296
303
set exports ( value ) {
@@ -345,7 +352,7 @@ export class ViteNodeRunner {
345
352
const codeDefinition = `'use strict';async (${ Object . keys ( context ) . join ( ',' ) } )=>{{`
346
353
const code = `${ codeDefinition } ${ transformed } \n}}`
347
354
const fn = vm . runInThisContext ( code , {
348
- filename : fsPath ,
355
+ filename : __filename ,
349
356
lineOffset : 0 ,
350
357
columnOffset : - codeDefinition . length ,
351
358
} )
0 commit comments