@graphql-codegen/visitor-plugin-common 6.1.2 → 6.2.0-alpha-20251119131248-4aa0bce27decc294509fae591f5c2236858abdbb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,11 +4,16 @@ exports.BaseVisitor = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
6
  const naming_js_1 = require("./naming.js");
7
+ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
7
8
  class BaseVisitor {
8
9
  _parsedConfig;
9
10
  _declarationBlockConfig = {};
10
11
  scalars;
11
12
  constructor(rawConfig, additionalConfig) {
13
+ const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
14
+ emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports,
15
+ importExtension: rawConfig.importExtension,
16
+ });
12
17
  this._parsedConfig = {
13
18
  convert: (0, naming_js_1.convertFactory)(rawConfig),
14
19
  typesPrefix: rawConfig.typesPrefix || '',
@@ -20,7 +25,8 @@ class BaseVisitor {
20
25
  useTypeImports: !!rawConfig.useTypeImports,
21
26
  allowEnumStringTypes: !!rawConfig.allowEnumStringTypes,
22
27
  inlineFragmentTypes: rawConfig.inlineFragmentTypes ?? 'inline',
23
- emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports === undefined ? true : !!rawConfig.emitLegacyCommonJSImports,
28
+ emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports ?? true,
29
+ importExtension,
24
30
  extractAllFieldsToTypes: rawConfig.extractAllFieldsToTypes ?? false,
25
31
  printFieldsOnNewLines: rawConfig.printFieldsOnNewLines ?? false,
26
32
  includeExternalFragments: rawConfig.includeExternalFragments ?? false,
@@ -296,7 +296,11 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
296
296
  }
297
297
  clearExtension(path) {
298
298
  const extension = (0, path_1.extname)(path);
299
- if (!this.config.emitLegacyCommonJSImports && extension === '.js') {
299
+ const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
300
+ emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
301
+ importExtension: this.config.importExtension,
302
+ });
303
+ if (extension === importExtension) {
300
304
  return path;
301
305
  }
302
306
  if (EXTENSIONS_TO_REMOVE.includes(extension)) {
@@ -330,9 +334,10 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
330
334
  if (this._collectedOperations.length > 0) {
331
335
  if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' && this._documents.length === 1) {
332
336
  let documentPath = `./${this.clearExtension((0, path_1.basename)(this._documents[0].location))}`;
333
- if (!this.config.emitLegacyCommonJSImports) {
334
- documentPath += '.js';
335
- }
337
+ documentPath += (0, plugin_helpers_1.normalizeImportExtension)({
338
+ emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
339
+ importExtension: this.config.importExtension,
340
+ });
336
341
  this._imports.add(`import * as Operations from '${documentPath}';`);
337
342
  }
338
343
  else {
@@ -350,6 +355,10 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
350
355
  }
351
356
  const excludeFragments = options.excludeFragments || this.config.globalNamespace || this.config.documentMode !== DocumentMode.graphQLTag;
352
357
  if (!excludeFragments) {
358
+ const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
359
+ emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
360
+ importExtension: this.config.importExtension,
361
+ });
353
362
  const deduplicatedImports = Object.values((0, utils_js_1.groupBy)(this.config.fragmentImports, fi => fi.importSource.path))
354
363
  .map((fragmentImports) => ({
355
364
  ...fragmentImports[0],
@@ -358,6 +367,7 @@ class ClientSideBaseVisitor extends base_visitor_js_1.BaseVisitor {
358
367
  identifiers: (0, utils_js_1.unique)((0, utils_js_1.flatten)(fragmentImports.map(fi => fi.importSource.identifiers)), identifier => identifier.name),
359
368
  },
360
369
  emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
370
+ importExtension,
361
371
  }))
362
372
  .filter(fragmentImport => fragmentImport.outputPath !== fragmentImport.importSource.path);
363
373
  for (const fragmentImport of deduplicatedImports) {
package/cjs/imports.js CHANGED
@@ -9,6 +9,7 @@ exports.fixLocalFilePath = fixLocalFilePath;
9
9
  const tslib_1 = require("tslib");
10
10
  const path_1 = require("path");
11
11
  const parse_filepath_1 = tslib_1.__importDefault(require("parse-filepath"));
12
+ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
12
13
  function generateFragmentImportStatement(statement, kind) {
13
14
  const { importSource: fragmentImportSource, ...rest } = statement;
14
15
  const { identifiers, path, namespace } = fragmentImportSource;
@@ -31,7 +32,12 @@ function generateImportStatement(statement) {
31
32
  const importNames = importSource.identifiers?.length
32
33
  ? `{ ${Array.from(new Set(importSource.identifiers)).join(', ')} }`
33
34
  : '*';
34
- const importExtension = importPath.startsWith('/') || importPath.startsWith('.') ? (statement.emitLegacyCommonJSImports ? '' : '.js') : '';
35
+ const importExtension = importPath.startsWith('/') || importPath.startsWith('.')
36
+ ? (0, plugin_helpers_1.normalizeImportExtension)({
37
+ emitLegacyCommonJSImports: statement.emitLegacyCommonJSImports,
38
+ importExtension: statement.importExtension,
39
+ })
40
+ : '';
35
41
  const importAlias = importSource.namespace ? ` as ${importSource.namespace}` : '';
36
42
  const importStatement = typesImport ? 'import type' : 'import';
37
43
  return `${importStatement} ${importNames}${importAlias} from '${importPath}${importExtension}';${importAlias ? '\n' : ''}`;
@@ -1,10 +1,15 @@
1
1
  import autoBind from 'auto-bind';
2
2
  import { convertFactory } from './naming.js';
3
+ import { normalizeImportExtension } from '@graphql-codegen/plugin-helpers';
3
4
  export class BaseVisitor {
4
5
  _parsedConfig;
5
6
  _declarationBlockConfig = {};
6
7
  scalars;
7
8
  constructor(rawConfig, additionalConfig) {
9
+ const importExtension = normalizeImportExtension({
10
+ emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports,
11
+ importExtension: rawConfig.importExtension,
12
+ });
8
13
  this._parsedConfig = {
9
14
  convert: convertFactory(rawConfig),
10
15
  typesPrefix: rawConfig.typesPrefix || '',
@@ -16,7 +21,8 @@ export class BaseVisitor {
16
21
  useTypeImports: !!rawConfig.useTypeImports,
17
22
  allowEnumStringTypes: !!rawConfig.allowEnumStringTypes,
18
23
  inlineFragmentTypes: rawConfig.inlineFragmentTypes ?? 'inline',
19
- emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports === undefined ? true : !!rawConfig.emitLegacyCommonJSImports,
24
+ emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports ?? true,
25
+ importExtension,
20
26
  extractAllFieldsToTypes: rawConfig.extractAllFieldsToTypes ?? false,
21
27
  printFieldsOnNewLines: rawConfig.printFieldsOnNewLines ?? false,
22
28
  includeExternalFragments: rawConfig.includeExternalFragments ?? false,
@@ -1,5 +1,5 @@
1
1
  import { basename, extname } from 'path';
2
- import { oldVisit } from '@graphql-codegen/plugin-helpers';
2
+ import { normalizeImportExtension, oldVisit } from '@graphql-codegen/plugin-helpers';
3
3
  import { optimizeDocumentNode } from '@graphql-tools/optimize';
4
4
  import autoBind from 'auto-bind';
5
5
  import { pascalCase } from 'change-case-all';
@@ -292,7 +292,11 @@ export class ClientSideBaseVisitor extends BaseVisitor {
292
292
  }
293
293
  clearExtension(path) {
294
294
  const extension = extname(path);
295
- if (!this.config.emitLegacyCommonJSImports && extension === '.js') {
295
+ const importExtension = normalizeImportExtension({
296
+ emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
297
+ importExtension: this.config.importExtension,
298
+ });
299
+ if (extension === importExtension) {
296
300
  return path;
297
301
  }
298
302
  if (EXTENSIONS_TO_REMOVE.includes(extension)) {
@@ -326,9 +330,10 @@ export class ClientSideBaseVisitor extends BaseVisitor {
326
330
  if (this._collectedOperations.length > 0) {
327
331
  if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file' && this._documents.length === 1) {
328
332
  let documentPath = `./${this.clearExtension(basename(this._documents[0].location))}`;
329
- if (!this.config.emitLegacyCommonJSImports) {
330
- documentPath += '.js';
331
- }
333
+ documentPath += normalizeImportExtension({
334
+ emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
335
+ importExtension: this.config.importExtension,
336
+ });
332
337
  this._imports.add(`import * as Operations from '${documentPath}';`);
333
338
  }
334
339
  else {
@@ -346,6 +351,10 @@ export class ClientSideBaseVisitor extends BaseVisitor {
346
351
  }
347
352
  const excludeFragments = options.excludeFragments || this.config.globalNamespace || this.config.documentMode !== DocumentMode.graphQLTag;
348
353
  if (!excludeFragments) {
354
+ const importExtension = normalizeImportExtension({
355
+ emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
356
+ importExtension: this.config.importExtension,
357
+ });
349
358
  const deduplicatedImports = Object.values(groupBy(this.config.fragmentImports, fi => fi.importSource.path))
350
359
  .map((fragmentImports) => ({
351
360
  ...fragmentImports[0],
@@ -354,6 +363,7 @@ export class ClientSideBaseVisitor extends BaseVisitor {
354
363
  identifiers: unique(flatten(fragmentImports.map(fi => fi.importSource.identifiers)), identifier => identifier.name),
355
364
  },
356
365
  emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
366
+ importExtension,
357
367
  }))
358
368
  .filter(fragmentImport => fragmentImport.outputPath !== fragmentImport.importSource.path);
359
369
  for (const fragmentImport of deduplicatedImports) {
package/esm/imports.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { dirname, isAbsolute, join, relative, resolve } from 'path';
2
2
  import parse from 'parse-filepath';
3
+ import { normalizeImportExtension } from '@graphql-codegen/plugin-helpers';
3
4
  export function generateFragmentImportStatement(statement, kind) {
4
5
  const { importSource: fragmentImportSource, ...rest } = statement;
5
6
  const { identifiers, path, namespace } = fragmentImportSource;
@@ -22,7 +23,12 @@ export function generateImportStatement(statement) {
22
23
  const importNames = importSource.identifiers?.length
23
24
  ? `{ ${Array.from(new Set(importSource.identifiers)).join(', ')} }`
24
25
  : '*';
25
- const importExtension = importPath.startsWith('/') || importPath.startsWith('.') ? (statement.emitLegacyCommonJSImports ? '' : '.js') : '';
26
+ const importExtension = importPath.startsWith('/') || importPath.startsWith('.')
27
+ ? normalizeImportExtension({
28
+ emitLegacyCommonJSImports: statement.emitLegacyCommonJSImports,
29
+ importExtension: statement.importExtension,
30
+ })
31
+ : '';
26
32
  const importAlias = importSource.namespace ? ` as ${importSource.namespace}` : '';
27
33
  const importStatement = typesImport ? 'import type' : 'import';
28
34
  return `${importStatement} ${importNames}${importAlias} from '${importPath}${importExtension}';${importAlias ? '\n' : ''}`;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "6.1.2",
3
+ "version": "6.2.0-alpha-20251119131248-4aa0bce27decc294509fae591f5c2236858abdbb",
4
4
  "peerDependencies": {
5
5
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
6
6
  },
7
7
  "dependencies": {
8
8
  "@graphql-tools/optimize": "^2.0.0",
9
- "@graphql-codegen/plugin-helpers": "^6.0.0",
9
+ "@graphql-codegen/plugin-helpers": "6.1.0-alpha-20251119131248-4aa0bce27decc294509fae591f5c2236858abdbb",
10
10
  "@graphql-tools/relay-operation-optimizer": "^7.0.0",
11
11
  "@graphql-tools/utils": "^10.0.0",
12
12
  "auto-bind": "~4.0.0",
@@ -21,7 +21,8 @@ export interface ParsedConfig {
21
21
  useTypeImports: boolean;
22
22
  allowEnumStringTypes: boolean;
23
23
  inlineFragmentTypes: InlineFragmentTypeOptions;
24
- emitLegacyCommonJSImports: boolean;
24
+ emitLegacyCommonJSImports?: boolean;
25
+ importExtension: '' | `.${string}`;
25
26
  printFieldsOnNewLines: boolean;
26
27
  includeExternalFragments: boolean;
27
28
  }
@@ -343,11 +344,17 @@ export interface RawConfig {
343
344
  */
344
345
  inlineFragmentTypes?: InlineFragmentTypeOptions;
345
346
  /**
347
+ * @deprecated Please use `importExtension` instead.
346
348
  * @default true
347
349
  * @description Emit legacy common js imports.
348
350
  * Default it will be `true` this way it ensure that generated code works with [non-compliant bundlers](https://github.com/dotansimha/graphql-code-generator/issues/8065).
349
351
  */
350
352
  emitLegacyCommonJSImports?: boolean;
353
+ /**
354
+ * @description Append this extension to all imports.
355
+ * Useful for ESM environments that require file extensions in import statements.
356
+ */
357
+ importExtension?: '' | `.${string}`;
351
358
  /**
352
359
  * @default false
353
360
  * @description Extract all field types to their own types, instead of inlining them.
@@ -21,7 +21,8 @@ export interface ParsedConfig {
21
21
  useTypeImports: boolean;
22
22
  allowEnumStringTypes: boolean;
23
23
  inlineFragmentTypes: InlineFragmentTypeOptions;
24
- emitLegacyCommonJSImports: boolean;
24
+ emitLegacyCommonJSImports?: boolean;
25
+ importExtension: '' | `.${string}`;
25
26
  printFieldsOnNewLines: boolean;
26
27
  includeExternalFragments: boolean;
27
28
  }
@@ -343,11 +344,17 @@ export interface RawConfig {
343
344
  */
344
345
  inlineFragmentTypes?: InlineFragmentTypeOptions;
345
346
  /**
347
+ * @deprecated Please use `importExtension` instead.
346
348
  * @default true
347
349
  * @description Emit legacy common js imports.
348
350
  * Default it will be `true` this way it ensure that generated code works with [non-compliant bundlers](https://github.com/dotansimha/graphql-code-generator/issues/8065).
349
351
  */
350
352
  emitLegacyCommonJSImports?: boolean;
353
+ /**
354
+ * @description Append this extension to all imports.
355
+ * Useful for ESM environments that require file extensions in import statements.
356
+ */
357
+ importExtension?: '' | `.${string}`;
351
358
  /**
352
359
  * @default false
353
360
  * @description Extract all field types to their own types, instead of inlining them.
@@ -4,7 +4,8 @@ export type ImportDeclaration<T = string> = {
4
4
  baseOutputDir: string;
5
5
  baseDir: string;
6
6
  typesImport: boolean;
7
- emitLegacyCommonJSImports: boolean;
7
+ emitLegacyCommonJSImports?: boolean;
8
+ importExtension: '' | `.${string}`;
8
9
  };
9
10
  export type ImportSource<T = string> = {
10
11
  /**
@@ -4,7 +4,8 @@ export type ImportDeclaration<T = string> = {
4
4
  baseOutputDir: string;
5
5
  baseDir: string;
6
6
  typesImport: boolean;
7
- emitLegacyCommonJSImports: boolean;
7
+ emitLegacyCommonJSImports?: boolean;
8
+ importExtension: '' | `.${string}`;
8
9
  };
9
10
  export type ImportSource<T = string> = {
10
11
  /**