[英]Use Visual Studio Code to debug Node.js dependency which is “require”d
I want to use VSC to develop some extensions to an existing project. 我想使用VSC为现有项目开发一些扩展。 In particular UglifyJS3. 特别是UglifyJS3。
So I create a test script, in which I call into the module to test and debug my changes. 因此,我创建了一个测试脚本,在其中调用模块来测试和调试更改。 The code there is simply: 那里的代码很简单:
var UglifyJS = require("../tools/node");
var result = UglifyJS.minify(code, ...)
This works while debugging/single-stepping. 在调试/单步执行时有效。 However VSC already fails to resolve the minify function which is exported by UglifyJS. 但是,VSC已经无法解析由UglifyJS导出的minify函数。
I am also not able to simply set breakpoints in the UglifyJS files and have them triggered. 我也不能简单地在UglifyJS文件中设置断点并触发它们。 VSC shows an error on startup about not being able to resolve the breakpoint. VSC在启动时显示有关无法解析断点的错误。
Looking at ../tools/node.js
I see code like: 看../tools/node.js
我看到类似的代码:
var UglifyJS = exports;
require.resolve("../lib/utils.js")
...
require.resolve("../lib/minify.js")
require.resolve("./exports.js")
with exports.js
containing exports["minify"] = minify;
与exports.js
包含exports["minify"] = minify;
. 。
Is there anything I have missed? 有什么我想念的吗? How can I make debugging (and optionally IntelliSense) work? 如何使调试(以及可选的IntelliSense)工作?
I found the reason: 我找到了原因:
First clue was that the code showed up in the debugger as <eval>
and in a VM<some number>
tab. 第一个线索是代码在调试器中显示为<eval>
和在VM<some number>
选项卡中。 So somehow the code was not actually read from file but dynamically evaluated. 因此,代码实际上并没有从文件中读取,而是经过了动态评估。
What happens is (to a reduced example): 发生了什么(作为简化示例):
var fn = require.resolve("<file>"); // Resolve the filename
var code = fs.readFileSync(fn, "utf8");; // Read the file contents
new Function("exports", code)(exports); // Create a new function containing the files code and execute it
The last bit is a bit more cryptic in code and the new Function
was new to me (no pun intended). 最后一点是代码更神秘, new Function
对我来说是new Function
(无双关语)。 It essentially boils down to an optimized eval
with strict-mode compatibility: https://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/ 它实质上可以归结为具有严格模式兼容性的优化eval
: https : //whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-评估代码仅限于该代码本地/
Hence: The code executed is actually eval'd code and not the files itself. 因此:执行的代码实际上是评估的代码,而不是文件本身。
Solution: Either live with it or change it upstream to "clean" modules. 解决方案:要么使用它,要么将其更改为上游的“清洁”模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.