Development Environment
-
Nodejs
-
vscode
-
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 codeThe 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.jsonThis 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** (orextension.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 Extensionconfiguration 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+PorCmd+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
vscetool 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 environments2. onStartupFinished
- Activates the extension after VSCode finishes starting
- Activates slightly later than *, but has less impact on startup performance
3. onCommand
Other Activation Events
- onUri
- Activates the extension when a specific URI is opened
- Example: onUri:https://my-extension.com
- onWebviewPanel
- Activates the extension when a specific type of webview panel is created
- onCustomEditor
- Activates the extension when a custom editor is opened
- onDebug
- Activates the extension when a debugging session starts
- onDebugInitialConfigurations
- Activates the extension when debug configurations are initialized
- onDebugResolve
- Activates the extension when a specific type of debug configuration is resolved
- onFileSystem
- Activates the extension when a specific file system scheme is accessed
- Example: onFileSystem
- onTerminalProfile
- Activates the extension when a specific terminal profile is created
- onAuthenticationRequest
- Activates the extension when a specific authentication provider is requested
- onSearch
- Activates the extension when a search operation is performed
- onTaskType
- Activates the extension when a specific type of task is executed
- onNotebook
- Activates the extension when a specific type of notebook is opened
- onTerminal
- Activates the extension when a terminal is created
If this article helped you, please share it with others!
Some information may be outdated





