Skip to content

Commit dd6a383

Browse files
committed
fix: add additional context on CJS module compilation failures to point to specific modules
1 parent 9ea923b commit dd6a383

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

edge-runtime/lib/cjs.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ function seedCJSModuleCacheAndReturnTarget(matchedModule: RegisteredModule, pare
2929
require.cache[filename] = mod
3030

3131
const wrappedSource = `(function (exports, require, module, __filename, __dirname) { ${source}\n});`
32-
const compiled = vm.runInThisContext(wrappedSource, {
33-
filename,
34-
lineOffset: 0,
35-
displayErrors: true,
36-
})
37-
compiled(mod.exports, createRequire(pathToFileURL(filename)), mod, filename, dirname(filename))
38-
mod.loaded = matchedModule.loaded = true
32+
try {
33+
const compiled = vm.runInThisContext(wrappedSource, {
34+
filename,
35+
lineOffset: 0,
36+
displayErrors: true,
37+
})
38+
compiled(mod.exports, createRequire(pathToFileURL(filename)), mod, filename, dirname(filename))
39+
mod.loaded = matchedModule.loaded = true
40+
} catch (error) {
41+
throw new Error(`Failed to compile CJS module: ${filename}`, { cause: error })
42+
}
3943

4044
return filename
4145
}

0 commit comments

Comments
 (0)