FrankKai / FrankKai.github.io

FE blog
https://frankkai.github.io/
362 stars 39 forks source link

VSCode插件开发踩坑 #257

Open FrankKai opened 2 years ago

FrankKai commented 2 years ago
FrankKai commented 2 years ago

require类型问题

require不生效

读取项目中文件(require("./foo.js"))的话,需要采用非webpack模式的打包,否则会引入失败

require.cache

需要手动delete require.cache["xxxx"],否则会导致引入文件为缓存文件,获取不到最新文件。 本质上是因为nodejs的require有缓存机制。

FrankKai commented 2 years ago

查看打包后的插件内容

vsix包中的打包文件,受到.vscodeignore的影响,有两种方式查看即将生成的vsix包中的文件:vsce ls 和 将vsix后缀改为zip,解压后查看

FrankKai commented 2 years ago

dependencies中的包未生效

如果在插件内部引入了npm包作为运行时依赖,也就是dependencies,需要将.vscodeignore中的node_modules/**移除。移除后,会将node_modules也打入到vsix包中

FrankKai commented 2 years ago

使用esbuild压缩打包文件

有很重要的一点,esbuild天然支持打包ts类型的文件。

Bundling can automatically strip TypeScript types, convert ECMAScript module syntax to CommonJS, and transform newer JavaScript syntax into older syntax for a specific version of node

原始的ts方式打包

  "scripts": {
    "vscode:prepublish": "npm run compile",
    "compile": "tsc -p ./",
    "watch": "tsc -watch -p ./",
    "pretest": "npm run compile",
    "build": "yarn compile && vsce package --no-yarn"
  },

=>

esbuild方式优化打包

  "scripts": {
    "vscode:prepublish": "yarn esbuild-base -- --minify",
    "esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node",
    "esbuild": "yarn esbuild-base -- --sourcemap",
    "esbuild-watch": "yarn esbuild-base -- --sourcemap --watch",
    "test-compile": "tsc -p ./",
    "pretest": "yarn test-compile"
  },

其中最重要的是这一行: esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node

我开发的一个插件,使用esbuild打包后,大小从2.23MB(把node_modules,未压缩js文件,图片等等都打了进去),变成了44KB(一个高度压缩的extension.js,图片等等)。

FrankKai commented 2 years ago

发布插件 ERROR TF400898

运行vsce publish,提示如下错误: ERROR TF400898: An Internal Error Occurred. Activity Id: 78f21af1-d92d-4984-afcd-b53942d97394.

是因为package.json里的displayName为空导致的。

{
  "displayName": "",
}

为其指定值即可。

Conan9912345 commented 1 year ago

vscode插件本地开发过程中运行正常,但是打包后引入插件就无效。 从侧边栏自定义了一个右键菜单,点击菜单中的选项会打开一个webview。但是打包之后引入插件,右键菜单出来了,点击之后webview不会打开。尝试了一下,把webview换成vscode.window.showWarningMessage,也没有生效。