Using “preLaunchTasks” and Naming a Task in Visual Studio Code

前端 未结 6 1586
野的像风
野的像风 2021-01-31 14:32

According to the documentation, it is possible to launch a program before debugging:

To launch a task before the start of each debug session, set the

6条回答
  •  生来不讨喜
    2021-01-31 15:09

    So, if it's still relevant, or if someone finds this thread with the same problem, I've just figured it out how it works:

    In the tasks.json, you need to create a 'tasks' array - code hint will help you with that - which holds an array of objects. Inside an object, you can have the 'taskName' key-value pair.

    Example:

    {
        "version": "0.1.0",
        "command": "npm",
        "isShellCommand": true,
        "args": ["run-script", "webpack"],
        "showOutput": "always",
        "tasks": [
            { 
                "taskName": "runwebpack",
                "suppressTaskName": true
            }
        ]
    }
    

    In my case, I had to run the npm run-script webpack command before running my project. In the launch.json file, the "preLaunchTask": "runwebpack" will work now.

    Note: the suppressTaskName is true in my example. Omitting it, or setting it to false will result in VS Code appending the taskName after the command.

    A more general approach would be something like this:

    {
        "version": "0.1.0",
        "command": "npm",
        "isShellCommand": true,
        "args": ["run-script"],
        "showOutput": "always",
        "tasks": [
            { "taskName": "webpack" }
        ]
    }
    

    With the latter example, you can extend the tasks array with other scripts to be run also.

    Hint for my usage: npm run-script fetches what to do from the package.json file's scripts object.

    Edit: this works with VS Code 1.3.1

提交回复
热议问题