You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionnormalizeStringPosix(path,allowAboveRoot){varres='';varlastSegmentLength=0;varlastSlash=-1;vardots=0;varcode;for(vari=0;i<=path.length;++i){if(i<path.length)code=path.charCodeAt(i);elseif(code===47/*/*/)break;elsecode=47/*/*/;if(code===47/*/*/){if(lastSlash===i-1||dots===1){// NOOP}elseif(lastSlash!==i-1&&dots===2){if(res.length<2||lastSegmentLength!==2||res.charCodeAt(res.length-1)!==46/*.*/||res.charCodeAt(res.length-2)!==46/*.*/){if(res.length>2){varlastSlashIndex=res.lastIndexOf('/');if(lastSlashIndex!==res.length-1){if(lastSlashIndex===-1){res='';lastSegmentLength=0;}else{res=res.slice(0,lastSlashIndex);lastSegmentLength=res.length-1-res.lastIndexOf('/');}lastSlash=i;dots=0;continue;}}elseif(res.length===2||res.length===1){res='';lastSegmentLength=0;lastSlash=i;dots=0;continue;}}if(allowAboveRoot){if(res.length>0)res+='/..';elseres='..';lastSegmentLength=2;}}else{if(res.length>0)res+='/'+path.slice(lastSlash+1,i);elseres=path.slice(lastSlash+1,i);lastSegmentLength=i-lastSlash-1;}lastSlash=i;dots=0;}elseif(code===46/*.*/&&dots!==-1){++dots;}else{dots=-1;}}returnres;}/** * link 代码来源 [email protected] 包 * @returns */exportfunctionresolve(){varresolvedPath='';varresolvedAbsolute=false;varcwd;for(vari=arguments.length-1;i>=-1&&!resolvedAbsolute;i--){varpath;if(i>=0)path=arguments[i];else{if(cwd===undefined)// cwd = process.cwd();path='';}// Skip empty entriesif(path.length===0){continue;}resolvedPath=path+'/'+resolvedPath;resolvedAbsolute=path.charCodeAt(0)===47/*/*/;}// At this point the path should be resolved to a full absolute path, but// handle relative paths to be safe (might happen when process.cwd() fails)// Normalize the pathresolvedPath=normalizeStringPosix(resolvedPath,!resolvedAbsolute);if(resolvedAbsolute){if(resolvedPath.length>0)return'/'+resolvedPath;elsereturn'/';}elseif(resolvedPath.length>0){returnresolvedPath;}else{return'.';}}
基础环境
vite 移除了node的垫片包, 所以 , 所以,在使用 path-browserify 一定会报错,
process is not defined
解决办法
源码中, 作者没有去处理这个问题,那么, 我们将源码中的 resolve方法 copy到项目中即可
说白了 你就是要解决 path.resolve(‘/a’, 'b') --> /a/b 这个问题
第一步
复制
normalizeStringPosix
方法 和resolve
方法 到你项目的utils 里面,作为一个工具包第二步: 注释有关 process相关的,其实就一行, 然后将
path = ''
使用
我看有评论说 , 在
vite.config.js
给全局注入一个process
空对象,其实这里会执行process.cwd()
,空对象.cwd()
执行方法也会报错,除非提供完整的, 这个其实麻烦了一些。 可能还有直接引入 Node的 pollyfill 包, 也不是很好使用demo
输出:
解决问题不易: 如果可以, 掘金给我一个小小的 star 掘金地址 ,谢谢
The text was updated successfully, but these errors were encountered: