在 VS Code 中使用监视模式运行 preLaunchTask

chn*_*ing 7 plugins task watch typescript visual-studio-code

我正在使用适用于 Chrome 的 VS Code 调试器来调试我的 Web 应用程序。在launch.json所有配置所在的位置,任务是这样定义的

{
    "name": "Debug App in Chrome",
    "type": "chrome",
    "request": "launch",
    "url": "http://localhost:8080/",
    "webRoot": "${workspaceRoot}",
    "sourceMaps": true
}
Run Code Online (Sandbox Code Playgroud)

效果很好。在特定地址上打开浏览器,调试工具工作正常。

但我希望在打开浏览器之前有一个preLaunchTask可以构建我的项目并创建服务器的服务器,这样当浏览器打开时,该地址/端口上就有一个实际的服务器。所以我添加了这一行"preLaunchTask": "server:dev"。该任务构建项目,启动监视模式并创建服务器。但是当我添加该任务时,预启动任务已成功执行,但浏览器根本无法打开。

我猜测这是因为该--watch标志实际上使任务保持“活动状态”(对于服务器的创建可能也是如此)并且 VS CodepreLaunchTask在启动主任务之前等待完全终止?

我的问题是:有没有办法告诉 VS Code 预启动任务实际上不会终止,因此它不应该等待它完全结束才开始主任务?或者我无法仅用 1 个按钮完成所有这些操作(构建、观看、启动服务器和打开浏览器)?

Kyl*_*Mit 3

Yes you can - Just add isBackground:true to your task definition:

{
  // ...
  "isBackground": true
}
Run Code Online (Sandbox Code Playgroud)

Note: If you don't care about parsing terminal output, you can ignore the warning about not having a problemMatcher.

According to this section in the docs:

Can a background task be used as a prelaunchTask in launch.json?

Yes. Since a background task will run until killed, a background task on its own has no signal that it has "completed". To use a background task as a prelaunchTask, you must add an appropriate background problemMatcher to the background task so that there is a way for the task system and debug system to know that the task "finished".

Your task could be:

{
  // ...
  "isBackground": true
}
Run Code Online (Sandbox Code Playgroud)

Note: The $tsc-watch is a background problem matcher, as is required for a background task.

You can then use the task as a prelaunchTask in your launch.json file:

{
  "type": "npm",
  "script": "watch",
  "problemMatcher": "$tsc-watch",
  "isBackground": true
}
Run Code Online (Sandbox Code Playgroud)

For more on background tasks, go to Background / watching tasks.

See Also: How to make vscode not wait for finishing a preLaunchTask?