11import { Plugin , ResolvedConfig } from 'vite' ;
22import { CodegenContext , generate , loadContext } from '@graphql-codegen/cli' ;
3- import {
4- normalizeInstanceOrArray ,
5- Types ,
6- } from '@graphql-codegen/plugin-helpers' ;
7- import minimatch from 'minimatch' ;
3+ import { createMatcher , isDocumentMatch , isGeneratedMatch } from './matchers' ;
84
95export default function VitePluginGraphQLCodegen ( ) : Plugin {
106 let viteConfig : ResolvedConfig ;
117 let codegenContext : CodegenContext ;
128
13- const getRelativePath = ( path : string ) => {
14- return path . split ( `${ viteConfig . root } /` ) . slice ( - 1 ) [ 0 ] ;
15- } ;
16-
179 return {
1810 name : 'graphql-codegen' ,
1911 async config ( config ) {
@@ -33,36 +25,14 @@ export default function VitePluginGraphQLCodegen(): Plugin {
3325 }
3426 } ,
3527 configureServer ( server ) {
36- const listener = async ( path = '' ) => {
37- const relativePath = getRelativePath ( path ) ;
38- const match = ( pattern : string ) => minimatch ( relativePath , pattern ) ;
28+ const listener = async ( absolutePath = '' ) => {
29+ const matcher = createMatcher ( absolutePath , viteConfig ) ;
3930
40- const codegenConfig = codegenContext . getConfig < Types . Config > ( ) ;
41- const { generates = { } , documents } = codegenConfig ;
31+ const isGenerated = await isGeneratedMatch ( matcher , codegenContext ) ;
32+ if ( isGenerated ) return ;
4233
43- // If modified file is generated
44- for ( const [ genPath , genConfig ] of Object . entries ( generates ) ) {
45- // If generated path is file
46- const fileExt = genPath . split ( '.' ) . slice ( - 1 ) [ 0 ] ;
47- const isFile = ! ! fileExt ;
48- if ( isFile && match ( genPath ) ) return ;
49-
50- // If generated path is directory
51- const { preset = '' , presetConfig } = genConfig ;
52- const isNearOperationFilePreset = preset === 'near-operation-file' ;
53- const presetExt = presetConfig ?. extension ?? '.generated.ts' ;
54- if ( isNearOperationFilePreset && match ( `**/*${ presetExt } ` ) ) return ;
55- }
56-
57- // If modified file is operation document
58- if ( ! documents ) return ;
59- const normalized = normalizeInstanceOrArray ( documents ) ;
60- const documentFiles = codegenContext . loadDocuments ( normalized ) ;
61-
62- for ( const docFile of await documentFiles ) {
63- if ( ! docFile . location ) break ;
64- if ( ! match ( getRelativePath ( docFile . location ) ) ) return ;
65- }
34+ const isDocument = await isDocumentMatch ( matcher , codegenContext ) ;
35+ if ( ! isDocument ) return ;
6636
6737 try {
6838 await generate ( codegenContext ) ;
0 commit comments