如何一键启动 VS Code 的 Live 服务器和 Chrome 调试器

如何一键启动 VS Code 的 Live 服务器和 Chrome 调试器

对于不使用 npm 或任务运行器的 Web 项目,您能否以某种方式让 Chrome 扩展程序的调试器在开始调试之前启动服务器?我正在使用实时服务器调试扩展程序,如果能够一键启动调试就好了。

例如,我可以以某种方式使用“preLaunchTask”属性吗?

答案1

您希望将“化合物”作为 中的顶级属性launch.json。如下所示:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Server Debug",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/server/server.js",
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "skipFiles": [
                "<node_internals>/**/*.js",
                "${workspaceRoot}/node_modules/**/*.js"
            ]
        },
        {
            "name": "Client Debug",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3000/",
            "webRoot": "${workspaceFolder}/clientSrc",
            "skipFiles": [
                "${workspaceFolder}/node_modules/",
            ]
        }
    ],
    "compounds": [
        {
            "name": "Debug Both",
            "configurations": ["Server Debug", "Client Debug"]
        }
    ]
}

然后,您可以将多个其他运行器组合成一个或多个“复合运行器”。

答案2

我能够使用 来运行它preLaunchTask。现在只需单击一下即可启动调试器和实时服务器。这是我的.vscode文件夹配置文件。

启动.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "port": 9222,
            "request": "launch",
            "type": "pwa-chrome",
            "url": "http://localhost:5500",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "StartServer",
            "postDebugTask": "StopServer"
        }
    ]
}

任务.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "StartServer",
            "type": "process",
            "command": "${input:startServer}"
        },
        {
            "label": "StopServer",
            "type": "process",
            "command": "${input:stopServer}"
        }
    ],
    "inputs": [
        {
            "id": "startServer",
            "type": "command",
            "command": "extension.liveServer.goOnline"
        },
        {
            "id": "stopServer",
            "type": "command",
            "command": "extension.liveServer.goOffline"
        }
    ]
}

settings.json(工作区设置)

{
    "liveServer.settings.ChromeDebuggingAttachment": true,
    "liveServer.settings.CustomBrowser": "chrome",
    "liveServer.settings.host": "localhost",
    "liveServer.settings.NoBrowser": true
}

相关内容