@boundaries/elements 1.1.2 → 1.2.0

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.
package/README.md CHANGED
@@ -25,6 +25,7 @@
25
25
  - [Using Matchers](#using-matchers)
26
26
  - [Element Matching](#element-matching)
27
27
  - [Dependency Matching](#dependency-matching)
28
+ - [Flagging Dependencies as External](#flagging-dependencies-as-external)
28
29
  - [API Reference](#api-reference)
29
30
  - [Legacy Selectors](#legacy-selectors)
30
31
  - [Contributing](#contributing)
@@ -120,6 +121,13 @@ When creating an `Elements` instance, you can provide configuration options that
120
121
  const elements = new Elements({
121
122
  ignorePaths: ["**/dist/**", "**/build/**", "**/node_modules/**"],
122
123
  includePaths: ["src/**/*"],
124
+ rootPath: "/absolute/path/to/project",
125
+ flagAsExternal: {
126
+ unresolvableAlias: true,
127
+ inNodeModules: true,
128
+ outsideRootPath: true,
129
+ customSourcePatterns: ["@myorg/*"],
130
+ },
123
131
  });
124
132
  ```
125
133
 
@@ -129,6 +137,15 @@ const elements = new Elements({
129
137
  - **`includePaths`**: Micromatch pattern(s) to include only specific paths (default: all paths)
130
138
  - **`legacyTemplates`**: Whether to enable legacy template syntax support (default: `true`, but it will be `false` in future releases). This allows using `${variable}` syntax in templates for backward compatibility.
131
139
  - **`cache`**: Whether to enable internal caching to improve performance (default: `true`)
140
+ - **`rootPath`**: Absolute path to the project root. When configured, file paths should be provided as absolute paths to allow the package to determine which files are outside the project root (default: `undefined`)
141
+ - **`flagAsExternal`**: Configuration for categorizing dependencies as external or local. Multiple conditions can be specified, and dependencies will be categorized as external if ANY condition is met (OR logic). See [Flagging Dependencies as External](#flagging-dependencies-as-external) for details.
142
+
143
+ > [!NOTE]
144
+ > **Pattern Matching with `rootPath`:**
145
+ > When `rootPath` **is configured**:
146
+ > - **Matching patterns** in element descriptors are **relative to the `rootPath`**. The package automatically converts absolute paths to relative paths internally for pattern matching.
147
+ > - In **`file` and `folder` modes**, patterns are evaluated **right-to-left** (from the end of the path), so the relativity to `rootPath` is typically less important. For example, a pattern like `*.model.ts` will match any file ending with `.model.ts` regardless of its location within `rootPath`.
148
+ > - In **`full` mode**, patterns must match the complete relative path from `rootPath`. Files outside `rootPath` maintain their absolute paths and require absolute patterns to match.
132
149
 
133
150
  ### Creating a Matcher
134
151
 
@@ -345,6 +362,114 @@ const isDependencyMatch = matcher.isMatch(
345
362
  > [!TIP]
346
363
  > You can also provide an array of selectors both to the `from` and `to` properties of the dependency selector. In this case, the method will return `true` if the dependency matches any combination of the provided selectors (OR logic).
347
364
 
365
+ ### Flagging Dependencies as External
366
+
367
+ The `flagAsExternal` configuration allows you to control how dependencies are categorized as external or local. This is especially useful in monorepo setups where you may want to treat inter-package dependencies as external even though they're within the same repository.
368
+
369
+ **Multiple conditions can be specified, and dependencies will be flagged as external if ANY condition is met (OR logic).**
370
+
371
+ #### Available Options
372
+
373
+ - **`unresolvableAlias`** (boolean, default: `true`): Non-relative imports whose path cannot be resolved are categorized as external
374
+
375
+ ```typescript
376
+ const elements = new Elements({
377
+ flagAsExternal: { unresolvableAlias: true },
378
+ });
379
+ const matcher = elements.getMatcher([/* descriptors */]);
380
+
381
+ // describeDependency({ from, to, source, kind }):
382
+ // to: null, source: 'unresolved-module' -> origin: 'external'
383
+ // to: '/project/src/Button.ts', source: './Button' -> origin: 'local'
384
+ ```
385
+
386
+ - **`inNodeModules`** (boolean, default: `true`): Non-relative paths that include `node_modules` in the resolved path are categorized as external
387
+
388
+ ```typescript
389
+ const elements = new Elements({
390
+ flagAsExternal: { inNodeModules: true },
391
+ });
392
+ const matcher = elements.getMatcher([/* descriptors */]);
393
+
394
+ // describeDependency({ from, to, source, kind }):
395
+ // to: '/project/node_modules/react/index.js', source: 'react' -> origin: 'external'
396
+ // to: '/project/src/utils.ts', source: './utils' -> origin: 'local'
397
+ ```
398
+
399
+ - **`outsideRootPath`** (boolean, default: `false`): Dependencies whose resolved path is outside the configured `rootPath` are categorized as external. This is particularly useful in monorepo setups.
400
+
401
+ > **⚠️ Important:** This option requires `rootPath` to be configured. When using this option, all file paths must be absolute and include the `rootPath` prefix for files within the project.
402
+
403
+ ```typescript
404
+ const elements = new Elements({
405
+ rootPath: '/monorepo/packages/app',
406
+ flagAsExternal: { outsideRootPath: true },
407
+ });
408
+ const matcher = elements.getMatcher([/* descriptors */]);
409
+
410
+ // describeDependency({ from, to, source, kind }):
411
+ // to: '/monorepo/packages/shared/index.ts', source: '@myorg/shared' -> origin: 'external'
412
+ // to: '/monorepo/packages/app/src/utils/helper.ts', source: './utils/helper' -> origin: 'local'
413
+ ```
414
+
415
+ - **`customSourcePatterns`** (string[], default: `[]`): Array of micromatch patterns that, when matching the import/export source string, categorize the dependency as external
416
+
417
+ ```typescript
418
+ const elements = new Elements({
419
+ flagAsExternal: { customSourcePatterns: ['@myorg/*', '~/**'] },
420
+ });
421
+ const matcher = elements.getMatcher([/* descriptors */]);
422
+
423
+ // describeDependency({ from, to, source, kind }):
424
+ // source: '@myorg/shared' -> origin: 'external' (matches '@myorg/*')
425
+ // source: '~/utils/helper' -> origin: 'external' (matches '~/**')
426
+ // source: '@other/package' -> origin: 'local' (no match, unless inNodeModules is true or other conditions met)
427
+ ```
428
+
429
+ #### Path Requirements with `rootPath`
430
+
431
+ When `rootPath` is configured, the package needs absolute paths to correctly determine which files are outside the project root, but matching patterns must remain relative to `rootPath`, especially in `full` mode (because `file` and `folder` modes match progressively from the right, so they may be less affected by relativity).
432
+
433
+ ```typescript
434
+ const elements = new Elements({
435
+ rootPath: '/project/packages/app',
436
+ flagAsExternal: {
437
+ outsideRootPath: true,
438
+ },
439
+ });
440
+
441
+ // Matching patterns are relative to rootPath
442
+ const matcher = elements.getMatcher([
443
+ { type: 'component', pattern: 'src/**/*.ts', mode: 'full' }, // Relative to /project/packages/app
444
+ ]);
445
+
446
+ // ✅ Correct: Using absolute file paths with relative patterns
447
+ const dep = matcher.describeDependency({
448
+ from: '/project/packages/app/src/index.ts', // absolute file path
449
+ to: '/project/packages/shared/index.ts', // absolute file path
450
+ source: '@myorg/shared',
451
+ kind: 'value',
452
+ });
453
+ // Result: dep.to.origin === 'external' (outside rootPath)
454
+ // Note: Pattern 'src/**/*.ts' matches because the package converts
455
+ // absolute paths to relative internally for pattern matching
456
+
457
+ // ❌ Incorrect: Using relative file paths (won't detect outsideRootPath correctly)
458
+ const dep2 = matcher.describeDependency({
459
+ from: 'src/index.ts', // relative file path
460
+ to: '../shared/index.ts', // relative file path
461
+ source: '@myorg/shared',
462
+ kind: 'value',
463
+ });
464
+ // Result: Won't correctly detect if outside rootPath
465
+ ```
466
+
467
+ > **💡 Key Points:**
468
+ > - **File paths** in API calls (`from`, `to`, `filePath`) must be **absolute** when using `rootPath`
469
+ > - **Matching patterns** in element descriptors stay **relative** to `rootPath`
470
+ > - The package handles the conversion internally
471
+ > - When not using `rootPath`, the package continues to work with relative paths as before, maintaining backward compatibility.
472
+
348
473
  ## API Reference
349
474
 
350
475
  ### Class: Elements
@@ -2,6 +2,19 @@
2
2
  * Type representing a micromatch pattern, which can be a string or an array of strings.
3
3
  */
4
4
  type MicromatchPattern = string | string[];
5
+ /**
6
+ * Configuration options for categorizing dependencies as external or local.
7
+ */
8
+ type FlagAsExternalOptions = {
9
+ /** When true, non-relative dependencies whose path cannot be resolved are categorized as external (default: true) */
10
+ unresolvableAlias?: boolean;
11
+ /** When true, non-relative paths that include node_modules are categorized as external (default: true) */
12
+ inNodeModules?: boolean;
13
+ /** When true, dependencies whose resolved path is outside the configured root path are categorized as external (default: false) */
14
+ outsideRootPath?: boolean;
15
+ /** List of patterns (using micromatch syntax) that, when matching the source of the dependency, categorize it as external (default: []) */
16
+ customSourcePatterns?: string[];
17
+ };
5
18
  /** Configuration options for the Config class */
6
19
  type ConfigOptions = {
7
20
  /** An array of path patterns to include when resolving elements. Defaults to all files if not specified */
@@ -15,15 +28,33 @@ type ConfigOptions = {
15
28
  legacyTemplates?: boolean;
16
29
  /** Whether to enable caching */
17
30
  cache?: boolean;
31
+ /** Configuration for categorizing dependencies as external or local */
32
+ flagAsExternal?: FlagAsExternalOptions;
33
+ /** Root path of the project, used for determining if dependencies are outside the project */
34
+ rootPath?: string;
35
+ };
36
+ type FlagAsExternalOptionsNormalized = {
37
+ /** When true, non-relative dependencies whose path cannot be resolved are categorized as external */
38
+ unresolvableAlias: boolean;
39
+ /** When true, non-relative paths that include node_modules are categorized as external */
40
+ inNodeModules: boolean;
41
+ /** When true, dependencies whose resolved path is outside the configured root path are categorized as external */
42
+ outsideRootPath: boolean;
43
+ /** List of patterns (using micromatch syntax) that, when matching the source of the dependency, categorize it as external */
44
+ customSourcePatterns: string[];
18
45
  };
19
- type ConfigOptionsNormalized = Omit<ConfigOptions, "legacyTemplates" | "cache"> & {
46
+ type ConfigOptionsNormalized = Omit<ConfigOptions, "legacyTemplates" | "cache" | "flagAsExternal"> & {
20
47
  /** Whether to enable legacy template support */
21
48
  legacyTemplates: boolean;
22
49
  /** Cache configuration options */
23
50
  cache: boolean;
51
+ /** Configuration for categorizing dependencies as external or local */
52
+ flagAsExternal: FlagAsExternalOptionsNormalized;
53
+ /** Root path of the project, already normalized, and finishing with a slash, used for determining if dependencies are outside the project */
54
+ rootPath: string | undefined;
24
55
  };
25
56
  /** Options for descriptors */
26
- type DescriptorOptionsNormalized = Pick<ConfigOptionsNormalized, "includePaths" | "ignorePaths" | "cache">;
57
+ type DescriptorOptionsNormalized = Pick<ConfigOptionsNormalized, "includePaths" | "ignorePaths" | "cache" | "flagAsExternal" | "rootPath">;
27
58
  /** Options for element matchers */
28
59
  type MatchersOptionsNormalized = Pick<ConfigOptionsNormalized, "legacyTemplates">;
29
60
 
@@ -1544,14 +1575,37 @@ declare class ElementsDescriptor {
1544
1575
  * @returns The base source of the external module. (e.g., for "@scope/package/submodule", it returns "@scope/package")
1545
1576
  */
1546
1577
  private _getExternalOrCoreModuleBaseSource;
1578
+ /**
1579
+ * Determines if a file path is outside the configured root path.
1580
+ * @param filePath The file path to check.
1581
+ * @returns True if the file path is outside the root path, false otherwise.
1582
+ */
1583
+ private _isOutsideRootPath;
1584
+ /**
1585
+ * Converts an absolute file path to a relative path if rootPath is configured.
1586
+ * If rootPath is not configured, returns the path as-is (maintains backward compatibility).
1587
+ * @param filePath The file path to convert (can be absolute or relative)
1588
+ * @returns The relative path if rootPath is configured and path is absolute, otherwise the original path
1589
+ */
1590
+ private _toRelativePath;
1591
+ /**
1592
+ * Checks if a source string matches any of the provided patterns using micromatch.
1593
+ * @param patterns - Array of micromatch patterns
1594
+ * @param source - The source string to match against patterns
1595
+ * @returns True if the source matches any pattern, false otherwise
1596
+ */
1597
+ private _matchesAnyPattern;
1547
1598
  /**
1548
1599
  * Determines if an element is external based on its file path and dependency source.
1549
- * Files inside "node_modules" are considered external.
1550
- * If the dependency source is not provided, only the file path is considered.
1551
- * If the dependency source is provided, it must not be a local path (i.e, it should start by "./", "../", or "/").
1552
- * @param filePath
1553
- * @param dependencySource
1554
- * @returns
1600
+ * Uses the flagAsExternal configuration to evaluate multiple conditions with OR logic:
1601
+ * - unresolvableAlias: Files whose path cannot be resolved (filePath is null)
1602
+ * - inNodeModules: Non-relative paths that include "node_modules"
1603
+ * - outsideRootPath: Resolved path is outside the configured root path (only if rootPath is configured)
1604
+ * - customSourcePatterns: Source matches any of the configured patterns
1605
+ * @param filePath The resolved file path (null if unresolved). Can be absolute if rootPath is configured, or relative if rootPath is not configured.
1606
+ * @param isOutsideRootPath Whether the file path is outside the configured root path.
1607
+ * @param dependencySource The import/export source string
1608
+ * @returns True if any of the configured conditions is met, false otherwise
1555
1609
  */
1556
1610
  private _isExternalDependency;
1557
1611
  /**
@@ -1599,27 +1653,28 @@ declare class ElementsDescriptor {
1599
1653
  /**
1600
1654
  * Returns an external or core dependency element given its dependency source and file path.
1601
1655
  * @param dependencySource The source of the dependency.
1602
- * @param filePath The resolved file path of the dependency, if known.
1656
+ * @param isOutsideRootPath Whether the file path is outside the configured root path.
1657
+ * @param filePath The resolved file path of the dependency, if known. Can be absolute if rootPath is configured.
1603
1658
  * @returns The external or core dependency element, or null if it is a local dependency.
1604
1659
  */
1605
1660
  private _getExternalOrCoreDependencyElement;
1606
1661
  /**
1607
1662
  * Describes an element given its file path and dependency source, if any.
1608
- * @param filePath The path of the file to describe.
1663
+ * @param filePath The path of the file to describe. Can be absolute if rootPath is configured, or relative if not.
1609
1664
  * @param dependencySource The source of the dependency, if the element to describe is so. It refers to the import/export path used to reference the file or external module.
1610
1665
  * @returns The description of the element. A dependency element if dependency source is provided, otherwise a file element.
1611
1666
  */
1612
1667
  private _describeElement;
1613
1668
  /**
1614
1669
  * Describes an element given its file path.
1615
- * @param filePath The path of the file to describe.
1670
+ * @param filePath The path of the file to describe. Can be absolute if rootPath is configured, or relative if not.
1616
1671
  * @returns The description of the element.
1617
1672
  */
1618
1673
  describeElement(filePath?: string): FileElement;
1619
1674
  /**
1620
1675
  * Describes a dependency element given its dependency source and file path.
1621
1676
  * @param dependencySource The source of the dependency.
1622
- * @param filePath The path of the file being the dependency, if known.
1677
+ * @param filePath The path of the file being the dependency, if known. Can be absolute if rootPath is configured, or relative if not.
1623
1678
  * @returns The description of the dependency element.
1624
1679
  */
1625
1680
  describeDependencyElement(dependencySource: string, filePath?: string): DependencyElementDescription;
@@ -1791,4 +1846,4 @@ declare class CacheManager<CacheKey extends NotUndefined, CachedValue> {
1791
1846
  setFromSerialized(serializedCache: Record<string, CachedValue>): void;
1792
1847
  }
1793
1848
 
1794
- export { type BaseDependencyElement, type BaseElement, type BaseElementDescriptor, type BaseElementSelector, type BaseElementSelectorData, type BaseElementSelectorWithOptions, type BaseElementsSelector, CacheManager, type CapturedValues, type CapturedValuesSelector, type ConfigOptions, type ConfigOptionsNormalized, type CoreDependencyElement, DEPENDENCY_KINDS_MAP, DEPENDENCY_KIND_TYPE, DEPENDENCY_KIND_TYPEOF, DEPENDENCY_KIND_VALUE, DEPENDENCY_RELATIONSHIPS_INVERTED_MAP, DEPENDENCY_RELATIONSHIPS_MAP, DependenciesDescriptor, type DependenciesDescriptorSerializedCache, DependenciesMatcher, type DependenciesMatcherSerializedCache, type DependencyDescription, type DependencyElementDescription, type DependencyElementSelector, type DependencyElementSelectorData, type DependencyElementSelectorWithOptions, type DependencyElementsSelector, type DependencyKind, type DependencyMatchResult, type DependencyRelationship, type DependencySelector, type DependencySelectorNormalized, type DescribeDependencyOptions, type DescriptionsSerializedCache, type DescriptorOptionsNormalized, Descriptors, type DescriptorsSerializedCache, ELEMENT_DESCRIPTOR_MODES_MAP, ELEMENT_ORIGINS_MAP, type ElementDescription, type ElementDescriptor, type ElementDescriptorMode, type ElementDescriptorPattern, type ElementDescriptorWithCategory, type ElementDescriptorWithType, type ElementDescriptorWithTypeAndCategory, type ElementDescriptors, type ElementOrigin, type ElementParent, type ElementSelector, type ElementSelectorData, type ElementSelectorWithOptions, type ElementSelectors, Elements, type ElementsDependencyInfo, ElementsDescriptor, type ElementsDescriptorSerializedCache, ElementsMatcher, type ElementsMatcherSerializedCache, type ElementsSelector, type ElementsSerializedCache, type ExternalDependencyElement, type ExternalLibrariesSelector, type ExternalLibrarySelector, type ExternalLibrarySelectorOptions, type ExternalLibrarySelectorWithOptions, type ExternalLibrarySelectors, type FileElement, type FileElementsSerializedCache, type IgnoredDependencyElement, type IgnoredElement, type LocalDependencyElement, type LocalDependencyElementKnown, type LocalDependencyElementUnknown, type LocalElementKnown, type LocalElementUnknown, Matcher, type MatcherOptions, type MatcherOptionsDependencySelectorsGlobals, type MatcherSerializedCache, type MatchersOptionsNormalized, type MicromatchPattern, type MicromatchSerializedCache, type NotUndefined, type ObjectCacheKey, type SelectableElement, type SimpleElementSelectorByType, type TemplateData, isBaseElement, isBaseElementDescriptor, isBaseElementSelectorData, isCapturedValuesSelector, isCoreDependencyElement, isDependencyDescription, isDependencyElementDescription, isDependencyKind, isDependencyRelationship, isDependencyRelationshipDescription, isDependencySelector, isElementDescription, isElementDescriptor, isElementDescriptorMode, isElementDescriptorPattern, isElementDescriptorWithCategory, isElementDescriptorWithType, isElementSelector, isElementSelectorData, isElementSelectorWithLegacyOptions, isElementsDependencyInfo, isElementsSelector, isExternalDependencyElement, isExternalLibrariesSelector, isExternalLibrarySelector, isExternalLibrarySelectorOptions, isExternalLibrarySelectorOptionsWithPath, isExternalLibrarySelectorOptionsWithSpecifiers, isExternalLibrarySelectorWithOptions, isIgnoredElement, isInternalDependency, isKnownLocalElement, isLocalDependencyElement, isLocalElement, isSimpleElementSelectorByType, isUnknownLocalElement, normalizeElementsSelector };
1849
+ export { type BaseDependencyElement, type BaseElement, type BaseElementDescriptor, type BaseElementSelector, type BaseElementSelectorData, type BaseElementSelectorWithOptions, type BaseElementsSelector, CacheManager, type CapturedValues, type CapturedValuesSelector, type ConfigOptions, type ConfigOptionsNormalized, type CoreDependencyElement, DEPENDENCY_KINDS_MAP, DEPENDENCY_KIND_TYPE, DEPENDENCY_KIND_TYPEOF, DEPENDENCY_KIND_VALUE, DEPENDENCY_RELATIONSHIPS_INVERTED_MAP, DEPENDENCY_RELATIONSHIPS_MAP, DependenciesDescriptor, type DependenciesDescriptorSerializedCache, DependenciesMatcher, type DependenciesMatcherSerializedCache, type DependencyDescription, type DependencyElementDescription, type DependencyElementSelector, type DependencyElementSelectorData, type DependencyElementSelectorWithOptions, type DependencyElementsSelector, type DependencyKind, type DependencyMatchResult, type DependencyRelationship, type DependencySelector, type DependencySelectorNormalized, type DescribeDependencyOptions, type DescriptionsSerializedCache, type DescriptorOptionsNormalized, Descriptors, type DescriptorsSerializedCache, ELEMENT_DESCRIPTOR_MODES_MAP, ELEMENT_ORIGINS_MAP, type ElementDescription, type ElementDescriptor, type ElementDescriptorMode, type ElementDescriptorPattern, type ElementDescriptorWithCategory, type ElementDescriptorWithType, type ElementDescriptorWithTypeAndCategory, type ElementDescriptors, type ElementOrigin, type ElementParent, type ElementSelector, type ElementSelectorData, type ElementSelectorWithOptions, type ElementSelectors, Elements, type ElementsDependencyInfo, ElementsDescriptor, type ElementsDescriptorSerializedCache, ElementsMatcher, type ElementsMatcherSerializedCache, type ElementsSelector, type ElementsSerializedCache, type ExternalDependencyElement, type ExternalLibrariesSelector, type ExternalLibrarySelector, type ExternalLibrarySelectorOptions, type ExternalLibrarySelectorWithOptions, type ExternalLibrarySelectors, type FileElement, type FileElementsSerializedCache, type FlagAsExternalOptions, type FlagAsExternalOptionsNormalized, type IgnoredDependencyElement, type IgnoredElement, type LocalDependencyElement, type LocalDependencyElementKnown, type LocalDependencyElementUnknown, type LocalElementKnown, type LocalElementUnknown, Matcher, type MatcherOptions, type MatcherOptionsDependencySelectorsGlobals, type MatcherSerializedCache, type MatchersOptionsNormalized, type MicromatchPattern, type MicromatchSerializedCache, type NotUndefined, type ObjectCacheKey, type SelectableElement, type SimpleElementSelectorByType, type TemplateData, isBaseElement, isBaseElementDescriptor, isBaseElementSelectorData, isCapturedValuesSelector, isCoreDependencyElement, isDependencyDescription, isDependencyElementDescription, isDependencyKind, isDependencyRelationship, isDependencyRelationshipDescription, isDependencySelector, isElementDescription, isElementDescriptor, isElementDescriptorMode, isElementDescriptorPattern, isElementDescriptorWithCategory, isElementDescriptorWithType, isElementSelector, isElementSelectorData, isElementSelectorWithLegacyOptions, isElementsDependencyInfo, isElementsSelector, isExternalDependencyElement, isExternalLibrariesSelector, isExternalLibrarySelector, isExternalLibrarySelectorOptions, isExternalLibrarySelectorOptionsWithPath, isExternalLibrarySelectorOptionsWithSpecifiers, isExternalLibrarySelectorWithOptions, isIgnoredElement, isInternalDependency, isKnownLocalElement, isLocalDependencyElement, isLocalElement, isSimpleElementSelectorByType, isUnknownLocalElement, normalizeElementsSelector };
@@ -2,6 +2,19 @@
2
2
  * Type representing a micromatch pattern, which can be a string or an array of strings.
3
3
  */
4
4
  type MicromatchPattern = string | string[];
5
+ /**
6
+ * Configuration options for categorizing dependencies as external or local.
7
+ */
8
+ type FlagAsExternalOptions = {
9
+ /** When true, non-relative dependencies whose path cannot be resolved are categorized as external (default: true) */
10
+ unresolvableAlias?: boolean;
11
+ /** When true, non-relative paths that include node_modules are categorized as external (default: true) */
12
+ inNodeModules?: boolean;
13
+ /** When true, dependencies whose resolved path is outside the configured root path are categorized as external (default: false) */
14
+ outsideRootPath?: boolean;
15
+ /** List of patterns (using micromatch syntax) that, when matching the source of the dependency, categorize it as external (default: []) */
16
+ customSourcePatterns?: string[];
17
+ };
5
18
  /** Configuration options for the Config class */
6
19
  type ConfigOptions = {
7
20
  /** An array of path patterns to include when resolving elements. Defaults to all files if not specified */
@@ -15,15 +28,33 @@ type ConfigOptions = {
15
28
  legacyTemplates?: boolean;
16
29
  /** Whether to enable caching */
17
30
  cache?: boolean;
31
+ /** Configuration for categorizing dependencies as external or local */
32
+ flagAsExternal?: FlagAsExternalOptions;
33
+ /** Root path of the project, used for determining if dependencies are outside the project */
34
+ rootPath?: string;
35
+ };
36
+ type FlagAsExternalOptionsNormalized = {
37
+ /** When true, non-relative dependencies whose path cannot be resolved are categorized as external */
38
+ unresolvableAlias: boolean;
39
+ /** When true, non-relative paths that include node_modules are categorized as external */
40
+ inNodeModules: boolean;
41
+ /** When true, dependencies whose resolved path is outside the configured root path are categorized as external */
42
+ outsideRootPath: boolean;
43
+ /** List of patterns (using micromatch syntax) that, when matching the source of the dependency, categorize it as external */
44
+ customSourcePatterns: string[];
18
45
  };
19
- type ConfigOptionsNormalized = Omit<ConfigOptions, "legacyTemplates" | "cache"> & {
46
+ type ConfigOptionsNormalized = Omit<ConfigOptions, "legacyTemplates" | "cache" | "flagAsExternal"> & {
20
47
  /** Whether to enable legacy template support */
21
48
  legacyTemplates: boolean;
22
49
  /** Cache configuration options */
23
50
  cache: boolean;
51
+ /** Configuration for categorizing dependencies as external or local */
52
+ flagAsExternal: FlagAsExternalOptionsNormalized;
53
+ /** Root path of the project, already normalized, and finishing with a slash, used for determining if dependencies are outside the project */
54
+ rootPath: string | undefined;
24
55
  };
25
56
  /** Options for descriptors */
26
- type DescriptorOptionsNormalized = Pick<ConfigOptionsNormalized, "includePaths" | "ignorePaths" | "cache">;
57
+ type DescriptorOptionsNormalized = Pick<ConfigOptionsNormalized, "includePaths" | "ignorePaths" | "cache" | "flagAsExternal" | "rootPath">;
27
58
  /** Options for element matchers */
28
59
  type MatchersOptionsNormalized = Pick<ConfigOptionsNormalized, "legacyTemplates">;
29
60
 
@@ -1544,14 +1575,37 @@ declare class ElementsDescriptor {
1544
1575
  * @returns The base source of the external module. (e.g., for "@scope/package/submodule", it returns "@scope/package")
1545
1576
  */
1546
1577
  private _getExternalOrCoreModuleBaseSource;
1578
+ /**
1579
+ * Determines if a file path is outside the configured root path.
1580
+ * @param filePath The file path to check.
1581
+ * @returns True if the file path is outside the root path, false otherwise.
1582
+ */
1583
+ private _isOutsideRootPath;
1584
+ /**
1585
+ * Converts an absolute file path to a relative path if rootPath is configured.
1586
+ * If rootPath is not configured, returns the path as-is (maintains backward compatibility).
1587
+ * @param filePath The file path to convert (can be absolute or relative)
1588
+ * @returns The relative path if rootPath is configured and path is absolute, otherwise the original path
1589
+ */
1590
+ private _toRelativePath;
1591
+ /**
1592
+ * Checks if a source string matches any of the provided patterns using micromatch.
1593
+ * @param patterns - Array of micromatch patterns
1594
+ * @param source - The source string to match against patterns
1595
+ * @returns True if the source matches any pattern, false otherwise
1596
+ */
1597
+ private _matchesAnyPattern;
1547
1598
  /**
1548
1599
  * Determines if an element is external based on its file path and dependency source.
1549
- * Files inside "node_modules" are considered external.
1550
- * If the dependency source is not provided, only the file path is considered.
1551
- * If the dependency source is provided, it must not be a local path (i.e, it should start by "./", "../", or "/").
1552
- * @param filePath
1553
- * @param dependencySource
1554
- * @returns
1600
+ * Uses the flagAsExternal configuration to evaluate multiple conditions with OR logic:
1601
+ * - unresolvableAlias: Files whose path cannot be resolved (filePath is null)
1602
+ * - inNodeModules: Non-relative paths that include "node_modules"
1603
+ * - outsideRootPath: Resolved path is outside the configured root path (only if rootPath is configured)
1604
+ * - customSourcePatterns: Source matches any of the configured patterns
1605
+ * @param filePath The resolved file path (null if unresolved). Can be absolute if rootPath is configured, or relative if rootPath is not configured.
1606
+ * @param isOutsideRootPath Whether the file path is outside the configured root path.
1607
+ * @param dependencySource The import/export source string
1608
+ * @returns True if any of the configured conditions is met, false otherwise
1555
1609
  */
1556
1610
  private _isExternalDependency;
1557
1611
  /**
@@ -1599,27 +1653,28 @@ declare class ElementsDescriptor {
1599
1653
  /**
1600
1654
  * Returns an external or core dependency element given its dependency source and file path.
1601
1655
  * @param dependencySource The source of the dependency.
1602
- * @param filePath The resolved file path of the dependency, if known.
1656
+ * @param isOutsideRootPath Whether the file path is outside the configured root path.
1657
+ * @param filePath The resolved file path of the dependency, if known. Can be absolute if rootPath is configured.
1603
1658
  * @returns The external or core dependency element, or null if it is a local dependency.
1604
1659
  */
1605
1660
  private _getExternalOrCoreDependencyElement;
1606
1661
  /**
1607
1662
  * Describes an element given its file path and dependency source, if any.
1608
- * @param filePath The path of the file to describe.
1663
+ * @param filePath The path of the file to describe. Can be absolute if rootPath is configured, or relative if not.
1609
1664
  * @param dependencySource The source of the dependency, if the element to describe is so. It refers to the import/export path used to reference the file or external module.
1610
1665
  * @returns The description of the element. A dependency element if dependency source is provided, otherwise a file element.
1611
1666
  */
1612
1667
  private _describeElement;
1613
1668
  /**
1614
1669
  * Describes an element given its file path.
1615
- * @param filePath The path of the file to describe.
1670
+ * @param filePath The path of the file to describe. Can be absolute if rootPath is configured, or relative if not.
1616
1671
  * @returns The description of the element.
1617
1672
  */
1618
1673
  describeElement(filePath?: string): FileElement;
1619
1674
  /**
1620
1675
  * Describes a dependency element given its dependency source and file path.
1621
1676
  * @param dependencySource The source of the dependency.
1622
- * @param filePath The path of the file being the dependency, if known.
1677
+ * @param filePath The path of the file being the dependency, if known. Can be absolute if rootPath is configured, or relative if not.
1623
1678
  * @returns The description of the dependency element.
1624
1679
  */
1625
1680
  describeDependencyElement(dependencySource: string, filePath?: string): DependencyElementDescription;
@@ -1791,4 +1846,4 @@ declare class CacheManager<CacheKey extends NotUndefined, CachedValue> {
1791
1846
  setFromSerialized(serializedCache: Record<string, CachedValue>): void;
1792
1847
  }
1793
1848
 
1794
- export { type BaseDependencyElement, type BaseElement, type BaseElementDescriptor, type BaseElementSelector, type BaseElementSelectorData, type BaseElementSelectorWithOptions, type BaseElementsSelector, CacheManager, type CapturedValues, type CapturedValuesSelector, type ConfigOptions, type ConfigOptionsNormalized, type CoreDependencyElement, DEPENDENCY_KINDS_MAP, DEPENDENCY_KIND_TYPE, DEPENDENCY_KIND_TYPEOF, DEPENDENCY_KIND_VALUE, DEPENDENCY_RELATIONSHIPS_INVERTED_MAP, DEPENDENCY_RELATIONSHIPS_MAP, DependenciesDescriptor, type DependenciesDescriptorSerializedCache, DependenciesMatcher, type DependenciesMatcherSerializedCache, type DependencyDescription, type DependencyElementDescription, type DependencyElementSelector, type DependencyElementSelectorData, type DependencyElementSelectorWithOptions, type DependencyElementsSelector, type DependencyKind, type DependencyMatchResult, type DependencyRelationship, type DependencySelector, type DependencySelectorNormalized, type DescribeDependencyOptions, type DescriptionsSerializedCache, type DescriptorOptionsNormalized, Descriptors, type DescriptorsSerializedCache, ELEMENT_DESCRIPTOR_MODES_MAP, ELEMENT_ORIGINS_MAP, type ElementDescription, type ElementDescriptor, type ElementDescriptorMode, type ElementDescriptorPattern, type ElementDescriptorWithCategory, type ElementDescriptorWithType, type ElementDescriptorWithTypeAndCategory, type ElementDescriptors, type ElementOrigin, type ElementParent, type ElementSelector, type ElementSelectorData, type ElementSelectorWithOptions, type ElementSelectors, Elements, type ElementsDependencyInfo, ElementsDescriptor, type ElementsDescriptorSerializedCache, ElementsMatcher, type ElementsMatcherSerializedCache, type ElementsSelector, type ElementsSerializedCache, type ExternalDependencyElement, type ExternalLibrariesSelector, type ExternalLibrarySelector, type ExternalLibrarySelectorOptions, type ExternalLibrarySelectorWithOptions, type ExternalLibrarySelectors, type FileElement, type FileElementsSerializedCache, type IgnoredDependencyElement, type IgnoredElement, type LocalDependencyElement, type LocalDependencyElementKnown, type LocalDependencyElementUnknown, type LocalElementKnown, type LocalElementUnknown, Matcher, type MatcherOptions, type MatcherOptionsDependencySelectorsGlobals, type MatcherSerializedCache, type MatchersOptionsNormalized, type MicromatchPattern, type MicromatchSerializedCache, type NotUndefined, type ObjectCacheKey, type SelectableElement, type SimpleElementSelectorByType, type TemplateData, isBaseElement, isBaseElementDescriptor, isBaseElementSelectorData, isCapturedValuesSelector, isCoreDependencyElement, isDependencyDescription, isDependencyElementDescription, isDependencyKind, isDependencyRelationship, isDependencyRelationshipDescription, isDependencySelector, isElementDescription, isElementDescriptor, isElementDescriptorMode, isElementDescriptorPattern, isElementDescriptorWithCategory, isElementDescriptorWithType, isElementSelector, isElementSelectorData, isElementSelectorWithLegacyOptions, isElementsDependencyInfo, isElementsSelector, isExternalDependencyElement, isExternalLibrariesSelector, isExternalLibrarySelector, isExternalLibrarySelectorOptions, isExternalLibrarySelectorOptionsWithPath, isExternalLibrarySelectorOptionsWithSpecifiers, isExternalLibrarySelectorWithOptions, isIgnoredElement, isInternalDependency, isKnownLocalElement, isLocalDependencyElement, isLocalElement, isSimpleElementSelectorByType, isUnknownLocalElement, normalizeElementsSelector };
1849
+ export { type BaseDependencyElement, type BaseElement, type BaseElementDescriptor, type BaseElementSelector, type BaseElementSelectorData, type BaseElementSelectorWithOptions, type BaseElementsSelector, CacheManager, type CapturedValues, type CapturedValuesSelector, type ConfigOptions, type ConfigOptionsNormalized, type CoreDependencyElement, DEPENDENCY_KINDS_MAP, DEPENDENCY_KIND_TYPE, DEPENDENCY_KIND_TYPEOF, DEPENDENCY_KIND_VALUE, DEPENDENCY_RELATIONSHIPS_INVERTED_MAP, DEPENDENCY_RELATIONSHIPS_MAP, DependenciesDescriptor, type DependenciesDescriptorSerializedCache, DependenciesMatcher, type DependenciesMatcherSerializedCache, type DependencyDescription, type DependencyElementDescription, type DependencyElementSelector, type DependencyElementSelectorData, type DependencyElementSelectorWithOptions, type DependencyElementsSelector, type DependencyKind, type DependencyMatchResult, type DependencyRelationship, type DependencySelector, type DependencySelectorNormalized, type DescribeDependencyOptions, type DescriptionsSerializedCache, type DescriptorOptionsNormalized, Descriptors, type DescriptorsSerializedCache, ELEMENT_DESCRIPTOR_MODES_MAP, ELEMENT_ORIGINS_MAP, type ElementDescription, type ElementDescriptor, type ElementDescriptorMode, type ElementDescriptorPattern, type ElementDescriptorWithCategory, type ElementDescriptorWithType, type ElementDescriptorWithTypeAndCategory, type ElementDescriptors, type ElementOrigin, type ElementParent, type ElementSelector, type ElementSelectorData, type ElementSelectorWithOptions, type ElementSelectors, Elements, type ElementsDependencyInfo, ElementsDescriptor, type ElementsDescriptorSerializedCache, ElementsMatcher, type ElementsMatcherSerializedCache, type ElementsSelector, type ElementsSerializedCache, type ExternalDependencyElement, type ExternalLibrariesSelector, type ExternalLibrarySelector, type ExternalLibrarySelectorOptions, type ExternalLibrarySelectorWithOptions, type ExternalLibrarySelectors, type FileElement, type FileElementsSerializedCache, type FlagAsExternalOptions, type FlagAsExternalOptionsNormalized, type IgnoredDependencyElement, type IgnoredElement, type LocalDependencyElement, type LocalDependencyElementKnown, type LocalDependencyElementUnknown, type LocalElementKnown, type LocalElementUnknown, Matcher, type MatcherOptions, type MatcherOptionsDependencySelectorsGlobals, type MatcherSerializedCache, type MatchersOptionsNormalized, type MicromatchPattern, type MicromatchSerializedCache, type NotUndefined, type ObjectCacheKey, type SelectableElement, type SimpleElementSelectorByType, type TemplateData, isBaseElement, isBaseElementDescriptor, isBaseElementSelectorData, isCapturedValuesSelector, isCoreDependencyElement, isDependencyDescription, isDependencyElementDescription, isDependencyKind, isDependencyRelationship, isDependencyRelationshipDescription, isDependencySelector, isElementDescription, isElementDescriptor, isElementDescriptorMode, isElementDescriptorPattern, isElementDescriptorWithCategory, isElementDescriptorWithType, isElementSelector, isElementSelectorData, isElementSelectorWithLegacyOptions, isElementsDependencyInfo, isElementsSelector, isExternalDependencyElement, isExternalLibrariesSelector, isExternalLibrarySelector, isExternalLibrarySelectorOptions, isExternalLibrarySelectorOptionsWithPath, isExternalLibrarySelectorOptionsWithSpecifiers, isExternalLibrarySelectorWithOptions, isIgnoredElement, isInternalDependency, isKnownLocalElement, isLocalDependencyElement, isLocalElement, isSimpleElementSelectorByType, isUnknownLocalElement, normalizeElementsSelector };