module_helpers.ts

Module path and metadata helpers.

Provides utilities for working with source module paths, file types, and import relationships in the package generation system.

All functions are prefixed with module_ for clarity.

Declarations
#

19 declarations

view source

AnalyzerType
#

module_helpers.ts view source

AnalyzerType

Analyzer type for source files.

- 'typescript' - TypeScript/JS files analyzed via TypeScript Compiler API - 'svelte' - Svelte components analyzed via svelte2tsx + TypeScript Compiler API

module_create_source_options
#

module_helpers.ts view source

(project_root: string, overrides?: Partial<ModuleSourcePartial> | undefined): ModuleSourceOptions

Create complete source options from project root and optional overrides.

project_root

Absolute path to project root (typically process.cwd())

type string

overrides?

Optional overrides for default options

type Partial<ModuleSourcePartial> | undefined
optional

returns

ModuleSourceOptions

examples

// Standard SvelteKit library const options = module_create_source_options(process.cwd());
// Multiple source directories const options = module_create_source_options(process.cwd(), { source_paths: ['src/lib', 'src/routes'], source_root: 'src', });
// Custom exclusions const options = module_create_source_options(process.cwd(), { exclude_patterns: [/\.test\.ts$/, /\.internal\.ts$/], });

module_extract_dependencies
#

module_helpers.ts view source

(source_file: SourceFileInfo, options: ModuleSourceOptions): { dependencies: string[]; dependents: string[]; }

Extract dependencies and dependents for a module from source file info.

Filters to only include source modules (excludes external packages, node_modules, tests). Returns sorted arrays of module paths (relative to source_root) for deterministic output.

source_file

The source file info to extract dependencies from

options

Module source options for filtering and path extraction

returns

{ dependencies: string[]; dependents: string[]; }

module_extract_path
#

module_helpers.ts view source

(source_id: string, options: ModuleSourceOptions): string

Extract module path relative to source root from absolute source ID.

Uses proper path semantics: strips project_root/source_root/ prefix.

source_id

Absolute path to the source file

type string

options

Module source options for path extraction

returns

string

examples

const options = module_create_source_options('/home/user/project'); module_extract_path('/home/user/project/src/lib/foo.ts', options) // => 'foo.ts' module_extract_path('/home/user/project/src/lib/nested/bar.svelte', options) // => 'nested/bar.svelte'
const options = module_create_source_options('/home/user/project', { source_paths: ['src/lib', 'src/routes'], source_root: 'src', }); module_extract_path('/home/user/project/src/lib/foo.ts', options) // => 'lib/foo.ts' module_extract_path('/home/user/project/src/routes/page.svelte', options) // => 'routes/page.svelte'

module_get_analyzer_default
#

module_helpers.ts view source

(path: string): AnalyzerType | null

Default analyzer resolver based on file extension.

- .svelte'svelte' - .ts, .js'typescript' - Other extensions → null (skip)

path

type string

returns

AnalyzerType | null

module_get_component_name
#

module_helpers.ts view source

(module_path: string): string

Extract component name from a Svelte module path.

module_path

type string

returns

string

examples

module_get_component_name('Alert.svelte') // => 'Alert' module_get_component_name('components/Button.svelte') // => 'Button'

module_get_key
#

module_helpers.ts view source

(module_path: string): string

Convert module path to module key format (with ./ prefix).

module_path

type string

returns

string

examples

module_get_key('foo.ts') // => './foo.ts'

module_get_source_root
#

module_helpers.ts view source

(options: ModuleSourceOptions): string

Get the effective source_root from options.

Returns source_root if provided, otherwise returns source_paths[0] for single-path configs.

options

returns

string

throws

  • Error - if source_root is required but not provided (multiple source_paths)

module_is_css
#

module_is_json
#

module_is_source
#

module_helpers.ts view source

(path: string, options: ModuleSourceOptions): boolean

Check if a path is an analyzable source file.

Combines all filtering: exclusion patterns, source directory paths, and analyzer availability. This is the single check for whether a file should be included in library analysis.

