unplugin-vue-components
unplugin-vue-components copied to clipboard
使用pnpm命令创建的vite项目无法在vscode中获得组件类型提示
如图,yarn和npm创建的工程都能获得类型提示,具有代码高亮
而使用pnpm命令创建的工程则没有代码高亮和类型提示,但是运行是正常的,令我感到困惑
https://github.com/antfu/unplugin-vue-components/issues/389
问题出现在自动生成的 components.d.ts 文件中的 declare module '@vue/runtime-core'
声明,在 pnpm 中只能访问项目的顶级依赖,而 @vue/runtime-core 是 vue 模块下的依赖,不是顶级依赖,导致声明语句失效。(yarn 和 npm 的 node_modules 平铺目录结构允许访问所有依赖)
解决方案如下:
- Volar 同时也读取了 vue 模块中的 GlobalComponents 类型,如果你愿意手动维护 components.d.ts 文件,直接将声明改成
declare module 'vue'
即可。 - 也可以直接引入
pnpm add @vue/runtime-core -D
依赖,让声明语句生效即可,但是要注意引入的依赖版本应该与 vue 的版本相同,声明语句才会生效在正确的模块。 - 推荐直接修改 .npmrc 配置文件
public-hoist-pattern[]=@vue/runtime-core
将依赖提升至根目录,也可以设置shamefully-hoist=true
将所有依赖都提升至根目录(不推荐)。
shamefully-hoist=true已经设置了,但是antv的Col和Row还是没有提示(其他组件有)
thanks
添加 .npmrc
# .npmrc
# 提升含有 eslint(模糊匹配)、prettier(模糊匹配)、viewerjs(精确匹配) 的依赖包到根 node_modules 目录下
public-hoist-pattern[]=@vue/runtime-core