evanw / node-source-map-support

Adds source map support to node.js (for stack traces)
MIT License
2.16k stars 223 forks source link

Use sourcemap inside of code executed by eval() #284

Open holtwick opened 3 years ago

holtwick commented 3 years ago

Currently, I use esbuild to compile code and dynamically transpile and execute user JS using eval(), like this:

try {
    if (code) {
      code = `(function(exports) {
        ${code}
        return exports;
      })({})`
      mod = eval(code)
    }
  } catch (err) {
    console.error('Exception:', err)
  }

The code contains a sourcemap.

Is there a way to use node-source-map-support to print the exception's stack using the sourcemap info?

zaydek commented 3 years ago

@holtwick I just ran into this myself. I’m not sure this is a supported use-case. Anyway, I found this article you wrote: https://dev.to/holtwick/esbuild-is-fast-2cjj which helped me a lot personally. For now I’m writing a hidden file to disk in the same root directory so that requires can resolve as expected and then unlinking the same you do when processing is done.

If you want to exchange notes, feel free to HMU https://twitter.com/username_ZAYDEK.