diff --git a/lib/index.mjs b/lib/index.mjs index 6a40cdd..7b61b54 100644 --- a/lib/index.mjs +++ b/lib/index.mjs @@ -119,7 +119,11 @@ export async function loadModule (id, opts = {}) { export async function evalModule (code, opts = {}) { const transformed = await transformModule(code, opts) - return import(toDataURL(transformed, opts)) + const dataURL = toDataURL(transformed, opts) + return import(dataURL).catch((err) => { + err.stack = err.stack.replace(new RegExp(dataURL, 'g'), opts.url || '_mlly_eval_.mjs') + throw err + }) } export async function transformModule (code, opts) { diff --git a/test/eval-err.mjs b/test/eval-err.mjs new file mode 100644 index 0000000..5ff5491 --- /dev/null +++ b/test/eval-err.mjs @@ -0,0 +1,5 @@ +async function test() { + throw new Error('Something went wrong in eval-err module!') +} + +await test() diff --git a/test/eval.mjs b/test/eval.mjs index c46063d..49dbcca 100644 --- a/test/eval.mjs +++ b/test/eval.mjs @@ -12,3 +12,5 @@ await evalModule(` await loadModule('./hello.mjs', { url: import.meta.url }) console.log(await loadModule('../package.json', { url: import.meta.url }).then(r => r.default.name)) + +await loadModule('./eval-err.mjs', { url: import.meta.url }).catch(e => console.error(e))