一、前言
7 K6 h7 n6 p; Z8 Y% \ q. h 本文为vscode插件——code snippets开发的踩坑笔记,旨在提供一个快捷有效的方式打造一款自己的vscode插件。上手不难,但是就是坑比较多,为了避免更多人踩坑,于是写文避坑。本文针对新手,楼主也是花了半天时间上手去踩坑的。文末有github仓库地址,码友们可以直接clone下来当作以后code snippets插件的模板。
4 \2 z$ N( c& w- l5 v: w7 n 二、环境准备 - a8 l- V6 U( @- S/ g6 D& w
node npm yeoman sudo npm i yo -g gernerator-code sudo npm i gernerator-code -g" v9 y- m7 M- w( X
三、开发者账号申请
4 d& A* K$ r& g" V8 r, B, T8 {( l 点击 Visual Studio Team Services ,点击最上方立即注册,注册自己的账号,可以用GitHub账号直接登录。
+ x8 M! a, s, _% i 1.注册并登录 Azure DevOps 。
$ ]9 ]6 p. n$ R1 e z' A# l 2.选择或者创建一个组织(new organization)。7 K3 l1 E- e ~8 f8 ?! A
3.在该组织下创建一个项目(new project)。* w) c* ]7 \, c' H5 m- B/ t. \
以上三步都是为了获取开发者(publisher)的token,获取token如下图:
M3 u; p5 O$ S; @% @8 k& O2 m+ \
& l& e i7 Y3 h, M; x% _3 Q
4. 点击 New Token,注意这里创建token一定要下拉选择 all accessible organizations,然后点击右下角的 Show all scopes 找到 Marketplace 选择 Manage。
7 ]. n' C3 n- z* p2 q+ K5 w
5 ^! c1 V3 X1 e$ e) | 5. 注册vscode插件开发者(publisher):点击此处 。注册保证name和id一致,并且只需要填写这两个就可以了。! l! p& J1 Q+ W
三、项目搭建 0 ]- a2 L/ S Q4 P/ Y) s5 V ]3 n$ E
1、项目初始化 $ ~ C4 H1 z; i/ f. p u
终端输入 yo code。 选项如下设置:language选择javascript,名称就是你扩展的名称。
% ]. G" y; J/ N
: w8 ]- \; }; y$ G F 2、项目改造 , W% X; R# l: r$ l
将项目文件夹yc-charts-code-snippets(你的扩展名字的文件夹)拖到vscode中打开。做以下改造:
- h% T. z9 W4 q- T 1.将package.json改造成以下模样:' w k: E+ M' U+ B
{
"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包会失败。0 r: S R( L1 K/ r6 B: X
3、编码 ' e( J- Q2 _" }, L7 e' V
这里的编码其实并不是写js,而是写json配置,在哪里写呢?在项目的 snippets 文件夹中的 snippets.code-snippets 文件中写入:9 E- q, Z9 S/ o& D* d7 c6 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键会跳转的语句,数字代表跳转的顺序,注意:冒号后面没有空格。4 o a' X: T2 s8 s
四、调试 8 g( z5 R0 s+ \. p
单纯的开发代码片段的话测试和调试都非常简单:3 ^3 R8 k. b+ l( ?+ s! U
首先先自定义全局的code snippet。
M& ~2 h2 `: F, d B6 J
8 `# e, l/ h( m) x 选择新建全局代码片段文件。! k7 b3 F U4 e& T7 V9 u8 K
- `( _* j& j$ ]5 X0 M
创建完文件名后会得到如下示例代码,放开example下面的代码即可测试。
6 G8 I4 Q L( b/ \7 a5 d
+ W9 l' n$ |$ o
command + shift + D 打开调试模式。 点击 Extension 旁边的绿色三角形,打开扩展开发宿主。 在扩展开发宿主中创建适合的文件类型如:test.js。 测试自己的写的 prefix 即可。这里我生成的代码片段是自己封装的组件的示例代码。
6 l8 z, L, P" P6 w1 U
8 C. Q/ ^% ?' N- ? 生成的代码片段如下:
+ f/ I: ~$ G D" |/ c! ? 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;五、打包
0 u, } h4 i# U% Q5 y 终端运行命令 vsce package 之后会生成一个打包好的二进制文件。
- z1 j. k( n/ V 六、发布
& f; h; \3 E4 ~/ a5 s 终端运行命令 vsce publish,命令运行完之后需要等待几分钟,之后你注册账号的邮箱会收到一封发布成功的邮件,那么恭喜你成功发布了一款vscode插件!) d% m. |; {8 t- \7 ^1 N9 _" X, |) M
4 }% T9 l- r6 n; B* L M. M
点击上图标记的url可以看到自己插件的一个访问和下载情况的统计图,如果是404请耐心等待几分钟哦~* E/ R9 O4 `2 w4 T, D, i8 X
" u* m2 Y A- e& f+ V& K# q% I0 A/ s 七、结语 $ m: }, V) ^: u* ?$ S
vscode的code snippets插件是最容易实现的一款插件,对于入门的同学来说是一个很好的上手例子。可以看到其实并没有什么编码工作,但是确实很多坑在里面。写这篇文章的目的就是让更少的人再去踩坑,希望对码友们有帮助~。本文项目github仓库:点击此处 。