当前位置: 首页 > 后端技术 > PHP

找出vue中引入但未使用的组件

时间:2023-03-29 20:46:12 PHP

这是什么?在使用vue的时候,有时候我们会因为各种原因导入一些组件,但是最终没有使用。所以我写了这个php文件来找出导入但未使用的组件。为什么phpJavaScript不能访问本地文件,node.js我不能。如果您没有使用过php,但想使用它。可以自己搭建php环境,下一个windows下的wamp可以一键安装。使用方法复制最下面的代码,编辑check.php文件的第一行,将''中的内容替换为你的src路径constPATH='你的vue项目的src路径';将其作为check.php保存到www目录下,然后浏览器访问http://localhost/check.phpcode$v){if(is_array($v)){getPath($v,$path.'/'.$k);}elseif(is_string($v)&&strpos($v,'.vue')!==false){searchNoUseComponents($path.'/'.$v);}}}}//将驼峰更改为破折号以分隔名称functionhumpToLine($str){$str=lcfirst($str);$str=preg_replace_callback('/(([A-Z]|[0-9]){1})/',function($matches){return'-'.strtolower($matches[0]);},$str);return$str;}//在vue中搜索导入但未使用的组件functionsearchNoUseComponents($path){if(file_exists($path)){$flag=0;$myFile=fopen($path,'r');$组件=[];$originComponents=[];while(!feof($myFile)){$line=fgets($myFile);如果(strpos($line,'components:{}')!==false){break;}elseif(strpos($line,'components:{')!==false){$flag=1;}elseif($flag==1&&strpos($行,'}')===false){$components[]=humpToLine(trim(trim($line),','));$originComponents[]=trim(trim($line),',');}elseif($flag==1&&strpos($line,'}')!==false){break;}}fclose($myFile);$res=fopen($path,'r');$vue=fread($res,文件大小($path));foreach($componentsas$k=>$v){if(strpos($vue,'<'.$v)===false){echoltrim($path,PATH).“内部组件”。$originComponents[$k]。'进口但未使用'。"
";}}}}https://github.com/wangshanta...