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 4343 "@rollup/plugin-commonjs" : " ^25.0.7" ,
4444 "@rollup/plugin-json" : " ^6.0.1" ,
4545 "@rollup/plugin-node-resolve" : " ^15.2.3" ,
46- "@types/node" : " ^18.18.9 " ,
46+ "@types/node" : " ^20.11.5 " ,
4747 "@types/ws" : " ^8.5.9" ,
4848 "@vitest/browser" : " workspace:*" ,
4949 "@vitest/coverage-istanbul" : " workspace:*" ,
Original file line number Diff line number Diff line change @@ -296,7 +296,14 @@ export class ViteNodeRunner {
296296 const modulePath = cleanUrl ( moduleId )
297297 // disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
298298 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+ }
300307 const exports = Object . create ( null )
301308 Object . defineProperty ( exports , Symbol . toStringTag , {
302309 value : 'Module' ,
@@ -344,7 +351,6 @@ export class ViteNodeRunner {
344351 } )
345352
346353 Object . assign ( mod , { code : transformed , exports } )
347- const __filename = fileURLToPath ( href )
348354 const moduleProxy = {
349355 set exports ( value ) {
350356 exportAll ( cjsExports , value )
@@ -388,7 +394,7 @@ export class ViteNodeRunner {
388394 exports : cjsExports ,
389395 module : moduleProxy ,
390396 __filename,
391- __dirname : dirname ( __filename ) ,
397+ __dirname,
392398 } )
393399
394400 debugExecute ( __filename )
Original file line number Diff line number Diff line change 173173 "@types/istanbul-reports" : " ^3.0.4" ,
174174 "@types/jsdom" : " ^21.1.6" ,
175175 "@types/micromatch" : " ^4.0.6" ,
176+ "@types/node" : " ^20.11.5" ,
176177 "@types/prompts" : " ^2.4.9" ,
177178 "@types/sinonjs__fake-timers" : " ^8.1.5" ,
178179 "birpc" : " 0.2.14" ,
Original file line number Diff line number Diff line change @@ -71,11 +71,11 @@ export class ExternalModulesExecutor {
7171 }
7272
7373 public resolveModule = async ( specifier : string , referencer : string ) => {
74- const identifier = await this . resolve ( specifier , referencer )
74+ const identifier = this . resolve ( specifier , referencer )
7575 return await this . createModule ( identifier )
7676 }
7777
78- public async resolve ( specifier : string , parent : string ) {
78+ public resolve ( specifier : string , parent : string ) {
7979 for ( const resolver of this . resolvers ) {
8080 const id = resolver ( specifier , parent )
8181 if ( id )
Original file line number Diff line number Diff line change @@ -76,6 +76,10 @@ export class CommonjsExecutor {
7676 return _require
7777 }
7878
79+ static register = ( ) => {
80+ throw new Error ( `[vitest] "register" is not available when running in Vitest.` )
81+ }
82+
7983 _compile ( code : string , filename : string ) {
8084 const cjsModule = Module . wrap ( code )
8185 const script = new vm . Script ( cjsModule , {
Original file line number Diff line number Diff line change 11/* eslint-disable antfu/no-cjs-exports */
22
33import type vm from 'node:vm'
4+ import { fileURLToPath } from 'node:url'
5+ import { dirname } from 'node:path'
46import type { ExternalModulesExecutor } from '../external-executor'
57import type { VMModule } from './types'
68import { SourceTextModule , SyntheticModule } from './utils'
@@ -62,8 +64,13 @@ export class EsmExecutor {
6264 importModuleDynamically : this . executor . importModuleDynamically ,
6365 initializeImportMeta : ( meta , mod ) => {
6466 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 )
6774 }
6875 } ,
6976 } ,
You can’t perform that action at this time.
0 commit comments