Skip to content

Commit

Permalink
feat: add initial support for guest-types command
Browse files Browse the repository at this point in the history
  • Loading branch information
lachieh committed Dec 12, 2024
1 parent 381df49 commit 40bebe2
Show file tree
Hide file tree
Showing 37 changed files with 6,192 additions and 151 deletions.
2 changes: 2 additions & 0 deletions crates/js-component-bindgen-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl Guest for JsComponentBindgenComponent {
no_namespaced_exports: options.no_namespaced_exports.unwrap_or(false),
multi_memory: options.multi_memory.unwrap_or(false),
import_bindings: options.import_bindings.map(Into::into),
guest: options.guest.unwrap_or(false),
};

let js_component_bindgen::Transpiled {
Expand Down Expand Up @@ -160,6 +161,7 @@ impl Guest for JsComponentBindgenComponent {
no_namespaced_exports: false,
multi_memory: false,
import_bindings: None,
guest: opts.guest.unwrap_or(false),
};

let files = generate_types(name, resolve, world, opts).map_err(|e| e.to_string())?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ world js-component-bindgen {
/// Whether to generate namespaced exports like `foo as "local:package/foo"`.
/// These exports can break typescript builds.
no-namespaced-exports: option<bool>,

/// Whether to generate module declarations like `declare module "local:package/foo" {...`.
guest: option<bool>,

/// Whether to output core Wasm utilizing multi-memory or to polyfill
/// this handling.
Expand Down Expand Up @@ -91,6 +94,8 @@ world js-component-bindgen {
map: option<maps>,
/// Features that should be enabled as part of feature gating
features: option<enabled-feature-set>,
/// Whether to generate module declarations like `declare module "local:package/foo" {...`.
guest: option<bool>,
}

enum export-type {
Expand Down
2 changes: 2 additions & 0 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct TranspileOpts {
/// Whether to output core Wasm utilizing multi-memory or to polyfill
/// this handling.
pub multi_memory: bool,
/// Whether to generate types for a guest module using module declarations.
pub guest: bool,
}

#[derive(Default, Clone, Debug)]
Expand Down
12 changes: 11 additions & 1 deletion crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct TsBindgen {
import_object: Source,
/// TypeScript definitions which will become the export object
export_object: Source,

/// Whether or not the types should be generated for a guest module
guest: bool,
}

/// Used to generate a `*.d.ts` file for each imported and exported interface for
Expand Down Expand Up @@ -59,6 +62,7 @@ pub fn ts_bindgen(
local_names: LocalNames::default(),
import_object: Source::default(),
export_object: Source::default(),
guest: opts.guest,
};

let world = &resolve.worlds[id];
Expand Down Expand Up @@ -520,9 +524,15 @@ impl TsBindgen {
return local_name;
}

let module_or_namespace = if self.guest {
format!("declare module '{id_name}' {{")
} else {
format!("export namespace {camel} {{")
};

let mut gen = self.ts_interface(resolve, false);

uwriteln!(gen.src, "export namespace {camel} {{");
uwriteln!(gen.src, "{module_or_namespace}");
for (_, func) in resolve.interfaces[id].functions.iter() {
// Ensure that the function the world item for stability guarantees and exclude if they do not match
if !feature_gate_allowed(resolve, package, &func.stability, &func.name)
Expand Down
107 changes: 61 additions & 46 deletions examples/components/add/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/components/add/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"all": "npm run build && npm run transpile && npm run transpiled-js"
},
"devDependencies": {
"@bytecodealliance/jco": "1.7.1",
"@bytecodealliance/componentize-js": "0.13.1"
"@bytecodealliance/jco": "1.8.1",
"@bytecodealliance/componentize-js": "0.15.0"
}
}
4 changes: 4 additions & 0 deletions examples/components/http-hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
*.wasm
pnpm-lock.yaml
1 change: 1 addition & 0 deletions examples/components/http-hello-world/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.5.1
Loading

0 comments on commit 40bebe2

Please sign in to comment.