@@ -5,14 +5,10 @@ import {
55 generate ,
66 loadContext ,
77} from '@graphql-codegen/cli' ;
8- import {
9- type FileMatcher ,
10- isCodegenConfig ,
11- isGraphQLDocument ,
12- isGraphQLSchema ,
13- } from './utils/fileMatchers' ;
8+ import { isCodegenConfig , isFileMatched } from './utils/fileMatchers' ;
149import { isBuildMode , isServeMode , type ViteMode } from './utils/viteModes' ;
1510import { debugLog } from './utils/debugLog' ;
11+ import { getDocumentPaths , getSchemaPaths } from './utils/configPaths' ;
1612
1713export interface Options {
1814 /**
@@ -178,56 +174,41 @@ export function GraphQLCodegen(options?: Options): Plugin {
178174 }
179175 }
180176 } ,
181- configureServer ( server ) {
177+ async configureServer ( server ) {
182178 if ( ! enableWatcher ) return ;
183179
184- const listener = async ( filePath = '' ) => {
185- log ( 'File changed:' , filePath ) ;
180+ const documentPaths = await getDocumentPaths ( codegenContext ) ;
181+ const schemaPaths = await getSchemaPaths ( codegenContext ) ;
186182
187- const isConfig = await isCodegenConfig ( filePath , codegenContext ) ;
183+ const isMatch = ( filePath : string ) => {
184+ let matched = false ;
185+ if ( matchOnDocuments ) {
186+ if ( isFileMatched ( filePath , documentPaths ) ) {
187+ matched = true ;
188+ log ( `Graphql document file matched: ${ filePath } ` ) ;
189+ }
190+ }
191+
192+ if ( matchOnSchemas ) {
193+ if ( isFileMatched ( filePath , schemaPaths ) ) {
194+ matched = true ;
195+ log ( `Graphql schema file matched: ${ filePath } ` ) ;
196+ }
197+ }
198+
199+ return matched ;
200+ } ;
201+
202+ const listener = async ( filePath = '' ) => {
203+ const isConfig = isCodegenConfig ( filePath , codegenContext . filepath ) ;
188204
189205 if ( isConfig ) {
190206 log ( 'Codegen config file changed, restarting vite' ) ;
191- server . restart ( ) ;
207+ await server . restart ( ) ;
192208 return ;
193209 }
194210
195- const matchers : [
196- enabled : boolean ,
197- name : string ,
198- matcher : FileMatcher ,
199- ] [ ] = [
200- [ matchOnDocuments , 'document' , isGraphQLDocument ] ,
201- [ matchOnSchemas , 'schema' , isGraphQLSchema ] ,
202- ] ;
203-
204- const matcherResults = await Promise . all (
205- matchers . map ( async ( [ enabled , name , matcher ] ) => {
206- if ( ! enabled ) {
207- log ( `Check for ${ name } file skipped in file watcher by config` ) ;
208- return false ;
209- }
210-
211- try {
212- const isMatch = await matcher ( filePath , codegenContext ) ;
213-
214- log ( `Check for ${ name } file successful in file watcher` ) ;
215-
216- if ( isMatch ) log ( `File matched a graphql ${ name } ` ) ;
217- else log ( `File did not match a graphql ${ name } ` ) ;
218-
219- return isMatch ;
220- } catch ( error ) {
221- // GraphQL Codegen handles logging useful errors
222- log ( `Check for ${ name } file failed in file watcher` ) ;
223- return false ;
224- }
225- } ) ,
226- ) ;
227-
228- const isMatch = matcherResults . some ( ( result ) => result ) ;
229-
230- if ( ! isMatch ) return ;
211+ if ( ! isMatch ( filePath ) ) return ;
231212
232213 try {
233214 await generateWithOverride ( configOverrideWatcher ) ;
0 commit comments