一、前言( o( F, K3 }% s2 F
本文为vscode插件——code snippets开发的踩坑笔记,旨在提供一个快捷有效的方式打造一款自己的vscode插件。上手不难,但是就是坑比较多,为了避免更多人踩坑,于是写文避坑。本文针对新手,楼主也是花了半天时间上手去踩坑的。文末有github仓库地址,码友们可以直接clone下来当作以后code snippets插件的模板。3 K6 w3 q: r1 M s/ C6 q% ^
二、环境准备' f7 L9 w! k+ g1 Y
- node
- npm
- yeoman sudo npm i yo -g
- gernerator-code sudo npm i gernerator-code -g- V9 i: ^2 J; A8 r v* W2 \
三、开发者账号申请; {; ~& a% X: k7 R0 P
点击 Visual Studio Team Services,点击最上方立即注册,注册自己的账号,可以用GitHub账号直接登录。3 j' @' Y8 O3 {; @6 s, t
1.注册并登录 Azure DevOps。
. p A" F: I2 ]2.选择或者创建一个组织(new organization)。
% d) _) G$ i' X& e5 ~3.在该组织下创建一个项目(new project)。
. _9 w" Z8 l3 ?$ Z8 A$ y9 J4 s8 x4 m9 M- m 以上三步都是为了获取开发者(publisher)的token,获取token如下图:- t+ n) E) u$ |1 V" _( X
# c' z% c* N8 I1 h5 o0 w
4. 点击 New Token,注意这里创建token一定要下拉选择 all accessible organizations,然后点击右下角的 Show all scopes 找到 Marketplace 选择 Manage。1 D' K$ b# B, D6 V0 S
3 E8 _. |9 J1 e
5. 注册vscode插件开发者(publisher):点击此处。注册保证name和id一致,并且只需要填写这两个就可以了。/ g& g, D' b. l+ ?: M4 t2 X3 [; Y8 f
三、项目搭建
- x( f1 C* i; O+ i* R1、项目初始化) I+ K% u* p Z I5 ~) A
- 终端输入 yo code。
- 选项如下设置:language选择javascript,名称就是你扩展的名称。
( |) p* A" |- U/ ~5 G( G
; G& Q' V5 B L: v6 b( F' B& o9 i
2、项目改造$ @& \8 [ l* ~& x% H' Z- a- g
将项目文件夹yc-charts-code-snippets(你的扩展名字的文件夹)拖到vscode中打开。做以下改造:$ D/ t. r0 Y! N2 y0 l$ b
1.将package.json改造成以下模样:
, z1 a; p3 s5 E% Q& ^5 `- W! t{
"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包会失败。, T# u7 h4 a+ _: O
3、编码! g& l7 ?( c6 O) T$ l2 ?% [
这里的编码其实并不是写js,而是写json配置,在哪里写呢?在项目的 snippets 文件夹中的 snippets.code-snippets 文件中写入:
# @+ |6 S4 G! ?6 k4 U8 Q" l{
"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键会跳转的语句,数字代表跳转的顺序,注意:冒号后面没有空格。
* U5 V2 `# R2 ?0 |, H% K% ^四、调试0 S1 e9 q9 {% \* g# _
单纯的开发代码片段的话测试和调试都非常简单:
$ q' W. I) X. R" } k- 首先先自定义全局的code snippet。2 \4 p$ N# P9 @' l
" h" W) Q8 K. Z0 | ~
- 选择新建全局代码片段文件。
. i4 e! q. c5 x% c" P
" ^' ]$ L( H/ p* E8 H
- 创建完文件名后会得到如下示例代码,放开example下面的代码即可测试。
; f( ~8 e n: S; b8 Z# E
+ @' i* J) f3 Y. \- P1 G- command + shift + D 打开调试模式。
- 点击 Extension 旁边的绿色三角形,打开扩展开发宿主。
- 在扩展开发宿主中创建适合的文件类型如:test.js。
- 测试自己的写的 prefix 即可。这里我生成的代码片段是自己封装的组件的示例代码。# y2 U4 @% V8 ^# N: C
5 x- l: A( h8 K, n. V( F1 W( t
生成的代码片段如下:
/ G/ h3 }+ E0 Q6 m. aimport 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; 五、打包
* P9 U' I0 h% s$ B 终端运行命令 vsce package 之后会生成一个打包好的二进制文件。/ v& W, h; v( Y& k. G% o
六、发布
4 W# h3 v! ^1 S, W! U/ L$ O 终端运行命令 vsce publish,命令运行完之后需要等待几分钟,之后你注册账号的邮箱会收到一封发布成功的邮件,那么恭喜你成功发布了一款vscode插件!
* Q, f: J$ L- v) I4 j" j+ Z6 I
; H, M, v; `2 b/ w0 _7 J1 g
点击上图标记的url可以看到自己插件的一个访问和下载情况的统计图,如果是404请耐心等待几分钟哦~, \, D! y: z, R$ ^% C5 b" A; e
+ D8 r/ Q6 F }- Q; L% s( C
七、结语" J$ u6 i4 a* O% X0 O2 G9 C
vscode的code snippets插件是最容易实现的一款插件,对于入门的同学来说是一个很好的上手例子。可以看到其实并没有什么编码工作,但是确实很多坑在里面。写这篇文章的目的就是让更少的人再去踩坑,希望对码友们有帮助~。本文项目github仓库:点击此处。 |