11import { Module , createRequire } from 'node:module'
22import vm from 'node:vm'
3- import { join , dirname } from 'node:path/posix'
3+ import { sep } from 'node:path'
4+ import { join , dirname , sep as posixSep } from 'node:path/posix'
45import { fileURLToPath , pathToFileURL } from 'node:url'
56
7+ const toPosixPath = ( path : string ) => path . split ( sep ) . join ( posixSep )
8+
69type RegisteredModule = {
710 source : string
811 loaded : boolean
@@ -231,7 +234,7 @@ function seedCJSModuleCacheAndReturnTarget(matchedModule: RegisteredModule, pare
231234 lineOffset : 0 ,
232235 displayErrors : true ,
233236 } )
234- const modRequire = createRequire ( pathToFileURL ( filepath ) )
237+ const modRequire = createRequire ( pathToFileURL ( filepath , { windows : false } ) )
235238 compiled ( mod . exports , modRequire , mod , filepath , dirname ( filepath ) )
236239 }
237240 mod . loaded = matchedModule . loaded = true
@@ -274,7 +277,7 @@ function tryMatchingWithIndex(target: string) {
274277}
275278
276279export function registerCJSModules ( baseUrl : URL , modules : Map < string , string > ) {
277- const basePath = dirname ( fileURLToPath ( baseUrl ) )
280+ const basePath = dirname ( toPosixPath ( fileURLToPath ( baseUrl , { windows : false } ) ) )
278281
279282 for ( const [ filename , source ] of modules . entries ( ) ) {
280283 const target = join ( basePath , filename )
@@ -291,7 +294,7 @@ export function registerCJSModules(baseUrl: URL, modules: Map<string, string>) {
291294
292295 if ( isRelative ) {
293296 // only handle relative require paths
294- const requireFrom = args ?. [ 1 ] ?. filename
297+ const requireFrom = toPosixPath ( args ?. [ 1 ] ?. filename )
295298
296299 target = join ( dirname ( requireFrom ) , args [ 0 ] )
297300 }
0 commit comments