一、前言- w+ b" }8 L& x% V! b" c6 E. k1 |
本文为vscode插件——code snippets开发的踩坑笔记,旨在提供一个快捷有效的方式打造一款自己的vscode插件。上手不难,但是就是坑比较多,为了避免更多人踩坑,于是写文避坑。本文针对新手,楼主也是花了半天时间上手去踩坑的。文末有github仓库地址,码友们可以直接clone下来当作以后code snippets插件的模板。
- M' W/ I9 U6 i; ~! w二、环境准备6 ~& V+ F* \: {- Q- \# p6 q
- node
- npm
- yeoman sudo npm i yo -g
- gernerator-code sudo npm i gernerator-code -g2 t7 |: w! d B, i
三、开发者账号申请0 s- C/ y5 V3 q7 `; t& W6 `$ N& a
点击 Visual Studio Team Services,点击最上方立即注册,注册自己的账号,可以用GitHub账号直接登录。
% Q9 Q* u3 ^8 {1 n1.注册并登录 Azure DevOps。
$ U! n- x5 O$ O$ p1 D2.选择或者创建一个组织(new organization)。
2 \( @" v; y- @& U" n6 W6 E8 @5 P3.在该组织下创建一个项目(new project)。+ D7 V* V2 z& p$ P2 N/ _$ a
以上三步都是为了获取开发者(publisher)的token,获取token如下图:( s# D0 ?5 w- k+ b1 V
8 z4 s# C8 c; g/ _4. 点击 New Token,注意这里创建token一定要下拉选择 all accessible organizations,然后点击右下角的 Show all scopes 找到 Marketplace 选择 Manage。
: o* y( N0 o2 @) Y8 {$ a, o& M
2 ]- ~" k- `( @2 ~& a- B7 `
5. 注册vscode插件开发者(publisher):点击此处。注册保证name和id一致,并且只需要填写这两个就可以了。
* ~& L9 }" R& R6 G2 R5 W" c1 L三、项目搭建
" r; ^3 q2 x1 [! c/ J- J1、项目初始化) W0 c; ]: s7 Q8 D1 I% |
- 终端输入 yo code。
- 选项如下设置:language选择javascript,名称就是你扩展的名称。: K6 c4 C/ t! h4 P
& E* B+ f7 v" O) F+ I0 K
2、项目改造
% _6 y3 G' ~1 P2 L8 ?5 s 将项目文件夹yc-charts-code-snippets(你的扩展名字的文件夹)拖到vscode中打开。做以下改造:, k. L" |: f7 |% ]
1.将package.json改造成以下模样:
8 T& O( F p! E r0 j0 F{
"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包会失败。& |; b h' u% o, i' V4 k# V7 S* E
3、编码
0 Y8 h0 ? G4 s6 J$ R4 Q8 K4 t) k 这里的编码其实并不是写js,而是写json配置,在哪里写呢?在项目的 snippets 文件夹中的 snippets.code-snippets 文件中写入:
, G" M O8 l5 [. M" {/ e. p{
"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键会跳转的语句,数字代表跳转的顺序,注意:冒号后面没有空格。
: b5 q, M4 }9 C& F! {四、调试
" G& q. O$ F" L5 D0 s8 V+ R- I' Q 单纯的开发代码片段的话测试和调试都非常简单:
' b0 J/ T% N" _+ m& {5 z- 首先先自定义全局的code snippet。, n+ Y) Y) p) L
- k" Z( P) M2 t2 V- 选择新建全局代码片段文件。
9 w8 ^ A& {+ P2 `) i% M4 H7 A
( k( j, \6 ]# G4 c4 D
- 创建完文件名后会得到如下示例代码,放开example下面的代码即可测试。
% v0 ]$ E# X, M6 A! _
- N; g @6 K( Q9 V: y/ |- command + shift + D 打开调试模式。
- 点击 Extension 旁边的绿色三角形,打开扩展开发宿主。
- 在扩展开发宿主中创建适合的文件类型如:test.js。
- 测试自己的写的 prefix 即可。这里我生成的代码片段是自己封装的组件的示例代码。, `" Y& \* @! t) m( V
) l% B' {% U$ ]9 G. I 生成的代码片段如下:
7 J) I% ^4 N6 Z* G1 }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; 五、打包
" G+ ~. s' o V% I 终端运行命令 vsce package 之后会生成一个打包好的二进制文件。
e9 Q6 _7 n# t# b7 P L六、发布7 D2 }2 }, t" ~% e" p
终端运行命令 vsce publish,命令运行完之后需要等待几分钟,之后你注册账号的邮箱会收到一封发布成功的邮件,那么恭喜你成功发布了一款vscode插件!
! L9 f& g, u4 b# D2 g
: a4 r S6 y- w9 d 点击上图标记的url可以看到自己插件的一个访问和下载情况的统计图,如果是404请耐心等待几分钟哦~
2 L& k6 V0 @( h- p' W* N( a( g! s* G! ]
^& m+ o1 U& r
七、结语* N" |( S7 I$ Y; D2 b
vscode的code snippets插件是最容易实现的一款插件,对于入门的同学来说是一个很好的上手例子。可以看到其实并没有什么编码工作,但是确实很多坑在里面。写这篇文章的目的就是让更少的人再去踩坑,希望对码友们有帮助~。本文项目github仓库:点击此处。 |