From c0737a7aab210b227f5395c2b0b04873262cf3bf Mon Sep 17 00:00:00 2001 From: Daniel Waltz Date: Sun, 1 Dec 2024 07:24:58 -0500 Subject: [PATCH] perf: skip match cache refresh if file is generated --- src/index.ts | 7 ++++++- src/utils/configPaths.ts | 8 +++++++- src/utils/fileMatchers.ts | 9 +++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 91bf4b7..97ad043 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { generate, loadContext, } from '@graphql-codegen/cli'; -import { isCodegenConfig } from './utils/fileMatchers'; +import { isCodegenConfig, isGeneratedFile } from './utils/fileMatchers'; import { isBuildMode, isServeMode, type ViteMode } from './utils/viteModes'; import { debugLog } from './utils/debugLog'; import { createMatchCache } from './utils/matchCache'; @@ -221,6 +221,11 @@ export function GraphQLCodegen(options?: Options): Plugin { server.watcher.on('add', async (filePath) => { log(`File added: ${filePath}`); + if (isGeneratedFile(filePath, codegenContext)) { + log('File is a generated output file, skipping'); + return; + } + try { log('Match cache refreshing'); await matchCache.refresh(); diff --git a/src/utils/configPaths.ts b/src/utils/configPaths.ts index 8ea6001..9c45a53 100644 --- a/src/utils/configPaths.ts +++ b/src/utils/configPaths.ts @@ -16,7 +16,7 @@ export async function getDocumentPaths( } const normalized = sourceDocuments - .filter((item): item is NonNullable => !!item) + .filter((item) => item !== undefined) .flat(); if (!normalized.length) return []; @@ -60,3 +60,9 @@ export async function getSchemaPaths( .filter(Boolean) .map(normalizePath); } + +export function getGeneratesPaths(context: CodegenContext): string[] { + const config = context.getConfig(); + + return Object.keys(config.generates).map(normalizePath); +} diff --git a/src/utils/fileMatchers.ts b/src/utils/fileMatchers.ts index ac588ff..16eeccd 100644 --- a/src/utils/fileMatchers.ts +++ b/src/utils/fileMatchers.ts @@ -1,8 +1,17 @@ import { normalizePath } from 'vite'; import type { CodegenContext } from '@graphql-codegen/cli'; +import { getGeneratesPaths } from './configPaths'; export function isCodegenConfig(filePath: string, context: CodegenContext) { if (!context.filepath) return false; return normalizePath(filePath) === normalizePath(context.filepath); } + +export function isGeneratedFile(filePath: string, context: CodegenContext) { + const generatesPaths = getGeneratesPaths(context); + + const normalizedFilePath = normalizePath(filePath); + + return generatesPaths.some((path) => normalizedFilePath.includes(path)); +}