Skip to content

Commit c0737a7

Browse files
committed
perf: skip match cache refresh if file is generated
1 parent 69c1d97 commit c0737a7

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
generate,
66
loadContext,
77
} from '@graphql-codegen/cli';
8-
import { isCodegenConfig } from './utils/fileMatchers';
8+
import { isCodegenConfig, isGeneratedFile } from './utils/fileMatchers';
99
import { isBuildMode, isServeMode, type ViteMode } from './utils/viteModes';
1010
import { debugLog } from './utils/debugLog';
1111
import { createMatchCache } from './utils/matchCache';
@@ -221,6 +221,11 @@ export function GraphQLCodegen(options?: Options): Plugin {
221221
server.watcher.on('add', async (filePath) => {
222222
log(`File added: ${filePath}`);
223223

224+
if (isGeneratedFile(filePath, codegenContext)) {
225+
log('File is a generated output file, skipping');
226+
return;
227+
}
228+
224229
try {
225230
log('Match cache refreshing');
226231
await matchCache.refresh();

src/utils/configPaths.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function getDocumentPaths(
1616
}
1717

1818
const normalized = sourceDocuments
19-
.filter((item): item is NonNullable<typeof item> => !!item)
19+
.filter((item) => item !== undefined)
2020
.flat();
2121

2222
if (!normalized.length) return [];
@@ -60,3 +60,9 @@ export async function getSchemaPaths(
6060
.filter(Boolean)
6161
.map(normalizePath);
6262
}
63+
64+
export function getGeneratesPaths(context: CodegenContext): string[] {
65+
const config = context.getConfig();
66+
67+
return Object.keys(config.generates).map(normalizePath);
68+
}

src/utils/fileMatchers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { normalizePath } from 'vite';
22
import type { CodegenContext } from '@graphql-codegen/cli';
3+
import { getGeneratesPaths } from './configPaths';
34

45
export function isCodegenConfig(filePath: string, context: CodegenContext) {
56
if (!context.filepath) return false;
67

78
return normalizePath(filePath) === normalizePath(context.filepath);
89
}
10+
11+
export function isGeneratedFile(filePath: string, context: CodegenContext) {
12+
const generatesPaths = getGeneratesPaths(context);
13+
14+
const normalizedFilePath = normalizePath(filePath);
15+
16+
return generatesPaths.some((path) => normalizedFilePath.includes(path));
17+
}

0 commit comments

Comments
 (0)