Uses proper path semantics with startsWith matching against project_root/source_path/. No heuristics needed - nested directories are correctly excluded by the prefix check.

path

Full absolute path to check

type string

options

Module source options for filtering

returns

boolean

True if the path is an analyzable source file

examples

const options = module_create_source_options('/home/user/project'); module_is_source('/home/user/project/src/lib/foo.ts', options) // => true module_is_source('/home/user/project/src/lib/foo.test.ts', options) // => false (excluded) module_is_source('/home/user/project/src/fixtures/mini/src/lib/bar.ts', options) // => false (wrong prefix)

module_is_svelte
#

module_is_test
#

module_is_typescript
#

module_helpers.ts view source

(path: string): boolean

Check if a path is a TypeScript or JS file.

Includes both .ts and .js files since JS files are valid in TS projects. Excludes .d.ts declaration files - use a custom get_analyzer to include them.

path

type string

returns

boolean

MODULE_SOURCE_PARTIAL
#

module_helpers.ts view source

ModuleSourcePartial

Default partial options for standard SvelteKit library structure.

Does not include project_root - use module_create_source_options() to create complete options with your project root.

module_validate_source_options
#

module_helpers.ts view source

(options: ModuleSourceOptions): void

Validate ModuleSourceOptions format and consistency.

Checks: 1. project_root is an absolute path (starts with /) 2. source_paths entries don't have leading/trailing slashes 3. source_root (if provided) doesn't have leading/trailing slashes 4. Multiple source_paths require explicit source_root 5. source_root is a prefix of all source_paths

options

returns

void

throws

  • Error - if validation fails

examples

// Valid - single source path (source_root auto-derived) module_validate_source_options({ project_root: '/home/user/project', source_paths: ['src/lib'], ... });
// Valid - multiple source paths with explicit source_root module_validate_source_options({ project_root: '/home/user/project', source_paths: ['src/lib', 'src/routes'], source_root: 'src', ... });
// Invalid - multiple source paths without source_root module_validate_source_options({ project_root: '/home/user/project', source_paths: ['src/lib', 'src/routes'], // throws ... });

ModuleSourceOptions
#

module_helpers.ts view source

ModuleSourceOptions

Configuration for module source detection and path extraction.

Uses proper path semantics with project_root as the base for all path operations. Paths are matched using startsWith rather than substring search, which correctly handles nested directories without special heuristics.

examples

const options = module_create_source_options(process.cwd(), { source_paths: ['src/lib', 'src/routes'], source_root: 'src', });

project_root

Absolute path to the project root directory.

All source_paths are relative to this. Typically process.cwd() when running from the project root via Gro, Vite, or other build tools.

type string

source_paths

Source directory paths to include, relative to project_root.

Paths should not have leading or trailing slashes - they are added internally for correct matching.

type Array<string>

source_root

Source root for extracting relative module paths, relative to project_root.

When omitted: - Single source_path: defaults to that path - Multiple source_paths: required (no auto-derivation)

type string

exclude_patterns

Patterns to exclude (matched against full path).

type Array<RegExp>

get_analyzer

Determine which analyzer to use for a file path.

Called for files in source directories. Return 'typescript', 'svelte', or null to skip the file. This is the single source of truth for which files are analyzable and how to analyze them.

type (path: string) => AnalyzerType | null

ModuleSourcePartial
#

SourceFileInfo
#

module_helpers.ts view source

SourceFileInfo

File information for source analysis.

Can be constructed from Gro's Disknode or from plain file system access. This abstraction enables non-Gro usage while keeping Gro support via adapter.

Note: content is required to keep analysis functions pure (no hidden I/O). Callers are responsible for reading file content before analysis.

id

Absolute path to the file.

type string

content

File content (required - analysis functions don't read from disk).

type string

dependencies

Absolute file paths of modules this file imports (optional). Only include resolved local imports, not node_modules. Order should be declaration order in source for deterministic output.

type ReadonlyArray<string>

dependents

Absolute file paths of modules that import this file (optional). Only include resolved local imports, not node_modules.

type ReadonlyArray<string>

Imported by
#