@@ -26,6 +26,7 @@ export default class Searcher {
2626 #files: StaticFile [ ] ;
2727 #sourceData: Map < string , Partial < Data > > ;
2828 #cache = new Map < string , Data [ ] > ( ) ;
29+ #cacheFiles = new Map < string , string [ ] > ( ) ;
2930 #filters: Filter [ ] ;
3031
3132 constructor ( options : Options ) {
@@ -38,6 +39,7 @@ export default class Searcher {
3839 /** Clear the cache (used after a change in watch mode) */
3940 deleteCache ( ) {
4041 this . #cache. clear ( ) ;
42+ this . #cacheFiles. clear ( ) ;
4143 }
4244
4345 /**
@@ -78,19 +80,12 @@ export default class Searcher {
7880
7981 /** Search files using a glob */
8082 files ( globOrRegexp ?: RegExp | string ) : string [ ] {
81- const files = this . #files. map ( ( file ) => file . outputPath ) ;
82- const pages = this . #pages. map ( ( page ) => page . outputPath ) ;
83- const allFiles = [ ...files , ...pages ] ;
84-
85- if ( ! globOrRegexp ) {
86- return allFiles ;
87- }
88-
89- const regexp = typeof globOrRegexp === "string"
90- ? globToRegExp ( globOrRegexp )
91- : globOrRegexp ;
83+ return this . #searchFiles( globOrRegexp ) ;
84+ }
9285
93- return allFiles . filter ( ( file ) => regexp . test ( file ) ) ;
86+ /** Search a single file using a glob */
87+ file ( globOrRegexp ?: RegExp | string ) : string | undefined {
88+ return this . #searchFiles( globOrRegexp ) [ 0 ] ;
9489 }
9590
9691 /** Returns all values from the same key of a search */
@@ -154,6 +149,30 @@ export default class Searcher {
154149 this . #cache. set ( id , result ) ;
155150 return [ ...result ] as ( Data & T ) [ ] ;
156151 }
152+
153+ #searchFiles( globOrRegexp ?: RegExp | string ) : string [ ] {
154+ const id = typeof globOrRegexp === "string"
155+ ? globOrRegexp
156+ : globOrRegexp ?. source || "" ;
157+
158+ if ( this . #cacheFiles. has ( id ) ) {
159+ return [ ...this . #cacheFiles. get ( id ) ! ] ;
160+ }
161+
162+ const files = this . #files. map ( ( file ) => file . outputPath ) ;
163+ const pages = this . #pages. map ( ( page ) => page . outputPath ) ;
164+ let result : string [ ] = [ ...files , ...pages ] ;
165+
166+ if ( typeof globOrRegexp === "string" ) {
167+ const regexp = globToRegExp ( globOrRegexp ) ;
168+ result = result . filter ( ( file ) => regexp . test ( file ) ) ;
169+ } else if ( globOrRegexp instanceof RegExp ) {
170+ result = result . filter ( ( file ) => globOrRegexp . test ( file ) ) ;
171+ }
172+
173+ this . #cacheFiles. set ( id , result ) ;
174+ return [ ...result ] ;
175+ }
157176}
158177
159178/**
0 commit comments