一、前言# {3 M3 k7 Q$ X% Z
本文为vscode插件——code snippets开发的踩坑笔记,旨在提供一个快捷有效的方式打造一款自己的vscode插件。上手不难,但是就是坑比较多,为了避免更多人踩坑,于是写文避坑。本文针对新手,楼主也是花了半天时间上手去踩坑的。文末有github仓库地址,码友们可以直接clone下来当作以后code snippets插件的模板。
$ Z( H% q0 z) o4 U8 u! m$ O二、环境准备* N4 q# @9 S% [
- node
- npm
- yeoman sudo npm i yo -g
- gernerator-code sudo npm i gernerator-code -g
: Z2 R: ?% ?, W. U, S7 ^ 三、开发者账号申请
% z4 d/ w' X/ T; f 点击 Visual Studio Team Services,点击最上方立即注册,注册自己的账号,可以用GitHub账号直接登录。8 l$ ]3 i( w6 h5 X5 ~3 C% ^
1.注册并登录 Azure DevOps。
1 F0 W3 G" K1 d7 B1 z g2.选择或者创建一个组织(new organization)。
6 L/ W# y& E5 A4 F3.在该组织下创建一个项目(new project)。
7 J* h C, x; K, X3 s- ]$ w 以上三步都是为了获取开发者(publisher)的token,获取token如下图:
7 i+ V9 l5 V9 e3 J: C; F% c
8 i; s+ Y2 J* l4. 点击 New Token,注意这里创建token一定要下拉选择 all accessible organizations,然后点击右下角的 Show all scopes 找到 Marketplace 选择 Manage。
^ z; n3 ]% I# E* o# Y( H
5 w1 Z! s) a4 R2 M
5. 注册vscode插件开发者(publisher):点击此处。注册保证name和id一致,并且只需要填写这两个就可以了。" Y/ a' Y) U) e' r
三、项目搭建
! @3 ^! X+ a3 ?1、项目初始化- ~6 t* s7 D4 [0 O0 C" P
- 终端输入 yo code。
- 选项如下设置:language选择javascript,名称就是你扩展的名称。8 B6 s+ S6 s! q+ ^$ F+ f2 a3 v
) U: n- |! e, e1 h2、项目改造
( Q0 O P7 H# `) D+ n1 ~ 将项目文件夹yc-charts-code-snippets(你的扩展名字的文件夹)拖到vscode中打开。做以下改造:; ~: _- S" l4 \" Q7 _
1.将package.json改造成以下模样:1 q# l, ?" n$ n
{
"name": "yc-charts-code-snippets",
"displayName": "yc-charts-code-snippets",
"description": "",
"version": "0.0.1",
// 开发者名称,很重要!!!
"publisher": "yuanchen-ljx",
"engines": {
"vscode": "^1.56.0"
},
"categories": [
"Snippets"
],
"contributes": {
"snippets": [
// 在.js、.jsx、.ts、.tsx文件中智能提示code snippets
{
"language": "javascript",
"path": "./snippets/snippets.code-snippets"
},
{
"language": "javascriptreact",
"path": "./snippets/snippets.code-snippets"
},
{
"language": "typescript",
"path": "./snippets/snippets.code-snippets"
},
{
"language": "typescriptreact",
"path": "./snippets/snippets.code-snippets"
}
]
},
// 仓库地址。很重要!!!
"repository": {
"type": "git",
"url": "https://github.com/ljx94nb/yc-charts-code-snippets.git"
},
// 扩展的介绍的md文档地址,也很重要!!!
"homepage": "https://github.com/ljx94nb/yc-charts-code-snippets/blob/master/README.md"
} 2.修改一下你项目目录中的README.md,无论怎么修改都可以!!!否则生成package包会失败。
. ~& C- f, F/ v5 s" |3、编码
/ A9 ]) L* B. u' V) g! b2 g 这里的编码其实并不是写js,而是写json配置,在哪里写呢?在项目的 snippets 文件夹中的 snippets.code-snippets 文件中写入:. E8 H: i8 b0 |3 `# j/ D
{
"FormTable": {
"prefix": "ycft",
"body": [
"import React from 'react';",
"import YcCharts from 'xxx';",
"",
"function App() {",
" const ${1:treeConfig} = {",
" // config",
" ${2:primaryColumn}: {},",
" ${3:columnWidth}: 120,",
" ${4:indicators}: [],",
" ${5:leftDimensions}: [],",
" ${6:topDimensions}: [],",
" ${7:topDimensionDataSource}: {},",
" ${8:leftDimensionDataSource}: {},",
" ${9:dataSource}: {},",
" ${10:operatorColumn}: {},",
" ${11:filters}: {},",
" ${12:actions}: [],",
" ${13:downloadCfg}: {},",
" // ${14:onSort}: (colIndex, sortOrder) => {},",
" // ${15:decorateValue}: (indicatorKey, value) => {}",
" };",
"",
" return <YcCharts.FormTable {...${16:treeConfig}} />;",
"}",
"",
"export default App;"
],
"description": "Code snippet for 'yc-charts FormTable'"
}
} 上面的代码片段是我自己封装的组件用法的示例代码。${1:treeConfig} 表示的是tab键会跳转的语句,数字代表跳转的顺序,注意:冒号后面没有空格。
$ e0 M" d# b/ e: J四、调试
1 }" s3 B# ?( @" E, ?0 l; R 单纯的开发代码片段的话测试和调试都非常简单:
4 K: u# s& t* @8 s; p/ a2 r- 首先先自定义全局的code snippet。
8 M; f$ e5 K) ~; y" g' y
& `/ V6 {! h- ]6 _" V9 m
- 选择新建全局代码片段文件。3 Q \5 {3 E$ O- |. S
' o: ^' q' c& L& U* }( _
- 创建完文件名后会得到如下示例代码,放开example下面的代码即可测试。9 G3 _5 K! _" n7 n
0 h$ }7 o+ e6 N- command + shift + D 打开调试模式。
- 点击 Extension 旁边的绿色三角形,打开扩展开发宿主。
- 在扩展开发宿主中创建适合的文件类型如:test.js。
- 测试自己的写的 prefix 即可。这里我生成的代码片段是自己封装的组件的示例代码。! V) {. ^2 n) E/ s5 X' \ ]
& B, d+ o8 E: i! g( [9 _9 t9 l6 K
生成的代码片段如下:$ _# X& x0 E: l9 b
import React from 'react';
import YcCharts from 'xxx';
function App() {
const treeConfig = {
// config
primaryColumn: {},
columnWidth: 120,
indicators: [],
leftDimensions: [],
topDimensions: [],
topDimensionDataSource: {},
leftDimensionDataSource: {},
dataSource: {},
operatorColumn: {},
filters: {},
actions: [],
downloadCfg: {},
// onSort: (colIndex, sortOrder) => {},
// decorateValue: (indicatorKey, value) => {}
};
return <YcCharts.FormTable {...treeConfig} />;
}
export default App; 五、打包) G4 ~$ e6 ~& S$ A) d P
终端运行命令 vsce package 之后会生成一个打包好的二进制文件。! B5 N1 D6 ~0 C, @
六、发布. D! `4 R k# i( q8 b
终端运行命令 vsce publish,命令运行完之后需要等待几分钟,之后你注册账号的邮箱会收到一封发布成功的邮件,那么恭喜你成功发布了一款vscode插件!9 m0 a" B o) m+ }! \
" a3 l& i0 z) ?( E0 d" a/ ~+ V 点击上图标记的url可以看到自己插件的一个访问和下载情况的统计图,如果是404请耐心等待几分钟哦~; ~3 ~( J: t' r+ I: o/ Q! ?1 W' x* F+ h
- h+ y& m. h& L七、结语5 N! p4 M5 P. g$ N
vscode的code snippets插件是最容易实现的一款插件,对于入门的同学来说是一个很好的上手例子。可以看到其实并没有什么编码工作,但是确实很多坑在里面。写这篇文章的目的就是让更少的人再去踩坑,希望对码友们有帮助~。本文项目github仓库:点击此处。 |