Skip to content

Commit

Permalink
feat: rewrite error stack for inline scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 23, 2021
1 parent d52219c commit 7357aeb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/index.mjs
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions test/eval-err.mjs
@@ -0,0 +1,5 @@
async function test() {
throw new Error('Something went wrong in eval-err module!')
}

await test()
2 changes: 2 additions & 0 deletions test/eval.mjs
Expand Up @@ -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))

0 comments on commit 7357aeb

Please sign in to comment.