Home > Article > Web Front-end > How to use PHP to find components that have been imported but not used in Vue
This article introduces you to using PHP to find components that have been imported but not used in Vue. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
When using vue, sometimes due to various reasons we import some components but end up not using them. So I wrote this php file to find out the imported but unused components.
JavaScript cannot access local files, and I don’t know node.js.
If you have never used php, but want to use it. You can build a php environment by yourself, and wamp can be installed under Windows with one click.
Copy the bottom code, edit the first line of the check.php file, replace the content in '' with your src path
const PATH = '你的vue项目的src路径';
Save as check. php to the www directory, and then access http://localhost/check.php
<?php const 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 />"; } } } }
Recommended related articles:
How to export with Vue Excel table function
Use Xdebug to analyze PHP programs and find performance bottlenecks
The above is the detailed content of How to use PHP to find components that have been imported but not used in Vue. For more information, please follow other related articles on the PHP Chinese website!