如何使用tasks.json在Visual Studio Code中链接任务?

28

我一直在查阅Visual Studio Code的文档,以了解如何将多个连续任务添加到tasks.json文件中。

tasks数组只允许创建相同命令的不同参数。在此示例中,命令是echo

{
    "version": "0.1.0",
    "command": "echo",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always",
    "echoCommand": true,
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "hello",
            "args": ["Hello World"]
        },
        {
            "taskName": "bye",
            "args": ["Good Bye"]
        }
    ]
}

tasks.json允许连续执行多个任务吗?例如,tsc后跟uglify


1
在最新的VS Code版本中,我不再使用tasks.json了。你可以把你的命令放到package.jsonscripts标签下。如果你只需要两三个连续的命令,你可以使用prepost标签。如果你的构建过程变得更加复杂,你可以使用gulp或webpack。 - Kokodoko
2个回答

36

dependsOn 功能在 版本 1.10.0 中发布。例如,我正在使用它来编译和运行 TypeScript 中的单文件脚本:

{
    "version": "2.0.0",
    "tasks": [
        {
            "command": "tsc -p ${cwd}/2017-play",
            "label": "tsc-compile",
            "type": "shell"
        },
        {
            "command": "node ${cwd}/2017-play/build/${fileBasenameNoExtension}.js",
            "label": "node-exec",
            "type": "shell",
            "dependsOn": [
                "tsc-compile"
            ],
            "problemMatcher": []
        }
    ]
}

这是一个巨大的改进!但我仍然认为微软的文档在如何使用 tasks.json 方面非常不清晰。到这个时候,我已经放弃了,只是使用 npm scriptswebpack - Kokodoko

5
这是一个工作示例,它使用shell脚本运行tcs构建并将源代码复制到另一个文件夹中。这基于StackOverflow上的各种帖子和此处找到的文档:https://code.visualstudio.com/updates/v1_10#_more-work-on-terminal-runner
你也可以创建一个tasks.json文件,其中包含两个任务,第二个任务依赖于第一个任务,如Ben Creasy的帖子所示,当调用第二个任务时,两个任务都会执行。我需要能够执行一个、另一个或两个任务。非常感谢Ben,我在看到这篇文章之前一直很难找到解决方案。
顺便说一下,当包含一个shell文件时,命令是相对于项目文件夹而不是脚本所在的文件夹运行的。
{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
  {
   "type": "typescript",
   "tsconfig": "tsconfig.json",
   "problemMatcher": [
    "$tsc"
   ],
   "group": "build",
   "identifier": "build"
  },
  {
   "label": "Copy files",
   "type": "shell",
   "command": "./scripts/copysrc.sh",
   "windows": {
    "command": ".\\scripts\\copysrc.cmd"
   },
   "group": "build",
   "presentation": {
    "reveal": "always"
   },
   "problemMatcher": [],
   "dependsOn": "build"
  },
  {
   "label": "Build and copy",
   "dependsOn": [
    "build",
    "Copy files"
   ],
   "group": "build",
   "problemMatcher": []
  }
 ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接