@@ -3,7 +3,7 @@ import Fuse from 'fuse.js'
3
3
import { Minimatch } from 'minimatch'
4
4
import { rpc } from './rpc'
5
5
6
- export const list = ref < ModuleInfo [ ] > ( await rpc . componentGraph ( ) )
6
+ const list = ref < ModuleInfo [ ] > ( await rpc . componentGraph ( ) )
7
7
export const searchText = useStorage ( 'vite-inspect-search-text' , '' )
8
8
export const includeNodeModules = useStorage ( 'vite-inspect-include-node-modules' , false )
9
9
export const includeVirtual = useStorage ( 'vite-inspect-include-virtual' , false )
@@ -63,61 +63,86 @@ function uniqById(data: typeof list.value) {
63
63
return uniqueArray
64
64
}
65
65
66
- function fuzzySearchDeps ( data : typeof list . value , id : string ) {
66
+ function fuzzySearchDeps ( data : ModuleInfo [ ] , text : string ) {
67
67
const fuzzySearcher = new Fuse ( data , {
68
68
ignoreLocation : true ,
69
69
keys : [ 'id' ] ,
70
70
shouldSort : true ,
71
71
threshold : 0.1 ,
72
72
} )
73
- const result = fuzzySearcher . search ( id ) . map ( i => i . item )
74
- if ( ! result ) {
73
+ const ids = fuzzySearcher . search ( text ) . map ( i => i . item . id )
74
+ if ( ! ids . length ) {
75
75
return {
76
- main : [ ] ,
77
- allWithDeps : [ ] ,
76
+ matchedKeys : [ ] ,
77
+ data : [ ] ,
78
78
}
79
79
}
80
80
return {
81
- main : result ,
82
- allWithDeps : uniqById ( result . flatMap ( item => getDepsByExactId ( data , item . id ) ) ) ,
81
+ matchedKeys : ids ,
82
+ data : uniqById ( ids . flatMap ( id => getDepsByExactId ( data , id ) ) ) ,
83
83
}
84
84
}
85
85
86
- function filterByUserDefinedGlob ( data : typeof list . value ) {
87
- if ( ! userCustomGlobPattern . value . trim ( ) . length )
86
+ function filterByUserDefinedGlob ( data : ModuleInfo [ ] , pattern : string ) {
87
+ if ( ! pattern . trim ( ) . length )
88
88
return data
89
- const globPattern = userCustomGlobPattern . value . trim ( ) . split ( ', ' )
90
- const globInstances = new Map ( globPattern . map ( pattern => [ pattern , new Minimatch ( pattern , { matchBase : true } ) ] ) )
91
- return data . filter ( item => globPattern . every ( pattern =>
89
+ const globPatterns = pattern . trim ( ) . split ( ', ' )
90
+ const globInstances = new Map ( globPatterns . map ( pattern => [ pattern , new Minimatch ( pattern , { matchBase : true , dot : true , partial : true } ) ] ) )
91
+ return data . filter ( item => globPatterns . every ( pattern =>
92
92
globInstances . get ( pattern ) ! . match ( item . id ) ,
93
93
) )
94
94
}
95
95
96
96
const { graphSettings } = useGraphSettings ( )
97
97
98
- export const searchResults = computed ( ( ) => {
99
- let data = (
100
- list . value
101
- ) || [ ]
102
-
103
- if ( ! includeNodeModules . value )
104
- data = data . filter ( item => ! item . id . includes ( '/node_modules/' ) )
105
- if ( ! includeVirtual . value )
106
- data = data . filter ( item => ! item . virtual )
107
-
108
- if ( graphSettings . value . enableUserDefinedGlob )
109
- data = filterByUserDefinedGlob ( data )
110
-
111
- if ( ! searchText . value ) {
112
- return {
113
- main : [ ] ,
114
- data,
98
+ const getDebounceTime = ( len : number ) => len > 85 ? 350 : 150
99
+ export const searchResults = ref < ModuleInfo [ ] > ( [ ] )
100
+ export const matchedKeys = ref < string [ ] > ( [ ] )
101
+
102
+ const filterer = {
103
+ excludeNodeModules : ( data : ModuleInfo [ ] ) => {
104
+ return includeNodeModules . value ? data : data . filter ( item => ! item . id . includes ( '/node_modules/' ) )
105
+ } ,
106
+ excludeVirtual : ( data : ModuleInfo [ ] ) => {
107
+ return includeVirtual . value ? data : data . filter ( item => ! item . virtual )
108
+ } ,
109
+ userCustomGlobPattern : ( data : ModuleInfo [ ] , pattern : string ) => {
110
+ if ( ! graphSettings . value . enableUserDefinedGlob || ! pattern . trim ( ) . length )
111
+ return data
112
+ return filterByUserDefinedGlob ( data , pattern )
113
+ } ,
114
+ searchText : ( data : ModuleInfo [ ] , text : string ) => {
115
+ if ( ! text . trim ( ) . length ) {
116
+ matchedKeys . value = [ ]
117
+ return data
115
118
}
116
- }
119
+ const { data : searchData , matchedKeys : searchMatchedKeys } = fuzzySearchDeps ( data , searchText . value . trim ( ) )
120
+ matchedKeys . value = searchMatchedKeys
121
+ return searchData
122
+ } ,
123
+ }
117
124
118
- const { main, allWithDeps } = fuzzySearchDeps ( data , searchText . value . trim ( ) )
119
- return {
120
- main,
121
- data : allWithDeps ,
122
- }
125
+ const allDataCanBeSearched = computed ( ( ) => {
126
+ return filterer . excludeVirtual (
127
+ filterer . excludeNodeModules ( list . value ) ,
128
+ )
123
129
} )
130
+
131
+ debouncedWatch ( [ searchText , userCustomGlobPattern , allDataCanBeSearched ] , ( [ , , list ] ) => {
132
+ filterData ( false , list )
133
+ } , { debounce : computed ( ( ) => getDebounceTime ( searchResults . value . length ) ) } )
134
+
135
+ async function filterData ( isInit = false , givenData ?: ModuleInfo [ ] ) {
136
+ let data : ModuleInfo [ ] = givenData ?? [ ]
137
+ if ( isInit )
138
+ data = list . value = await rpc . componentGraph ( )
139
+ data = filterer . searchText (
140
+ filterer . userCustomGlobPattern (
141
+ filterer . excludeVirtual (
142
+ filterer . excludeNodeModules ( data ) ,
143
+ ) , userCustomGlobPattern . value )
144
+ , searchText . value )
145
+ searchResults . value = data
146
+ }
147
+
148
+ await filterData ( true )
0 commit comments