Skip to content

Commit

Permalink
feat: Support overwriting codegen config options
Browse files Browse the repository at this point in the history
Add support for extending and overwriting codegen options in the context
of the plugin
  • Loading branch information
danielwaltz committed Feb 1, 2022
1 parent 9c546a7 commit 4224cec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ codegen({
runOnBuild: true,
/* Should codegen run when files get added or change. Defaults to true. */
enableWatcher: true
/* Allows overriding codegen configuration options just in the context of this plugin. Useful if you prefer a cleaner log by passing { errorsOnly: true }. */
configOverride: CodegenConfig
})
```

Expand Down
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import { Plugin } from 'vite';
import { CodegenContext, generate, loadContext } from '@graphql-codegen/cli';
import { Types } from '@graphql-codegen/plugin-helpers';
import { isCodegenConfig } from './helpers/isCodegenConfig';
import { isGraphQLDocument } from './helpers/isGraphQLDocument';
import { restartVite } from './helpers/restartVite';
Expand All @@ -22,6 +23,10 @@ export interface Options {
* @defaultValue `true`
*/
enableWatcher?: boolean;
/**
* Override codegen configuration just for this plugin.
*/
configOverride?: Partial<Types.Config>;
}

const VITE_CONFIG_FILE_NAMES = ['vite.config.ts', 'vite.config.js'] as const;
Expand All @@ -34,7 +39,8 @@ export default function VitePluginGraphQLCodegen(options?: Options): Plugin {
const {
runOnStart = true,
runOnBuild = true,
enableWatcher = true, //
enableWatcher = true,
configOverride = {},
} = options ?? {};

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

// Vite handles file watching
codegenContext.updateConfig({ watch: false });
const watch = false;

codegenContext.updateConfig({ ...configOverride, watch });

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

if (!isDocument) return;
} catch {
} catch (error) {
// GraphQL Codegen handles logging useful errors
}

Expand Down

0 comments on commit 4224cec

Please sign in to comment.