@graphql-tools/import 7.1.0 → 7.1.1-alpha-20250828202714-99448f4d8b98f25fe85135b1d24e631dfff6ff25

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.
Files changed (3) hide show
  1. package/cjs/index.js +29 -15
  2. package/esm/index.js +29 -15
  3. package/package.json +1 -1
package/cjs/index.js CHANGED
@@ -52,8 +52,11 @@ function processImport(filePath, cwd = globalThis.process?.cwd(), predefinedImpo
52
52
  };
53
53
  }
54
54
  function visitFile(filePath, cwd, visitedFiles, predefinedImports, pathAliases) {
55
- if (!(0, path_1.isAbsolute)(filePath) && !(filePath in predefinedImports)) {
56
- filePath = resolveFilePath(cwd, filePath, pathAliases);
55
+ if (!(filePath in predefinedImports)) {
56
+ filePath = applyPathAliases(filePath, pathAliases);
57
+ if (!(0, path_1.isAbsolute)(filePath)) {
58
+ filePath = resolveFilePath(cwd, filePath);
59
+ }
57
60
  }
58
61
  if (!visitedFiles.has(filePath)) {
59
62
  const fileContent = filePath in predefinedImports ? predefinedImports[filePath] : (0, fs_1.readFileSync)(filePath, 'utf8');
@@ -442,19 +445,7 @@ function parseImportLine(importLine) {
442
445
  # import [Type].[Field] from [File]
443
446
  `);
444
447
  }
445
- function resolveFilePath(filePath, importFrom, pathAliases) {
446
- // First, check if importFrom matches any path aliases.
447
- if (pathAliases != null) {
448
- for (const [prefixPattern, mapping] of Object.entries(pathAliases.mappings)) {
449
- const matchedMapping = applyPathAlias(prefixPattern, mapping, importFrom);
450
- if (matchedMapping == null) {
451
- continue;
452
- }
453
- const resolvedMapping = (0, resolve_from_1.default)(pathAliases.rootDir ?? process.cwd(), matchedMapping);
454
- return (0, fs_1.realpathSync)(resolvedMapping);
455
- }
456
- }
457
- // Fall back to original resolution logic
448
+ function resolveFilePath(filePath, importFrom) {
458
449
  const dirName = (0, path_1.dirname)(filePath);
459
450
  try {
460
451
  const fullPath = (0, path_1.join)(dirName, importFrom);
@@ -467,6 +458,29 @@ function resolveFilePath(filePath, importFrom, pathAliases) {
467
458
  throw e;
468
459
  }
469
460
  }
461
+ /**
462
+ * Rewrites the provided file path using the first matched path alias (or
463
+ * returns the file path with no changes if none match).
464
+ *
465
+ * @param filePath - The file path to rewrite.
466
+ * @param pathAliases - The path aliases to evaluate.
467
+ *
468
+ * @returns The rewritten file path.
469
+ */
470
+ function applyPathAliases(filePath, pathAliases) {
471
+ if (pathAliases == null) {
472
+ return filePath;
473
+ }
474
+ for (const [prefixPattern, mapping] of Object.entries(pathAliases.mappings)) {
475
+ const matchedMapping = applyPathAlias(prefixPattern, mapping, filePath);
476
+ if (matchedMapping == null) {
477
+ continue;
478
+ }
479
+ const resolvedMapping = (0, resolve_from_1.default)(pathAliases.rootDir ?? process.cwd(), matchedMapping);
480
+ return (0, fs_1.realpathSync)(resolvedMapping);
481
+ }
482
+ return filePath;
483
+ }
470
484
  /**
471
485
  * Resolves an import alias and it's mapping using the same strategy as
472
486
  * tsconfig.json#paths
package/esm/index.js CHANGED
@@ -44,8 +44,11 @@ export function processImport(filePath, cwd = globalThis.process?.cwd(), predefi
44
44
  };
45
45
  }
46
46
  function visitFile(filePath, cwd, visitedFiles, predefinedImports, pathAliases) {
47
- if (!isAbsolute(filePath) && !(filePath in predefinedImports)) {
48
- filePath = resolveFilePath(cwd, filePath, pathAliases);
47
+ if (!(filePath in predefinedImports)) {
48
+ filePath = applyPathAliases(filePath, pathAliases);
49
+ if (!isAbsolute(filePath)) {
50
+ filePath = resolveFilePath(cwd, filePath);
51
+ }
49
52
  }
50
53
  if (!visitedFiles.has(filePath)) {
51
54
  const fileContent = filePath in predefinedImports ? predefinedImports[filePath] : readFileSync(filePath, 'utf8');
@@ -434,19 +437,7 @@ export function parseImportLine(importLine) {
434
437
  # import [Type].[Field] from [File]
435
438
  `);
436
439
  }
437
- function resolveFilePath(filePath, importFrom, pathAliases) {
438
- // First, check if importFrom matches any path aliases.
439
- if (pathAliases != null) {
440
- for (const [prefixPattern, mapping] of Object.entries(pathAliases.mappings)) {
441
- const matchedMapping = applyPathAlias(prefixPattern, mapping, importFrom);
442
- if (matchedMapping == null) {
443
- continue;
444
- }
445
- const resolvedMapping = resolveFrom(pathAliases.rootDir ?? process.cwd(), matchedMapping);
446
- return realpathSync(resolvedMapping);
447
- }
448
- }
449
- // Fall back to original resolution logic
440
+ function resolveFilePath(filePath, importFrom) {
450
441
  const dirName = dirname(filePath);
451
442
  try {
452
443
  const fullPath = join(dirName, importFrom);
@@ -459,6 +450,29 @@ function resolveFilePath(filePath, importFrom, pathAliases) {
459
450
  throw e;
460
451
  }
461
452
  }
453
+ /**
454
+ * Rewrites the provided file path using the first matched path alias (or
455
+ * returns the file path with no changes if none match).
456
+ *
457
+ * @param filePath - The file path to rewrite.
458
+ * @param pathAliases - The path aliases to evaluate.
459
+ *
460
+ * @returns The rewritten file path.
461
+ */
462
+ function applyPathAliases(filePath, pathAliases) {
463
+ if (pathAliases == null) {
464
+ return filePath;
465
+ }
466
+ for (const [prefixPattern, mapping] of Object.entries(pathAliases.mappings)) {
467
+ const matchedMapping = applyPathAlias(prefixPattern, mapping, filePath);
468
+ if (matchedMapping == null) {
469
+ continue;
470
+ }
471
+ const resolvedMapping = resolveFrom(pathAliases.rootDir ?? process.cwd(), matchedMapping);
472
+ return realpathSync(resolvedMapping);
473
+ }
474
+ return filePath;
475
+ }
462
476
  /**
463
477
  * Resolves an import alias and it's mapping using the same strategy as
464
478
  * tsconfig.json#paths
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/import",
3
- "version": "7.1.0",
3
+ "version": "7.1.1-alpha-20250828202714-99448f4d8b98f25fe85135b1d24e631dfff6ff25",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {