Development Environment
-
Node.js
-
VS Code
-
Yeoman&generator-code
To conveniently generate the basic structure of a plugin project, you can use Yeoman and the official VS Code extension generator. Open a terminal and run the following commands to install globally:
npm install -g yo generator-code
Initial Plugin Framework
Create Plugin
yo codeThe generator will ask the following questions:
- **Choose Extension Type**:For example “New Extension (TypeScript)” or “New Extension (JavaScript)”.- **Extension name and description**:Fill in the extension name and description as prompted.- **Git initialization**:Whether to initialize a Git repository.- **Package manager**:Choose using npm or yarn.After generation completes, you will have a basic project structure scaffolded.
Understanding the Structure
Open the generated project, you will see some important files and directories:
-
package.jsonThis file defines the extension’s basic information, dependencies, as well as VS Code activation events and command registrations.
For example:
{"name": "my-sample-extension","displayName": "My Sample Extension","description": "A simple VS Code 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(orextension.js)This is the entry point of the extension; this code runs when the extension is activated.
For example, a simple sample:
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 VS Code extension!');});context.subscriptions.push(disposable);}export function deactivate() {}
Run and Debug
- After opening the project, in VS Code’s Debug panel, you will see a configuration named
Launch Extension. - Press
F5; VS Code will start a new Extension Development Host. - In the new window, via the Command Palette (
Ctrl+Shift+PorCmd+Shift+P), run the registered command, for example typing “Hello World” to test whether the extension works properly.
Publish Extension
When development and testing are complete, you may consider publishing the extension to the VS Code Marketplace for others to use:
-
Install the
vscetool to help package and publish extensions:npm install -g vsce -
In the project root, run the packaging command:
vsce package -
Follow the guidance in the official documentation to complete the publishing process. (Official docs: https://code.visualstudio.com/api/working-with-extensions/publishing-extension)
Specific Development
Extension Activation
VS Code extensions are activated according to the activationEvents array in package.json. Here are the main activation event types:
Common Activation Events
-
- Activate the extension immediately when VS Code starts
- Pros: the extension is always available
- Cons: may affect VS Code startup performance; not recommended for production environments
- onStartupFinished
- Activate the extension after VS Code finishes starting up
- Slightly later than the previous, but with less impact on startup performance
- onCommand
- Activate the extension when the user executes a specific command
- For example: onCommand
.helloWorld
- onLanguage
- Activate the extension when opening files of a specific language
- For example: onLanguage
, onLanguage
- onView
- Activate the extension when a specific view becomes visible
- For example: onView
Other Activation Events
- onUri
- Activate the extension when a specific URI is opened
- For example: onUri:https://my-extension.com
- onWebviewPanel
- Activate the extension when creating a specific type of Webview panel
- onCustomEditor
- Activate the extension when opening a custom editor
- onDebug
- Activate the extension when starting a debugging session
- onDebugInitialConfigurations
- Activate the extension when initializing debug configurations
- onDebugResolve
- Activate the extension when resolving a specific type of debug configuration
- onFileSystem
- Activate the extension when accessing a specific filesystem scheme
- For example: onFileSystem
- onTerminalProfile
- Activate the extension when creating a terminal profile
- onAuthenticationRequest
- Activate the extension when requesting authentication from a specific provider
- onSearch
- Activate the extension when performing a search
- onTaskType
- Activate the extension when executing a task of a specific type
- onNotebook
- Activate the extension when opening a notebook of a specific type
- onTerminal
- Activate the extension when a terminal is created
If this article helped you, please share it with others!
Some information may be outdated





