Skip to content

Commit a6a3348

Browse files
authored
[client-preset] feat: add skipIndexFile option to skip index.ts generation (#10946)
1 parent 2e79816 commit a6a3348

4 files changed

Lines changed: 142 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/client-preset': minor
3+
---
4+
5+
Add `skipIndexFile` preset config option to `client-preset` to allow disabling generation of the `index.ts` barrel file that re-exports the other generated files. Defaults to `false` (unchanged behavior); set to `true` to skip generating `index.ts`, for example when your project avoids barrel files for tree-shaking or lint rule reasons.

‎packages/presets/client/src/index.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ export type ClientPresetConfig = {
9898
*/
9999
hashAlgorithm?: 'sha1' | 'sha256' | (string & {}) | ((operation: string) => string);
100100
};
101+
/**
102+
* @description Skip generating the `index.ts` barrel file that re-exports the other generated files.
103+
* @default false
104+
*
105+
* @exampleMarkdown
106+
* ```tsx
107+
* const config = {
108+
* schema: 'https://graphql.org/graphql/',
109+
* documents: ['src/**\/*.tsx', '!src\/gql/**\/*'],
110+
* generates: {
111+
* './src/gql/': {
112+
* preset: 'client',
113+
* presetConfig: {
114+
* skipIndexFile: true,
115+
* }
116+
* },
117+
* },
118+
* };
119+
* export default config;
120+
* ```
121+
*/
122+
skipIndexFile?: boolean;
101123
};
102124

103125
const isOutputFolderLike = (baseOutputDir: string) => baseOutputDir.endsWith('/');
@@ -292,7 +314,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
292314

293315
let indexFileGenerateConfig: Types.GenerateOptions | null = null;
294316

295-
if (reexports.length) {
317+
if (reexports.length && !options.presetConfig?.skipIndexFile) {
296318
indexFileGenerateConfig = {
297319
filename: `${options.baseOutputDir}index.ts`,
298320
pluginMap: {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { executeCodegen } from '@graphql-codegen/cli';
2+
import { preset } from '../src/index.js';
3+
4+
describe('client-preset - presetConfig.skipIndexFile', () => {
5+
const schema = [
6+
/* GraphQL */ `
7+
type Query {
8+
a: String
9+
b: String
10+
c: String
11+
}
12+
`,
13+
];
14+
const documents = /* GraphQL */ `
15+
query A {
16+
a
17+
}
18+
`;
19+
20+
it('generates index.ts by default', async () => {
21+
const { result } = await executeCodegen({
22+
schema,
23+
documents,
24+
generates: {
25+
'out1/': {
26+
preset,
27+
},
28+
},
29+
});
30+
31+
expect(result).toHaveLength(4);
32+
const indexFile = result.find(file => file.filename === 'out1/index.ts');
33+
expect(indexFile.content).toEqual(`export * from "./fragment-masking";
34+
export * from "./gql";`);
35+
});
36+
37+
it('does not generate index.ts when `skipIndexFile: true`', async () => {
38+
const { result } = await executeCodegen({
39+
schema,
40+
documents,
41+
generates: {
42+
'out1/': {
43+
preset,
44+
presetConfig: {
45+
skipIndexFile: true,
46+
},
47+
},
48+
},
49+
});
50+
51+
expect(result).toHaveLength(3);
52+
const indexFile = result.find(file => file.filename === 'out1/index.ts');
53+
expect(indexFile).toBeUndefined();
54+
});
55+
56+
it('generates index.ts when `skipIndexFile: false`', async () => {
57+
const { result } = await executeCodegen({
58+
schema,
59+
documents,
60+
generates: {
61+
'out1/': {
62+
preset,
63+
presetConfig: {
64+
skipIndexFile: false,
65+
},
66+
},
67+
},
68+
});
69+
70+
expect(result).toHaveLength(4);
71+
const indexFile = result.find(file => file.filename === 'out1/index.ts');
72+
expect(indexFile.content).toEqual(`export * from "./fragment-masking";
73+
export * from "./gql";`);
74+
});
75+
});

‎website/content/plugins/presets/preset-client.mdx‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,45 @@ const client = new createClient({
635635
})
636636
```
637637

638+
## Index File
639+
640+
By default, the `client-preset` generates an `index.ts` file that re-exports the other generated
641+
files (`gql.ts`, and `fragment-masking.ts` when Fragment Masking is enabled), so you can import
642+
everything from the output directory itself:
643+
644+
```ts
645+
import { graphql } from './gql'
646+
```
647+
648+
Some projects prefer to avoid barrel files, for example because they interfere with tree-shaking or
649+
because of a lint rule against them. In that case, `index.ts` generation can be disabled with the
650+
`skipIndexFile` option:
651+
652+
```ts title="codegen.ts" {9-11}
653+
import { type CodegenConfig } from '@graphql-codegen/cli'
654+
655+
const config: CodegenConfig = {
656+
schema: 'schema.graphql',
657+
documents: ['src/**/*.tsx', '!src/gql/**/*'],
658+
generates: {
659+
'./src/gql/': {
660+
preset: 'client',
661+
presetConfig: {
662+
skipIndexFile: true
663+
}
664+
}
665+
}
666+
}
667+
668+
export default config
669+
```
670+
671+
With `skipIndexFile` enabled, import from the specific generated files instead:
672+
673+
```ts
674+
import { graphql } from './gql/gql'
675+
```
676+
638677
## Reducing Bundle Size
639678

640679
Large scale projects might want to enable code splitting or tree shaking on the `client-preset`

0 commit comments

Comments
 (0)