Skip to content

Commit 4224cec

Browse files
committed
feat: Support overwriting codegen config options
Add support for extending and overwriting codegen options in the context of the plugin
1 parent 9c546a7 commit 4224cec

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ codegen({
4343
runOnBuild: true,
4444
/* Should codegen run when files get added or change. Defaults to true. */
4545
enableWatcher: true
46+
/* Allows overriding codegen configuration options just in the context of this plugin. Useful if you prefer a cleaner log by passing { errorsOnly: true }. */
47+
configOverride: CodegenConfig
4648
})
4749
```
4850

src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import { Plugin } from 'vite';
33
import { CodegenContext, generate, loadContext } from '@graphql-codegen/cli';
4+
import { Types } from '@graphql-codegen/plugin-helpers';
45
import { isCodegenConfig } from './helpers/isCodegenConfig';
56
import { isGraphQLDocument } from './helpers/isGraphQLDocument';
67
import { restartVite } from './helpers/restartVite';
@@ -22,6 +23,10 @@ export interface Options {
2223
* @defaultValue `true`
2324
*/
2425
enableWatcher?: boolean;
26+
/**
27+
* Override codegen configuration just for this plugin.
28+
*/
29+
configOverride?: Partial<Types.Config>;
2530
}
2631

2732
const VITE_CONFIG_FILE_NAMES = ['vite.config.ts', 'vite.config.js'] as const;
@@ -34,7 +39,8 @@ export default function VitePluginGraphQLCodegen(options?: Options): Plugin {
3439
const {
3540
runOnStart = true,
3641
runOnBuild = true,
37-
enableWatcher = true, //
42+
enableWatcher = true,
43+
configOverride = {},
3844
} = options ?? {};
3945

4046
return {
@@ -43,7 +49,9 @@ export default function VitePluginGraphQLCodegen(options?: Options): Plugin {
4349
codegenContext = await loadContext(config.root);
4450

4551
// Vite handles file watching
46-
codegenContext.updateConfig({ watch: false });
52+
const watch = false;
53+
54+
codegenContext.updateConfig({ ...configOverride, watch });
4755

4856
viteMode = env.command;
4957
},
@@ -86,7 +94,7 @@ export default function VitePluginGraphQLCodegen(options?: Options): Plugin {
8694
const isDocument = await isGraphQLDocument(filePath, codegenContext);
8795

8896
if (!isDocument) return;
89-
} catch {
97+
} catch (error) {
9098
// GraphQL Codegen handles logging useful errors
9199
}
92100

0 commit comments

Comments
 (0)