lisonge / vite-plugin-monkey

A vite plugin server and build your.user.js for userscript engine like Tampermonkey, Violentmonkey, Greasemonkey, ScriptCat
MIT License
1.31k stars 70 forks source link

添加`externalGlobals`选项后编译报错 #88

Closed zheung closed 1 year ago

zheung commented 1 year ago

vite-plugin-monkey: 3.2.2 vite: 4.3.9

初次使用插件,依据文档添加externalGlobals选项。发现无论怎样修改都会报错Cannot find module

尝试从https://github.com/lisonge/vite-plugin-monkey/tree/main/playground/example 复制文件,安装并构建,也同样收到相同的错误:

image

除安装依赖时,package.json必要的修改外,example文件夹的其他文件均无修改

- "vite-plugin-monkey": "workspace:*"
+ "vite": "^4.3.9",
+  "vite-plugin-monkey": "^3.2.2"
lisonge commented 1 year ago
pnpm add blueimp-md5
# npm install -S blueimp-md5
lisonge commented 1 year ago

当前仓库依赖结构是 monorepo ,所有的依赖都在 /package.json

https://github.com/lisonge/vite-plugin-monkey/blob/133f6cc393c3785b28a0391c927ea0bae971ea0a/package.json#L15-L76

zheung commented 1 year ago

请教一下之所以需要pnpm i 库是为了更完整的jsdoc提示吗,还是在编译过程中还有其他作用?

我观察了example的dist文件夹中构建后的内容,如果在externalGlobals定义了cdn,构建后的文件并不会包含这个库的源代码,而是通过@require引用cdn。那么通过pnpm i安装库就不会起到除智能提示外的作用了吧。 所以一开始我没有选择pnpm i安装对应的库

externalGlobals: {
    'blueimp-md5': cdn.jsdelivr('md5'),
}
lisonge commented 1 year ago

vite serve 仍然需要 npm iexternalGlobals 同时也需要这个库的 package.json 里的 version/main 来生成 require 链接

externalGlobals 的作用只是减少 vite build 时发布的 xx.user.js 的大小

zheung commented 1 year ago

vite serve 仍然需要 npm iexternalGlobals 同时也需要这个库的 package.json 里的 version/main 来生成 require 链接

externalGlobals 的作用只是减少 vite build 时发布的 xx.user.js 的大小

报错应该来自 rollup 的 resolveId 阶段,插件使用的是 external 来排除依赖,external 执行顺序在 resolveId 之后

原来如此,感谢解答 :)