1
1
import { Module , createRequire } from 'node:module'
2
2
import 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'
4
5
import { fileURLToPath , pathToFileURL } from 'node:url'
5
6
7
+ const toPosixPath = ( path : string ) => path . split ( sep ) . join ( posixSep )
8
+
6
9
type RegisteredModule = {
7
10
source : string
8
11
loaded : boolean
@@ -231,7 +234,7 @@ function seedCJSModuleCacheAndReturnTarget(matchedModule: RegisteredModule, pare
231
234
lineOffset : 0 ,
232
235
displayErrors : true ,
233
236
} )
234
- const modRequire = createRequire ( pathToFileURL ( filepath ) )
237
+ const modRequire = createRequire ( pathToFileURL ( filepath , { windows : false } ) )
235
238
compiled ( mod . exports , modRequire , mod , filepath , dirname ( filepath ) )
236
239
}
237
240
mod . loaded = matchedModule . loaded = true
@@ -274,7 +277,7 @@ function tryMatchingWithIndex(target: string) {
274
277
}
275
278
276
279
export function registerCJSModules ( baseUrl : URL , modules : Map < string , string > ) {
277
- const basePath = dirname ( fileURLToPath ( baseUrl ) )
280
+ const basePath = dirname ( toPosixPath ( fileURLToPath ( baseUrl , { windows : false } ) ) )
278
281
279
282
for ( const [ filename , source ] of modules . entries ( ) ) {
280
283
const target = join ( basePath , filename )
@@ -291,7 +294,7 @@ export function registerCJSModules(baseUrl: URL, modules: Map<string, string>) {
291
294
292
295
if ( isRelative ) {
293
296
// only handle relative require paths
294
- const requireFrom = args ?. [ 1 ] ?. filename
297
+ const requireFrom = toPosixPath ( args ?. [ 1 ] ?. filename )
295
298
296
299
target = join ( dirname ( requireFrom ) , args [ 0 ] )
297
300
}
0 commit comments