File tree Expand file tree Collapse file tree 8 files changed +176
-349
lines changed Expand file tree Collapse file tree 8 files changed +176
-349
lines changed Original file line number Diff line number Diff line change 43
43
"@rollup/plugin-commonjs" : " ^25.0.7" ,
44
44
"@rollup/plugin-json" : " ^6.0.1" ,
45
45
"@rollup/plugin-node-resolve" : " ^15.2.3" ,
46
- "@types/node" : " ^18.18.9 " ,
46
+ "@types/node" : " ^20.11.5 " ,
47
47
"@types/ws" : " ^8.5.9" ,
48
48
"@vitest/browser" : " workspace:*" ,
49
49
"@vitest/coverage-istanbul" : " workspace:*" ,
Original file line number Diff line number Diff line change @@ -296,7 +296,14 @@ export class ViteNodeRunner {
296
296
const modulePath = cleanUrl ( moduleId )
297
297
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
298
298
const href = pathToFileURL ( modulePath ) . href
299
- const meta = { url : href , env }
299
+ const __filename = fileURLToPath ( href )
300
+ const __dirname = dirname ( __filename )
301
+ const meta = {
302
+ url : href ,
303
+ env,
304
+ filename : __filename ,
305
+ dirname : __dirname ,
306
+ }
300
307
const exports = Object . create ( null )
301
308
Object . defineProperty ( exports , Symbol . toStringTag , {
302
309
value : 'Module' ,
@@ -344,7 +351,6 @@ export class ViteNodeRunner {
344
351
} )
345
352
346
353
Object . assign ( mod , { code : transformed , exports } )
347
- const __filename = fileURLToPath ( href )
348
354
const moduleProxy = {
349
355
set exports ( value ) {
350
356
exportAll ( cjsExports , value )
@@ -388,7 +394,7 @@ export class ViteNodeRunner {
388
394
exports : cjsExports ,
389
395
module : moduleProxy ,
390
396
__filename,
391
- __dirname : dirname ( __filename ) ,
397
+ __dirname,
392
398
} )
393
399
394
400
debugExecute ( __filename )
Original file line number Diff line number Diff line change 173
173
"@types/istanbul-reports" : " ^3.0.4" ,
174
174
"@types/jsdom" : " ^21.1.6" ,
175
175
"@types/micromatch" : " ^4.0.6" ,
176
+ "@types/node" : " ^20.11.5" ,
176
177
"@types/prompts" : " ^2.4.9" ,
177
178
"@types/sinonjs__fake-timers" : " ^8.1.5" ,
178
179
"birpc" : " 0.2.14" ,
Original file line number Diff line number Diff line change @@ -71,11 +71,11 @@ export class ExternalModulesExecutor {
71
71
}
72
72
73
73
public resolveModule = async ( specifier : string , referencer : string ) => {
74
- const identifier = await this . resolve ( specifier , referencer )
74
+ const identifier = this . resolve ( specifier , referencer )
75
75
return await this . createModule ( identifier )
76
76
}
77
77
78
- public async resolve ( specifier : string , parent : string ) {
78
+ public resolve ( specifier : string , parent : string ) {
79
79
for ( const resolver of this . resolvers ) {
80
80
const id = resolver ( specifier , parent )
81
81
if ( id )
Original file line number Diff line number Diff line change @@ -76,6 +76,10 @@ export class CommonjsExecutor {
76
76
return _require
77
77
}
78
78
79
+ static register = ( ) => {
80
+ throw new Error ( `[vitest] "register" is not available when running in Vitest.` )
81
+ }
82
+
79
83
_compile ( code : string , filename : string ) {
80
84
const cjsModule = Module . wrap ( code )
81
85
const script = new vm . Script ( cjsModule , {
Original file line number Diff line number Diff line change 1
1
/* eslint-disable antfu/no-cjs-exports */
2
2
3
3
import type vm from 'node:vm'
4
+ import { fileURLToPath } from 'node:url'
5
+ import { dirname } from 'node:path'
4
6
import type { ExternalModulesExecutor } from '../external-executor'
5
7
import type { VMModule } from './types'
6
8
import { SourceTextModule , SyntheticModule } from './utils'
@@ -62,8 +64,13 @@ export class EsmExecutor {
62
64
importModuleDynamically : this . executor . importModuleDynamically ,
63
65
initializeImportMeta : ( meta , mod ) => {
64
66
meta . url = mod . identifier
65
- meta . resolve = ( specifier : string , importer ?: string ) => {
66
- return this . executor . resolve ( specifier , importer ?? mod . identifier )
67
+ if ( mod . identifier . startsWith ( 'file:' ) ) {
68
+ const filename = fileURLToPath ( mod . identifier )
69
+ meta . filename = filename
70
+ meta . dirname = dirname ( filename )
71
+ }
72
+ meta . resolve = ( specifier : string , importer ?: string | URL ) => {
73
+ return this . executor . resolve ( specifier , importer != null ? importer . toString ( ) : mod . identifier )
67
74
}
68
75
} ,
69
76
} ,
You can’t perform that action at this time.
0 commit comments