一、前言
g& D9 k. I+ x3 `9 J% m/ Y5 x 本文为vscode插件——code snippets开发的踩坑笔记,旨在提供一个快捷有效的方式打造一款自己的vscode插件。上手不难,但是就是坑比较多,为了避免更多人踩坑,于是写文避坑。本文针对新手,楼主也是花了半天时间上手去踩坑的。文末有github仓库地址,码友们可以直接clone下来当作以后code snippets插件的模板。
% E+ E5 ?! e9 N7 \二、环境准备/ t( t+ @- w5 j+ ~$ k
- node
- npm
- yeoman sudo npm i yo -g
- gernerator-code sudo npm i gernerator-code -g5 u9 h5 i0 @- G9 P8 V
三、开发者账号申请
- ]9 X- l6 f' ~$ i# C5 y 点击 Visual Studio Team Services,点击最上方立即注册,注册自己的账号,可以用GitHub账号直接登录。
* P$ m3 v' w8 [" C' p9 t1.注册并登录 Azure DevOps。
5 i' s$ H: t- {. u% C; s2.选择或者创建一个组织(new organization)。1 O: Z% Y) D7 E# G# q% Z/ r; n
3.在该组织下创建一个项目(new project)。; q# N7 C$ }0 P% H0 I+ z
以上三步都是为了获取开发者(publisher)的token,获取token如下图:
- `2 g1 v, e# K1 x, v3 i
& b2 ~3 Z+ M2 q W. g D
4. 点击 New Token,注意这里创建token一定要下拉选择 all accessible organizations,然后点击右下角的 Show all scopes 找到 Marketplace 选择 Manage。
( a" @/ F# b" H7 V% R
+ E$ X; i. N) y% ^ d2 E5. 注册vscode插件开发者(publisher):点击此处。注册保证name和id一致,并且只需要填写这两个就可以了。" n: f% E6 D9 {9 y- Y
三、项目搭建
4 e( S+ N- j+ p' A R1、项目初始化/ Y) ^) w+ N3 Z2 |
- 终端输入 yo code。
- 选项如下设置:language选择javascript,名称就是你扩展的名称。
5 P1 K$ f2 i% v; g& ~
% W/ Z1 U- Z, q( b
2、项目改造4 f( \+ o) Q: B
将项目文件夹yc-charts-code-snippets(你的扩展名字的文件夹)拖到vscode中打开。做以下改造:! G5 ?) w7 _# z
1.将package.json改造成以下模样:& ^ x h9 {% Q
{
"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包会失败。 S7 ], N! L$ t' @
3、编码
( z- K. y2 e! [7 c/ C+ l 这里的编码其实并不是写js,而是写json配置,在哪里写呢?在项目的 snippets 文件夹中的 snippets.code-snippets 文件中写入:8 F: l4 f- G% q8 I+ g
{
"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键会跳转的语句,数字代表跳转的顺序,注意:冒号后面没有空格。3 O( e( W6 d# m+ ^6 ~7 H
四、调试/ {9 { l! u2 ]/ u% L
单纯的开发代码片段的话测试和调试都非常简单:
, m5 l% n3 V) H0 j7 u1 Z8 i4 c- 首先先自定义全局的code snippet。
# k- G* ^+ @+ e2 h* e4 |- W
/ o2 b9 Q. _- _: g7 X- 选择新建全局代码片段文件。9 s9 J4 `2 C1 d: D' D" ~2 n# n
% J0 K5 T) l; V0 w, i- 创建完文件名后会得到如下示例代码,放开example下面的代码即可测试。
- `. C7 V/ B& `- z1 q, n- }
$ ~1 l- B# l, {- z3 t9 d
- command + shift + D 打开调试模式。
- 点击 Extension 旁边的绿色三角形,打开扩展开发宿主。
- 在扩展开发宿主中创建适合的文件类型如:test.js。
- 测试自己的写的 prefix 即可。这里我生成的代码片段是自己封装的组件的示例代码。9 Q) f$ X4 U1 T# @# J3 l$ f) v
3 ]" _. l. e! G" @. j* h& W 生成的代码片段如下:: G" H7 k8 \( c1 A
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; 五、打包
5 v+ C: v3 C$ s4 X1 _ [1 W8 Z4 u 终端运行命令 vsce package 之后会生成一个打包好的二进制文件。# f8 {+ v$ y0 Y! ~' I
六、发布
, \- I# R. B3 ~8 m. n 终端运行命令 vsce publish,命令运行完之后需要等待几分钟,之后你注册账号的邮箱会收到一封发布成功的邮件,那么恭喜你成功发布了一款vscode插件!4 @, v" w& h3 L% `1 `! W9 Y, f }
- D$ x" I3 }3 B+ b% ~% q
点击上图标记的url可以看到自己插件的一个访问和下载情况的统计图,如果是404请耐心等待几分钟哦~1 L/ P# C& Z6 D
5 g' \- G m2 l$ a+ y3 f% s4 t七、结语1 y! x8 j* X+ }# O
vscode的code snippets插件是最容易实现的一款插件,对于入门的同学来说是一个很好的上手例子。可以看到其实并没有什么编码工作,但是确实很多坑在里面。写这篇文章的目的就是让更少的人再去踩坑,希望对码友们有帮助~。本文项目github仓库:点击此处。 |