如何一鍵啟動 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
}

相關內容