Skip to main content

compile

Function compile 

Source
pub fn compile(
    input: &Program,
    wit: &Wit,
    options: &Options,
) -> Result<Compiled, CompileError>
Expand description

Compile in-memory TypeScript source modules and WIT package definitions into a validated WASI 0.3 WebAssembly component.

use perry_wit::api::{self, Options, Program, Wit};

let program = Program {
    source: br#"
        export function authorize(token: string): boolean {
            return token.startsWith("Bearer ") && token.length > 16;
        }
    "#
    .as_slice()
    .into(),
    modules: vec![],
};

let wit = Wit {
    application: br#"
        package example:auth@0.1.0;
        world filter {
            export authorize: func(token: string) -> bool;
        }
    "#
    .as_slice()
    .into(),
    dependencies: vec![],
};

let compiled = api::compile(&program, &wit, &Options::default())?;
assert!(!compiled.core.is_empty());
assert!(compiled.component.is_some_and(|component| !component.bytes.is_empty()));