Skip to content

Commit 560bf4c

Browse files
committed
fix: force posix pathing when simulating cjs
1 parent 2034df6 commit 560bf4c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

edge-runtime/lib/cjs.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { Module, createRequire } from 'node:module'
22
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'
45
import { fileURLToPath, pathToFileURL } from 'node:url'
56

7+
const toPosixPath = (path: string) => path.split(sep).join(posixSep)
8+
69
type 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

276279
export 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

Comments
 (0)