expand_sync

Function expand_sync 

Source
pub fn expand_sync(
    _env: Env,
    code: String,
    filepath: String,
    options: Option<ExpandOptions>,
) -> Result<ExpandResult>
Expand description

Synchronously expands macros in TypeScript code.

This is the standalone macro expansion function that doesn’t use caching. For cached expansion, use NativePlugin::process_file instead.

§Arguments

  • _env - NAPI environment (unused but required by NAPI)
  • code - The TypeScript source code to expand
  • filepath - The file path (used for TSX detection)
  • options - Optional configuration (e.g., keep_decorators)

§Returns

An ExpandResult containing the expanded code, diagnostics, and source mapping.

§Errors

Returns an error if:

  • Thread spawning fails
  • The worker thread panics
  • Macro host initialization fails

§Performance

  • Uses a 32MB thread stack to prevent stack overflow
  • Performs early bailout for files without @derive decorators

§Example

const result = expand_sync(env, code, "user.ts", { keep_decorators: false });
console.log(result.code); // Expanded TypeScript code
console.log(result.diagnostics); // Any warnings or errors