Home > Web Front-end > JS Tutorial > How to use PHP to find components that have been imported but not used in Vue

How to use PHP to find components that have been imported but not used in Vue

不言
Release: 2018-08-01 11:25:20
Original
1606 people have browsed it

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.

Why is php

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.

How to use

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路径';
Copy after login

Save as check. php to the www directory, and then access http://localhost/check.php

code

<?php
const PATH = &#39;你的vue项目的src路径&#39;;

getPath(my_dir(PATH), PATH);
echo &#39;------------------------end------------------------&#39;;

// 遍历目录下所有文件夹和文件
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 &#39;文件夹路径有误,请检查路径&#39;;
        exit(0);
    }
}

// 根据遍历的内容找出路径 如果是vue文件就遍历他
function getPath($t, $path = &#39;&#39;)
{
    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, '<&#39; . $v) === false) {
                echo ltrim($path, PATH) . &#39; 内组件 &#39; . $originComponents[$k] . &#39; 导入但是未使用&#39; . "<br />";
            }
        }
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template