electron-vite / vite-plugin-electron-renderer

Ployfill Node.js API for Renderer process

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Windows环境下无法开发 Error: path argument is undefined on Windows

muxue-wang opened this issue · comments

commented

错误在这一行
root = path.posix.join(root, ".."),
参数root = D:\Projects\Web\electron-demo\src\renderer,这个方法返回值【.】,此时进入调用方
node_modules_path = node_modules(config.root ? path.resolve(config.root) : process.cwd())[0];
node_modules_path = undefied
cache_dir = path.join(node_modules_path, CACHE_DIR);
这里抛出异常..

需要改成root = path.posix.join(normalizePath(root), "..")

image

好坑的 Windows 😅,壮士 can you submit a PR for fix it!
按照项目提供的 quick-start 模板就会出问题?有其他的配置改动么,还是在非 root 目录下执行的命令行呢。

commented

path.posix.join这个方法里面是按正斜线分割路径的,当有反斜杠时,会把含反斜杠整体路径当做同一级路径,所以Mac端是正常的,Windows获取不到期望结果。
可以新建一个文件,暂且命名为test.mjs,输入以下内容后,使用【node test.mjs】执行,可以复现
期望值:D:\Projects\Web\electron-demo\src
输出值:.
import path from 'path'

const res = path.posix.join('D:\Projects\Web\electron-demo\src\renderer','..');
console.log(res)

commented

issue 35和issue27都是这个问题,在0.13.0版本之前,也是解析不了路径的,只是会打个警告,不会加载相关模块,在0.13.0版本中,应该调用处改成了如下,path.join入参应该是string,传入undified所以直接抛出异常,导致程序中断,这个问题只出现在Windows开发环境中
node_modules_path = node_modules(config.root ? path.resolve(config.root) : process.cwd())[0];
cache_dir = path.join(node_modules_path, CACHE_DIR);

node_modules 内部做了 posix 路径处理,很迷茫
https://github.com/vite-plugin/vite-plugin-utils/blob/main/function/index.ts#L147

按理说这段代码最起码会返回一个路径 node_modules(config.root ? path.resolve(config.root) : process.cwd())

commented

p这个变量生命周期在这里结束了,对于入参变量root并没有处理过
https://github.com/vite-plugin/vite-plugin-utils/blob/72494e2e9e4b00c58a5ff617f9667688dda16556/function/index.ts#L148-L149

最终返回的是由若干个 p 组成的 paths,入参 root 不会被返回使用的

使用 v0.13.x 版本更好用 :)