最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Source map for a dynamically created function - Stack Overflow

programmeradmin1浏览0评论

JavaScript allows you to create new functions at runtime with the new Function(...) or eval. Is it possible for a function created in this way to set something like a source map? That is, set the name of the source file from which it was allegedly loaded, as well as different source line numbers.

If there is no general solution, then there may be particular solutions for specific JavaScript engines.

JavaScript allows you to create new functions at runtime with the new Function(...) or eval. Is it possible for a function created in this way to set something like a source map? That is, set the name of the source file from which it was allegedly loaded, as well as different source line numbers.

If there is no general solution, then there may be particular solutions for specific JavaScript engines.

Share Improve this question asked Mar 24, 2018 at 9:13 kiv_applekiv_apple 6715 silver badges18 bronze badges 7
  • "...as well as different source line numbers." huh? – zer00ne Commented Mar 24, 2018 at 9:20
  • I know this does not solve the question but aren't new Function and eval the no-no features? – Jankapunkt Commented Mar 24, 2018 at 9:32
  • I meant redefining the original line numbers of code passed to new Function or eval. Let's say a function to create via new Function was generated from some other source code and I want to keep references to the old code. If you create a new JavaScript file, you can specify the source map, but what if I want to do this pletely in runtime without writing to disk (and in the case of the browser it's pletely impossible)? – kiv_apple Commented Mar 24, 2018 at 9:36
  • Source Maps are piled so that would require writing to disk I think... – zer00ne Commented Mar 24, 2018 at 9:43
  • @zer00ne that doesn't make sense- you can pile to a string. – mseddon Commented Mar 24, 2018 at 12:49
 |  Show 2 more ments

1 Answer 1

Reset to default 9

For eval'd code you can simply add:

//# sourceMappingURL=<url>
//# sourceURL=<filename.js>

To the bottom of the code you are evaluating. For the sourceMappingURL, you will send in a data url, since naturally you don't have an existing source map hosted somewhere and you will be dynamically generating it.

Place your original source in the sourcesContent field in the source map, set the file field in the source map to whatever you set in sourceURL, and you should be good to go.

My current attempts at getting this to work in Function constructors is failing, which is a shame- in this case, in both Edge and Chrome, the original source is correctly listed, but it appears that the line number information is broken (the instruction pointer always points to the last line). I am currently investigating this. However, the source map spec does allude to Function constructors containing sourcemaps:

If the generated code is being evaluated as a string with the eval() function or via new Function(), then the source origin will be the page’s origin.

This leads me to believe either I am doing something silly, or there is not great support for this feature.

发布评论

评论列表(0)

  1. 暂无评论