VSCode插件开发-0-初始化

2021-03-22 15:26:012021-03-26 12:26:14

由于晚上下班坐公交方便点,虽然移动端有不少app供查看,但懒得打开手机,而且自己大多数时间都在使用vscode,因此打算开发一个实时公交插件。

GitHub地址 插件市场地址

项目搭建

  1. 安装官方类似于脚手架的工具

npm install -g yo generator-code

  1. 使用 yo code 初始化一个项目模板

选择 New Extension (TypeScript)webpack

项目简单运行

VSCode打开项目并F5,会在一个新的VSCode窗口中运行插件

在新窗口的命令面板(Ctrl+Shift+P) 运行Hello World命令。右下角会弹出Hello World通知信息

注:

默认项目的激活事件设置的是执行helloWorld指令:

"activationEvents": [
    "onCommand:extension-template.helloWorld"
],
"contributes": {
    "commands": [
        {
            "command": "extension-template.helloWorld",
            "title": "Hello World"
        }
    ]
}

因此在执行Hello World命令时会激活插件。而且extension.ts中注册了helloWorld命令回调,因此会显示通知信息

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

目录结构

vscode插件目录结构