这篇文章给大家介绍的内容是关于利用php找出vue里已经导入但是未使用的组件,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
在使用vue的时候,有时候由于种种原因会使我们导入一些组件,最终却没有使用它。于是我就编写了这个php文件来找出已经导入但是未使用的组件。
为什么是phpjavascript不能访问本地文件,node.js我不会。
如果你没用过php,但是想使用。可以自己搭建一个php环境,windows下个wamp可以一键安装。
怎么使用复制底部代码,编辑check.php文件的第一行,替换''里的内容为你的src路径
const path = '你的vue项目的src路径';
保存为check.php到www目录下,然后游览器访问http://localhost/check.php
代码<?phpconst path = '你的vue项目的src路径';getpath(my_dir(path), path);echo '------------------------end------------------------';// 遍历目录下所有文件夹和文件function my_dir($dir){ $files = array(); if (@$handle = opendir($dir)) { //注意这里要加一个@,不然会有warning错误提示:) while (($file = readdir($handle)) !== false) { if ($file != ".." && $file != ".") { //排除根目录; if (is_dir($dir . "/" . $file)) { //如果是子文件夹,就进行递归 $files[$file] = my_dir($dir . "/" . $file); } else { //不然就将文件的名字存入数组; $files[] = $file; } } } closedir($handle); return $files; } else { echo '文件夹路径有误,请检查路径'; exit(0); }}// 根据遍历的内容找出路径 如果是vue文件就遍历他function getpath($t, $path = ''){ if (is_array($t)) { foreach ($t as $k => $v) { if (is_array($v)) { getpath($v, $path . '/' . $k); } else if (is_string($v) && strpos($v, '.vue') !== false) { searchnousecomponents($path . '/' . $v); } } }}// 把驼峰改成短横线分隔命名function humptoline($str){ $str = lcfirst($str); $str = preg_replace_callback('/(([a-z]|[0-9]){1})/', function ($matches) { return '-' . strtolower($matches[0]); }, $str); return $str;}// 寻找vue内导入却未使用的组件function searchnousecomponents($path){ if (file_exists($path)) { $flag = 0; $myfile = fopen($path, 'r'); $components = []; $origincomponents = []; while (!feof($myfile)) { $line = fgets($myfile); if (strpos($line, 'components: {}') !== false) { break; } else if (strpos($line, 'components: {') !== false) { $flag = 1; } else if ($flag == 1 && strpos($line, '}') === false) { $components[] = humptoline(trim(trim($line), ',')); $origincomponents[] = trim(trim($line), ','); } else if ($flag == 1 && strpos($line, '}') !== false) { break; } } fclose($myfile); $res = fopen($path, 'r'); $vue = fread($res, filesize($path)); foreach ($components as $k => $v) { if (strpos($vue, '<' . $v) === false) { echo ltrim($path, path) . ' 内组件 ' . $origincomponents[$k] . ' 导入但是未使用' . "<br />; } } }}
相关文章推荐:
怎么用vue导出excel表格功能
利用xdebug分析php程序,找出性能瓶颈
以上就是利用php找出vue里已经导入但是未使用的组件的方法的详细内容。