乐闻世界logo
搜索文章和话题

How to configure and use VS Code task system?

2月18日 18:25

VS Code Task System and Automated Build

VS Code task system allows defining and running external tools within the editor, implementing automated build, test, deployment and other processes. Task configurations are stored in the .vscode/tasks.json file.

Task Configuration Basics

Creating Task Configuration

Through menu: Terminal > Configure Tasks or manually create .vscode/tasks.json:

json
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "npm", "script": "build", "problemMatcher": "$tsc" } ] }

Task Types

npm Tasks

Using npm scripts:

json
{ "label": "npm: build", "type": "npm", "script": "build", "group": "build" }

Shell Tasks

Execute shell commands:

json
{ "label": "echo", "type": "shell", "command": "echo", "args": ["Hello World"] }

Process Tasks

Run processes directly:

json
{ "label": "run server", "type": "process", "command": "node", "args": ["server.js"], "isBackground": true }

Task Properties

Common Properties

  • label: Task display name
  • type: Task type (shell, process, npm)
  • command: Command to execute
  • args: Command arguments
  • options: Options (cwd, env, etc.)
  • group: Task grouping (build, test, none)
  • dependsOn: Other tasks this depends on
  • problemMatcher: Problem matcher

Task Grouping

json
{ "label": "build", "type": "npm", "script": "build", "group": { "kind": "build", "isDefault": true } }

Problem Matchers

Problem matchers convert task output to clickable error links:

Built-in Matchers

  • $tsc: TypeScript compiler
  • $eslint: ESLint
  • $jshint: JSHint
  • $go: Go compiler

Custom Matchers

json
{ "label": "lint", "type": "shell", "command": "eslint", "args": ["src/**/*.js"], "problemMatcher": { "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "message": 4 } } }

Task Dependencies

Define dependencies between tasks:

json
{ "version": "2.0.0", "tasks": [ { "label": "clean", "type": "shell", "command": "rm", "args": ["-rf", "dist"] }, { "label": "build", "dependsOn": ["clean"], "type": "npm", "script": "build" } ] }

Multi-task Configuration

Build and Test

json
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "npm", "script": "build", "group": "build" }, { "label": "test", "type": "npm", "script": "test", "group": "test" } ] }

Auto-detection Tasks

VS Code can auto-detect tasks from common build tools:

  • npm scripts
  • Gulp
  • Grunt
  • Jake
  • Maven

Task Running Methods

  1. Command Palette: Ctrl+Shift+P > "Tasks: Run Task"
  2. Keyboard Shortcuts: Bind custom shortcuts
  3. From Task List: Terminal > Run Task

Important Notes

  • Task configurations should be committed to version control
  • Use isBackground: true for long-running tasks
  • Use problem matchers appropriately to improve development efficiency
  • Consider cross-platform compatibility (Windows/Linux/macOS)
  • Organize complex build workflows using dependsOn
标签:VSCode