AshTypescript.Codegen.ImportResolver (ash_typescript v0.17.2)

Copy Markdown View Source

Resolves relative import paths between generated TypeScript files and generates namespace re-export content.

Used when generating namespace re-export files that need to import from the main generated file, or when any generated file needs to import from another.

Summary

Functions

Builds import and re-export lines for shared types.

The marker comment used to separate generated code from custom code in namespace files. Content below this marker is preserved when regenerating namespace files.

Resolves custom import paths from import_into_generated config entries relative to a target output file.

Computes a relative TypeScript import path from one file to another.

Functions

build_shared_type_imports(import_paths, shared_type_names, body_content)

Builds import and re-export lines for shared types.

Generates an export type * from re-export, plus a local import type { ... } for only the type names actually referenced in body_content.

Parameters

  • import_paths - Map with optional :types key containing the import path
  • shared_type_names - List of type names exported by the shared types file
  • body_content - The generated TypeScript body to scan for type usage

generate_namespace_reexport_content(namespace, exports, namespace_file, main_file_path, zod_file_path \\ nil, valibot_file_path \\ nil)

Generates a namespace re-export file from a list of categorized exports.

This is the shared implementation used by both RPC and controller namespace generation.

Parameters

  • namespace - The namespace name (used in the header comment)
  • exports - List of {name, kind} tuples where kind is :value, :type, :zod_value, or :valibot_value
  • namespace_file - Full path of the namespace file being generated (for import resolution)
  • main_file_path - Path to the main source file (RPC or routes)
  • zod_file_path - Path to the Zod file (nil to import Zod from main file)
  • valibot_file_path - Path to the Valibot file (nil to import Valibot from main file)

namespace_custom_code_marker()

The marker comment used to separate generated code from custom code in namespace files. Content below this marker is preserved when regenerating namespace files.

resolve_custom_imports(target_output_file, imports)

@spec resolve_custom_imports(String.t(), [map()]) :: String.t()

Resolves custom import paths from import_into_generated config entries relative to a target output file.

Each import config is a map with :import_name and :file keys, where :file is a project-root-relative path (e.g., "assets/js/hooks.ts").

Parameters

  • target_output_file - The output file that will contain the import statements
  • imports - List of import config maps with :import_name and :file keys

Examples

iex> resolve_custom_imports("assets/js/ash_rpc.ts", [%{import_name: "Hooks", file: "assets/js/hooks.ts"}])
"import * as Hooks from \"./hooks\";"

resolve_import_path(from_file, to_file)

@spec resolve_import_path(String.t(), String.t()) :: String.t()

Computes a relative TypeScript import path from one file to another.

Both paths should be relative to the project root (e.g., "assets/js/ash_rpc.ts"). Returns a relative path suitable for TypeScript import statements (without .ts extension).

Examples

iex> resolve_import_path("assets/js/namespace/todos.ts", "assets/js/ash_rpc.ts")
"../ash_rpc"

iex> resolve_import_path("assets/js/todos.ts", "assets/js/ash_rpc.ts")
"./ash_rpc"