Skip to content

Commit

Permalink
perf: skip match cache refresh if file is generated
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwaltz committed Dec 1, 2024
1 parent 69c1d97 commit c0737a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/utils/configPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function getDocumentPaths(
}

const normalized = sourceDocuments
.filter((item): item is NonNullable<typeof item> => !!item)
.filter((item) => item !== undefined)
.flat();

if (!normalized.length) return [];
Expand Down Expand Up @@ -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);
}
9 changes: 9 additions & 0 deletions src/utils/fileMatchers.ts
Original file line number Diff line number Diff line change
@@ -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));
}

0 comments on commit c0737a7

Please sign in to comment.