tomjs / vite-plugin-vscode

用 vue/react 来开发 vscode extension webview ,支持 esm 和 cjs。Use vue/react to develop vscode extension webview, supporting esm and cjs.
MIT License
29 stars 1 forks source link

用户生产环境时,自定义目录,动态引入的文件,不会附加上vscode 的uri,导致无法使用。 #6

Closed programmermark closed 3 months ago

programmermark commented 3 months ago

我在项目中使用冷了多页的方式去实现。 自定义目录后,部分动态引入的文件由于没有在html文件中声明,打包后引入时是绝对路径,导致404. 可以在构建时,设置 base为:'./'(vite.config.ts)。 这样动态引入时是相对路径,vscode会附加uri。但是html文件中引入的js、css是相对路径。

需要在插件为这些资源附加 vscode的uri时,替换相对路径(../ ../../等)为绝对路径(/),再拼接vscode的uri。

const attr = element.getAttribute(tag[tag]);
if(attr) {
/** 替换开头的‘./’ '../'等为 ‘/’ */
 let attrWithBaseUri = replaceRelativePathToAbsolutePath(attr);
attrWithBaseUri = `{{baseUri}}${attrWithBaseUri}`;
element.setAttribute(tag[tag], attrWithBaseUri);
}
tomgao365 commented 3 months ago

先试试这个方式?

https://github.com/tomjs/vite-plugin-vscode/blob/94057884d33f8da730561455e9d45d396817f32b/examples/vue-import/vite.config.ts#L28-L31

programmermark commented 3 months ago

这个我有看过,对于动态引入的页面是没法适用的。

先试试这个方式?

https://github.com/tomjs/vite-plugin-vscode/blob/94057884d33f8da730561455e9d45d396817f32b/examples/vue-import/vite.config.ts#L28-L31

tomgao365 commented 3 months ago

只能避免使用动态引用,不在 HTML 引入的资源文件,无法动态加载。