<?php
const
PATH =
'你的vue项目的src路径'
;
getPath(my_dir(PATH), PATH);
echo
'------------------------end------------------------'
;
function
my_dir(
$dir
)
{
$files
=
array
();
if
(@
$handle
= opendir(
$dir
)) {
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);
}
}
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
;
}
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 />"
;
}
}
}
}