简体   繁体   English

如何在源图中使用断点(Chrome DevTools)

[英]How to use breakpoints in sourcemaps (Chrome DevTools)

I have added some stuff like Babel and closure compiler to my hobby project only to find out Chrome doesn't hit breakpoints in my mapped files. 我已经在我的爱好项目中添加了一些像Babel和闭包编译器这样的东西,但却发现Chrome没有在我的映射文件中点击断点。

Here is a snippet that reproduces the problem: 这是一个重现问题的片段:

 function test(){console.log("Break me")}test(); //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxTQUFBLElBQUEsR0FBQTtBQUNBLFlBQUEsR0FBQSxDQUFBLFVBQUE7QUFDQTs7QUFFQSIsImZpbGUiOiJtYWluLm1pbi5qcyIsInNvdXJjZXNDb250ZW50IjpbImZ1bmN0aW9uIHRlc3QoKSB7XHJcbiAgICBjb25zb2xlLmxvZygnQnJlYWsgbWUnKTtcclxufVxyXG5cclxudGVzdCgpOyJdfQ== 

Chrome picks up the mapped files but breakpoints are not hit here, Chrome会选择已映射的文件,但此处不会触发断点,
kind of defeating the purpose of adding sourcemaps... 打败添加源图的目的......

文件树 映射文件

What can I do to hit the breakpoints on my map? 如何在地图上点击断点?

Chrome version 50.0.2661.94 m, using external map files Chrome版本50.0.2661.94米,使用外部地图文件

EDIT: 编辑:
It seems to be a problem with my sourcemap when I'm piping code through Babel / closure... 当我通过Babel / closure管道代码时,我的sourcemap似乎有问题...
(so please ignore the snippet, the sourcemap seems corrupt) (所以请忽略该片段,源图似乎已损坏)

gulpfile.js gulpfile.js

.pipe(sourcemaps.init())
.pipe(concat("main.min.js"))
.pipe(babel({ presets: ["es2015"] }))
.pipe(closure({ compilation_level: "SIMPLE_OPTIMIZATIONS" }))
.pipe(sourcemaps.write("."))

Using gulp-sourcemaps , gulp-babel , gulp-closure-compiler-service 使用gulp-sourcemapsgulp-sourcemaps gulp-babelgulp-sourcemaps gulp-closure-compiler-service

I've not had a lot of practical experience with source maps but I'll have a go answering. 我没有很多关于源地图的实践经验,但我会去回答。 Feel free to enlighten me if I go wrong here. 如果我在这里出错,请随时赐教。

I think the issue is that the breakpoints you have added in the Chrome debugger are not actually where you think you added them because of the changes the compilers have made to your code. 我认为问题在于,您在Chrome调试器中添加的断点实际上并不是您认为添加它们的地方,因为编译器对您的代码所做的更改。

For example, a simple case I've seen is where you are doing a string concatenation in multiple statements. 例如,我看到的一个简单的例子是你在多个语句中进行字符串连接。 A minifier will merge them into a single statement using the Comma Operator . 缩小器将使用逗号运算符将它们合并为单个语句。 That means if you put a breakpoint on one of the statements, it will jump to another line. 这意味着如果你在其中一个语句上放置一个断点,它将跳转到另一行。

In your case, it's possible the breakpoint could be in a completely different part of your code that isn't getting executed under the conditions you are currently running it. 在您的情况下,断点可能位于代码的完全不同的部分,而不是在您当前运行它的条件下执行。

Uglify seems to have config property that can help in the combined statement case - using the following: Uglify似乎有配置属性,可以在合并的语句中帮助 - 使用以下内容:

compress: {
    sequences: false
}

This stops the compiler from merging the multiple statements into one. 这会阻止编译器将多个语句合并为一个。 I'm not sure what kind of optimisations Closure makes and what options you have, but obviously these would be tradeoffs on performance optimisations offered by the compiler. 我不确定Closure做了什么样的优化以及你有什么选择,但显然这些将是编译器提供的性能优化的权衡。

There is also this issue logged in the Babel issue tracker that could also be the cause or a contributing factor to your problem. 在Babel问题跟踪器中记录此问题也可能是导致问题的原因或影响因素。

Source maps are relatively new and there's lot's of work currently being done to improve them. 源地图相对较新,目前正在进行大量改进工作。 In Chrome Canary, the nightly build project, the debugger can see the original variable names. 在Chrome Canary(每晚构建项目)中,调试器可以看到原始变量名称。

I don't know how helpful this post is but hopefully something I said here is of use to someone. 我不知道这篇文章有多大帮助,但希望我在这里说的对某人有用。

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

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