Skip to content

Commit d94ae3d

Browse files
authored
Fix: add file extenstions to import for ESM modules (#10314)
1 parent c5efba3 commit d94ae3d

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

.changeset/stale-swans-serve.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/graphql-modules-preset': patch
3+
---
4+
5+
Fix generated imports for graphql-modules-preset: .js extension is used.

packages/presets/graphql-modules/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export const preset: Types.OutputPreset<ModulesConfig> = {
7272
documentTransforms: options.documentTransforms,
7373
};
7474

75-
const baseTypesFilename = baseTypesPath.replace(/\.(js|ts|d.ts)$/, '');
75+
const baseTypesFilename = baseTypesPath.replace(
76+
/\.(js|ts|d.ts)$/,
77+
// we need extension if ESM modules are used
78+
options.config.emitLegacyCommonJSImports ? '' : '.js'
79+
);
7680
const baseTypesDir = stripFilename(baseOutput.filename);
7781

7882
// One file per each module

packages/presets/graphql-modules/tests/integration.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,19 @@ describe('Integration', () => {
198198
};
199199
`);
200200
});
201+
202+
test('import paths for ESM should have correct extension', async () => {
203+
const emitLegacyCommonJSImports = {
204+
emitLegacyCommonJSImports: false,
205+
...options,
206+
};
207+
const output = await executeCodegen(emitLegacyCommonJSImports);
208+
const esmImportStatement = `import * as Types from "../global-types.js";`;
209+
210+
expect(output.length).toBe(5);
211+
expect(output[1].content).toMatch(esmImportStatement);
212+
expect(output[2].content).toMatch(esmImportStatement);
213+
expect(output[3].content).toMatch(esmImportStatement);
214+
expect(output[4].content).toMatch(esmImportStatement);
215+
});
201216
});

0 commit comments

Comments
 (0)