简体   繁体   English

如何将来自Browserify捆绑包的堆栈跟踪信息转换为原始源代码位置?

[英]How do I translate stack traces from Browserify bundles to the original source code positions?

I would like to report stack traces from uncaught exceptions in my JavaScript app, but the problem is that the included JavaScript is a Browserify bundle. 我想报告我的JavaScript应用程序中未捕获的异常的堆栈跟踪,但是问题是所包含的JavaScript是Browserify捆绑包。 This means that when I obtain the stack of an exception, it refers to positions within the bundle file, even if the JavaScript bundle contains source maps! 这意味着当我获取异常的堆栈时,即使JavaScript捆绑包含源映射,它也引用捆绑文件中的位置!

How can I translate the file positions within the stack to the original source files? 如何将堆栈中的文件位置转换为原始源文件? I guess it involves some use of source maps? 我猜这涉及对源地图的某种使用?

Below is an example program that prints the stack trace of an exception: 下面是一个示例程序,该程序显示异常的堆栈跟踪:

index.html index.html

<script src="/bundle.js"></script>

index.js index.js

window.onerror = (message, url, line, column, error) => {
  console.log(`An exception was caught for URL ${url}, line ${line}:`, error.stack)
}

const thrower = () => {
  throw new Error(`Catch me if you can`)
}

const callingThrower = () => {
  thrower()
}

callingThrower()

Generate the Bundle 生成捆绑

# The --debug flag enables inline source maps
browserify --debug -o bundle.js index.js

Program Output 节目输出

An exception was caught for URL http://localhost:8000/bundle.js, line 7: Error: Catch me if you can
    at thrower (http://localhost:8000/bundle.js:7:9)
    at callingThrower (http://localhost:8000/bundle.js:11:3)
    at Object.1 (http://localhost:8000/bundle.js:14:1)
    at s (http://localhost:8000/bundle.js:1:254)
    at e (http://localhost:8000/bundle.js:1:425)
    at http://localhost:8000/bundle.js:1:443

Browsers 浏览器

I've tested with Chrome and Firefox on OS X. 我已经在OS X上使用Chrome和Firefox进行了测试。

One thing you can do in your code is to enable the sourcemaps 您可以在代码中做的 件事就是启用源地图

The source maps are the files that tell your browser to translate the line references in your transformed code and the line references in your original code . 源映射是告诉浏览器转换转换代码中的行引用和原始代码中的行引用的文件

Here is a good link to get an idea of the sourceMaps 这是一个很好的链接,可让您了解sourceMaps

Enabling the source maps is very easy in the Browserify .You just have to run Browserify中启用源地图非常容易。您只需运行

browserify --debug main.js -o bundle.js

--debug tells you to include the sourceMaps in the bundle.js but to a disadvantage it makes your bundle twice as large --debug告诉你包括在该sourceMaps bundle.js而是一个缺点它使你的包两倍

However you can tell the users to download this file separately by using the plugin exorcist and it will split the source maps into bundle.js.map 但是你可以告诉用户通过使用插件单独下载该文件驱魔 ,它将源地图分成bundle.js.map

For me I add the browserify options in the gulpfile.js as gruntfile.js as 对我来说,我加在了browserify 选项 gulpfile.jsgruntfile.js

browserify: {
        dist: {
            src: ['src/main.js'],
            dest: 'dist/bundle.js',
            options: {
                debug: true
            }
        }
    },

to enable it or as mentioned in this thread you can use browserifyOptions instead as 要启用它或如本主题所述,您可以使用browserifyOptions代替

options: { browserifyOptions : {} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM