mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
475 words
2 minutes
Getting Started with VS Code Extension Development
2025-03-13

Development Environment#

  1. Nodejs

  2. vscode

  3. Yeoman&generator-code

    To conveniently generate the basic structure of an extension project, you can use Yeoman and the official extension generator provided by VSCode. Open a terminal and enter the following command to install them globally:

    npm install -g yo generator-code

Initial Extension Scaffold#

Creating an Extension#

yo code

The generator will ask the following questions:

- **Select an extension type**: For example, “New Extension (TypeScript)” or “New Extension (JavaScript)”.
- **Extension name and description**: Enter the desired extension name and description as prompted.
- **Git initialization**: Whether to initialize a Git repository.
- **Package manager**: Choose npm or yarn.

Once generation is complete, you will have an initially scaffolded project structure.

Understanding the Structure#

Open the generated project, and you will see several important files and directories:

  • package.json

    This file defines the extension’s basic information, dependencies, VSCode activation events, and command registrations.

    For example:

    {
    "name": "my-sample-extension",
    "displayName": "My Sample Extension",
    "description": "A simple VSCode extension.",
    "version": "0.0.1",
    "engines": {
    "vscode": "^1.60.0"
    },
    "activationEvents": [
    "onCommand:extension.helloWorld"
    ],
    "main": "./out/extension.js",
    "contributes": {
    "commands": [
    {
    "command": "extension.helloWorld",
    "title": "Hello World"
    }
    ]
    },
    "scripts": {
    "vscode:prepublish": "npm run compile",
    "compile": "tsc -p ./"
    },
    "devDependencies": {
    "typescript": "^4.0.0",
    "vscode": "^1.1.37",
    "@types/node": "^12.0.0"
    }
    }
  • src/extension.ts** (or extension.js)**

    This is the extension’s entry file. The code here runs when the extension is activated.

    For example, here is a simple example:

    import * as vscode from 'vscode';
    export function activate(context: vscode.ExtensionContext) {
    console.log('Congratulations, your extension "my-sample-extension" is now active!');
    let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
    vscode.window.showInformationMessage('Hello World from your VSCode extension!');
    });
    context.subscriptions.push(disposable);
    }
    export function deactivate() {}

Running and Debugging#

  • After opening the project, you can find a Launch Extension configuration in VSCode’s Run and Debug panel.
  • Press F5, and VSCode will launch a new Extension Development Host.
  • In the new window, open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and enter the registered command, such as “Hello World”, to test whether the extension works correctly.

Publishing the Extension#

Once development and testing are complete, you can publish the extension to the VSCode Marketplace for others to use:

  • Install the vsce tool to help package and publish the extension:

    npm install -g vsce
  • Run the packaging command in the project’s root directory:

    vsce package
  • Follow the instructions in the official documentation to complete the publishing process.

Extension Development#

Extension Activation#

VSCode extensions use the activationEvents array in package.json to define when they should be activated. The following are the main activation event types:

Common Activation Events#

- Activates the extension immediately when VSCode starts
- Advantage: The extension is always available
- Disadvantage: It affects VSCode's startup performance and is not recommended for production environments

2. onStartupFinished - Activates the extension after VSCode finishes starting - Activates slightly later than *, but has less impact on startup performance 3. onCommand - Activates the extension when the user executes a specific command - Example: onCommand.helloWorld 4. onLanguage - Activates the extension when a file in a specific language is opened - Example: onLanguage, onLanguage 5. onView - Activates the extension when a specific view becomes visible - Example: onView

Other Activation Events#

  1. onUri
  2. onWebviewPanel
    • Activates the extension when a specific type of webview panel is created
  3. onCustomEditor
    • Activates the extension when a custom editor is opened
  4. onDebug
    • Activates the extension when a debugging session starts
  5. onDebugInitialConfigurations
    • Activates the extension when debug configurations are initialized
  6. onDebugResolve
    • Activates the extension when a specific type of debug configuration is resolved
  7. onFileSystem
    • Activates the extension when a specific file system scheme is accessed
    • Example: onFileSystem
  8. onTerminalProfile
    • Activates the extension when a specific terminal profile is created
  9. onAuthenticationRequest
    • Activates the extension when a specific authentication provider is requested
  10. onSearch
    • Activates the extension when a search operation is performed
  11. onTaskType
    • Activates the extension when a specific type of task is executed
  12. onNotebook
    • Activates the extension when a specific type of notebook is opened
  13. onTerminal
    • Activates the extension when a terminal is created
Share

If this article helped you, please share it with others!

Getting Started with VS Code Extension Development
https://dreaife.tokyo/en/posts/vscode-plugin-dev/
Author
dreaife
Published at
2025-03-13
License
CC BY-NC-SA 4.0

Some information may be outdated

Table of Contents