Skip to content

Commit 9c546a7

Browse files
committed
feat: Support disabling codegen in certain modes
Adds a plugin config that provides support for disabling codegen under different modes
1 parent 92690a0 commit 9c546a7

6 files changed

Lines changed: 98 additions & 29 deletions

File tree

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Experimental zero-config vite plugin that uses the vite file watcher to run [gra
66

77
Installation instructions found [here](https://www.graphql-code-generator.com/docs/getting-started/installation). Optional if already set up in project.
88

9-
## Install
9+
## Install Plugin
1010

1111
```bash
1212
# npm
@@ -16,7 +16,7 @@ npm i -D vite-plugin-graphql-codegen
1616
yarn add -D vite-plugin-graphql-codegen
1717
```
1818

19-
## Initialize
19+
## Initialize Plugin
2020

2121
```js
2222
# vite.config.ts
@@ -31,4 +31,19 @@ export default defineConfig({
3131
});
3232
```
3333

34+
## Options
35+
36+
Providing options is not required as sensible defaults are in place, but there may be times where it's helpful to disable codegen under certain circumstances, like when running builds in CI.
37+
38+
```js
39+
codegen({
40+
/* Should codegen run when the dev server starts. Defaults to true. */
41+
runOnStart: true,
42+
/* Should codegen run on build. Will prevent build if codegen fails. Defaults to true. */
43+
runOnBuild: true,
44+
/* Should codegen run when files get added or change. Defaults to true. */
45+
enableWatcher: true
46+
})
47+
```
48+
3449
Project bootstrapped with [TSDX](https://github.com/palmerhq/tsdx).

src/helpers/isCodegenConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { CodegenContext } from '@graphql-codegen/cli';
2+
3+
export function isCodegenConfig(
4+
filePath: string,
5+
context: CodegenContext
6+
): boolean {
7+
const configPath = context.filepath;
8+
return configPath === filePath;
9+
}
Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs';
21
import { CodegenContext } from '@graphql-codegen/cli';
32
import { normalizeInstanceOrArray } from '@graphql-codegen/plugin-helpers';
43

@@ -22,23 +21,3 @@ export async function isGraphQLDocument(
2221

2322
return paths.some(documentPath => documentPath === filePath);
2423
}
25-
26-
export function isCodegenConfig(
27-
filePath: string,
28-
context: CodegenContext
29-
): boolean {
30-
const configPath = context.filepath;
31-
return configPath === filePath;
32-
}
33-
34-
export function restartVite(fileName: string) {
35-
if (!fileName) return;
36-
37-
const time = new Date();
38-
39-
try {
40-
fs.utimesSync(fileName, time, time);
41-
} catch (error) {
42-
fs.closeSync(fs.openSync(fileName, 'w'));
43-
}
44-
}

src/helpers/restartVite.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from 'fs';
2+
3+
export function restartVite(fileName: string) {
4+
if (!fileName) return;
5+
6+
const time = new Date();
7+
8+
try {
9+
fs.utimesSync(fileName, time, time);
10+
} catch (error) {
11+
fs.closeSync(fs.openSync(fileName, 'w'));
12+
}
13+
}

src/helpers/viteModes.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ConfigEnv } from 'vite';
2+
3+
export type ViteMode = ConfigEnv['command'];
4+
5+
const modes = {
6+
serve: 'serve',
7+
build: 'build',
8+
} as { [K in ViteMode]: K };
9+
10+
export const isServeMode = (mode: ViteMode) => mode === modes.serve;
11+
12+
export const isBuildMode = (mode: ViteMode) => mode === modes.build;

src/index.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
import fs from 'fs';
22
import { Plugin } from 'vite';
33
import { CodegenContext, generate, loadContext } from '@graphql-codegen/cli';
4-
import { isCodegenConfig, isGraphQLDocument, restartVite } from './helpers';
4+
import { isCodegenConfig } from './helpers/isCodegenConfig';
5+
import { isGraphQLDocument } from './helpers/isGraphQLDocument';
6+
import { restartVite } from './helpers/restartVite';
7+
import { ViteMode, isServeMode, isBuildMode } from './helpers/viteModes';
8+
9+
export interface Options {
10+
/**
11+
* Enable codegen in serve mode.
12+
* @defaultValue `true`
13+
*/
14+
runOnStart?: boolean;
15+
/**
16+
* Enable codegen in build mode.
17+
* @defaultValue `true`
18+
*/
19+
runOnBuild?: boolean;
20+
/**
21+
* Enable codegen integration with vite file watcher.
22+
* @defaultValue `true`
23+
*/
24+
enableWatcher?: boolean;
25+
}
526

627
const VITE_CONFIG_FILE_NAMES = ['vite.config.ts', 'vite.config.js'] as const;
728

8-
export default function VitePluginGraphQLCodegen(): Plugin {
29+
export default function VitePluginGraphQLCodegen(options?: Options): Plugin {
930
let codegenContext: CodegenContext;
31+
let viteMode: ViteMode;
1032
let viteConfigFileName: typeof VITE_CONFIG_FILE_NAMES[number];
1133

34+
const {
35+
runOnStart = true,
36+
runOnBuild = true,
37+
enableWatcher = true, //
38+
} = options ?? {};
39+
1240
return {
1341
name: 'graphql-codegen',
14-
async config(config) {
42+
async config(config, env) {
1543
codegenContext = await loadContext(config.root);
1644

1745
// Vite handles file watching
1846
codegenContext.updateConfig({ watch: false });
47+
48+
viteMode = env.command;
1949
},
2050
configResolved() {
2151
for (const fileName of VITE_CONFIG_FILE_NAMES) {
@@ -26,10 +56,19 @@ export default function VitePluginGraphQLCodegen(): Plugin {
2656
}
2757
},
2858
async buildStart() {
59+
const isServe = isServeMode(viteMode);
60+
const isBuild = isBuildMode(viteMode);
61+
62+
if (isServe && !runOnStart) return;
63+
if (isBuild && !runOnBuild) return;
64+
2965
try {
3066
await generate(codegenContext);
3167
} catch (error) {
32-
// GraphQL Codegen handles printing errors
68+
// Prevent build if codegen fails
69+
if (isBuild) throw error;
70+
71+
// GraphQL Codegen handles logging useful errors
3372
}
3473
},
3574
configureServer(server) {
@@ -41,18 +80,20 @@ export default function VitePluginGraphQLCodegen(): Plugin {
4180
return;
4281
}
4382

83+
if (!enableWatcher) return;
84+
4485
try {
4586
const isDocument = await isGraphQLDocument(filePath, codegenContext);
4687

4788
if (!isDocument) return;
4889
} catch {
49-
// GraphQL Codegen handles printing errors
90+
// GraphQL Codegen handles logging useful errors
5091
}
5192

5293
try {
5394
await generate(codegenContext);
5495
} catch (error) {
55-
// GraphQL Codegen handles printing errors
96+
// GraphQL Codegen handles logging useful errors
5697
}
5798
};
5899

0 commit comments

Comments
 (0)