使用Typescript + VSCode调试Node.js异步/等待

dnp*_*dnp 6 javascript node.js typescript visual-studio-code

我检查了以下答案:

使用nodejs 7异步等待

如何在Visual Studio代码中调试异步/等待?

但是都没有解决我的问题。

我希望能够使用Node.js v7.4.0从VSCode调试本机Async / Await,而无需使用可怕的Typescript编译版本。我能够让Typescript输出正确的代码,即没有__awaiter等。但是,一旦我尝试调试代码,就会出现所有已编译的状态机代码!因此,我可以调试代码,而不仅仅是我想调试的代码。无论如何,有什么方法可以防止已调试的代码具有已编译的状态机代码?

这是我的配置文件:

tsconfig.json

{
    "compilerOptions": {
        "target": "es2017",

        "module": "commonjs",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "lib",
        "noUnusedParameters": false,
        "noUnusedLocals": false,
        "skipLibCheck": true
        //"importHelpers": true
    },
        "exclude": [
        "node_modules"
    ]
}
Run Code Online (Sandbox Code Playgroud)

launch.json

{
    "name": "Launch",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/node_modules/jest-cli/bin/jest.js",
    "stopOnEntry": false,
    "cwd": "${workspaceRoot}",
    //"preLaunchTask": "tsc",
    "runtimeExecutable": null,
    "args": [
        "--runInBand"
    ],
    "runtimeArgs": [
        "--harmony-async-await",
        "--no-deprecation"
    ],
    "env": {
        "NODE_ENV": "development"
    },
    "console": "integratedTerminal",
    "sourceMaps": true,
    "outFiles": [
        "${workspaceRoot}/{lib}/**/*.js"
    ],
    "skipFiles": [
        "node_modules/**/*.js",
        "lib/**/*.js"
    ]
}
Run Code Online (Sandbox Code Playgroud)

为了进一步说明我的意思,这是输出的javascript中的代码片段:

let handler = subscription.messageSubscription.handler;
debugger;
await handler(message.message, context);
Run Code Online (Sandbox Code Playgroud)

但是,在调试时,它看起来像这样:

case 4:
    handler = subscription.messageSubscription.handler;
    debugger;
    return [4 /*yield*/, handler(message.message, context)];
case 5:
Run Code Online (Sandbox Code Playgroud)

Man*_*ner 3

我添加"smartStep": truelaunch.json并根据需要调试等待/异步模式(使用 Node v8.4.0)。

这是我的 launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceRoot}/src/main.ts",
      "cwd": "${workspaceRoot}",
      "console": "integratedTerminal",
      "outFiles": [ "${workspaceRoot}/dist/*.js" ],
      "sourceMaps": true,
      "preLaunchTask": "build",
      "smartStep": true
    }
  ]
Run Code Online (Sandbox Code Playgroud)

}

有关更多详细信息,请参阅https://code.visualstudio.com/updates/vApril#_smart-code-stepping

这不是一个完美的解决方案,因为使用smartStep您无法调试到库代码,因此如果您想调试到库中,您必须手动注释掉此选项。也许有人知道如何解决这个小不便。