2017-04-17 133 views
0

我正在尝试将我的默认grunt任务附加到vscode调试器。所以我期望的工作流程是启动调试器,它运行默认的grunt任务,然后我可以使用vscode调试器在我的代码中放置断点。我启动的JSON文件看起来像这样。将grunt附加到VSCODE调试器

{ 
    "version": "0.2.0", 
    "configurations": [ 
     { 
      "type": "node", 
      "request": "launch", 
      "name": "Launch Program", 
      "program": "${workspaceRoot}/node_modules/grunt/lib/grunt", 
      "args": ["run"], 
      "cwd": "${workspaceRoot}", 
      "preLaunchTask": null, 
      "runtimeExecutable": null, 
      "runtimeArgs": [ 
      "--nolazy" 
      ], 
      "env": { 
       "NODE_ENV": "production" 
      } 
     }, 
     { 
      "type": "node", 
      "request": "attach", 
      "name": "Attach to Process", 
      "port": 5858 
     } 
    ] 
} 

但我得到一个错误,无法启动程序 'node_modules /咕噜/ lib目录/咕噜';设置'outfiles'属性可能有帮助

+1

你的咕噜任务是做什么的?它只是运行应用程序还是有编译步骤?如果它是第一个,那么对于任务跑步者已经有很多类似的问题,例如, http://stackoverflow.com/questions/203/what-is-the-proper-way-to-debug-an-npm-script-using-vscode/43212281#43212281。 如果您的'run'任务也编译源代码,那么您可以使用'preLaunchTask'选项集来定义一个Node.js调试配置。 –

+0

@jsynowiec它的后者,你能指导我如何实现这个 – lboyel

回答

1

如果要调试编译代码,则必须在tasks.json中定义构建任务,然后在launch.json配置中将其指定为preLaunchTask

还记得增加您的构建配置,以便它输出源地图。使用源映射,可以单步执行或在原始源中设置断点。

您需要配置tasks.json文件中的任务(位于工作区.vscode文件夹下)。如果您还没有一个tasks.json文件,运行任务:配置从命令面板任务运行行动( + + 在Windows/Linux的P在MacOS或F1)将提供你有一组模板可供选择。从列表中选择Grunt,它会生成一个文件看起来应该像这样的:

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    "version": "0.1.0", 
    "command": "grunt", 
    "isShellCommand": true, 
    "args": ["--no-color"], 
    "showOutput": "always" 
} 

现在您可以定义生成任务:

{ 
    ... 
    "showOutput": "always", 
    "tasks": [ 
    { 
     "taskName": "build", 
     "args": [], 
     "isBuildCommand": true 
    } 
    ] 

假设你的咕噜建立你的应用程序从src/app.jsdist ,你可以定义你的启动配置是这样的:

{ 
    "type": "node", 
    "request": "launch", 
    "name": "Launch", 
    "program": "${workspaceRoot}/src/app.js", 
    "sourceMaps": true, 
    "outFiles": ["${workspaceRoot}/dist/**/*.js"], 
    "preLaunchTask": "build" 
} 

您可以在VS代码文档阅读更多 - Node.js Debugging in VS Code

+0

谢谢,这是非常有用的 – lboyel