Skip to content

Reenable Specta integration #2432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ fern = { version = "0.7", features = ["colored"] }
num_enum = "0.7"
num-derive = "0.4"
num-traits = { version = "0.2", default-features = false, features = ["i128"] }
specta = { version = "2.0.0-rc.22", features = [
"glam",
specta = { version = "=2.0.0-rc.22", features = [
"derive",
# "typescript",
"glam",
] }
specta-typescript = { version = "=0.0.9" }
syn = { version = "2.0", default-features = false, features = [
"full",
"derive",
Expand Down
1 change: 1 addition & 0 deletions editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ futures = { workspace = true }
glam = { workspace = true, features = ["serde", "debug-glam-assert"] }
derivative = { workspace = true }
specta = { workspace = true }
specta-typescript = { workspace = true }
image = { workspace = true, features = ["bmp", "png"] }
dyn-any = { workspace = true }
num_enum = { workspace = true }
Expand Down
40 changes: 9 additions & 31 deletions editor/src/generate_ts_types.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
/// Running this test will generate a `types.ts` file at the root of the repo,
/// containing every type annotated with `specta::Type`
// #[cfg(all(test, feature = "specta-export"))]
#[ignore]
/// Running this test will generate a `bindings.ts` file containing every type annotated with `specta::Type`.
#[test]
fn generate_ts_types() {
// TODO: Un-comment this out when we figure out how to reenable the "typescript` Specta feature flag
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this but it's probally not needed with the wasm exporting solution (hence why it's still #[ignore]ed)


// use crate::messages::prelude::FrontendMessage;
// use specta::ts::{export_named_datatype, BigIntExportBehavior, ExportConfig};
// use specta::{NamedType, TypeMap};
// use std::fs::File;
// use std::io::Write;

// let config = ExportConfig::new().bigint(BigIntExportBehavior::Number);

// let mut type_map = TypeMap::default();

// let datatype = FrontendMessage::definition_named_data_type(&mut type_map);

// let mut export = String::new();

// export += &export_named_datatype(&config, &datatype, &type_map).unwrap();

// type_map
// .iter()
// .map(|(_, v)| v)
// .flat_map(|v| export_named_datatype(&config, v, &type_map))
// .for_each(|e| export += &format!("\n\n{e}"));

// let mut file = File::create("../types.ts").unwrap();

// write!(file, "{export}").ok();
use crate::messages::prelude::FrontendMessage;
use specta::TypeCollection;
use specta_typescript::{BigIntExportBehavior, Typescript};

Typescript::default()
.bigint(BigIntExportBehavior::Number)
.export_to("../frontend/src/bindings.ts", TypeCollection::default().register::<FrontendMessage>())
.unwrap();
}
276 changes: 276 additions & 0 deletions frontend/bindings_from_node.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions frontend/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ web-sys = { workspace = true, features = [
"HtmlCanvasElement",
"IdleRequestOptions",
] }
specta = { workspace = true }
specta-typescript = { workspace = true }

# Optional workspace dependencies
ron = { workspace = true, optional = true }
Expand Down
7 changes: 7 additions & 0 deletions frontend/wasm/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import graphite, { get_specta_types } from "./pkg/graphite_wasm.js";

graphite(fs.readFileSync(path.join(import.meta.dirname, './pkg/graphite_wasm_bg.wasm'))).then(() =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We import the wasm file and then call the get_specta_types function and save the result to the disk.

By doing this we don't need to build a separate x86/ARM binary specially for exporting the types. Commonly this would be done via cargo test. By having it be included in the binary makes it way quicker for Graphite's specific setup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mostly following how this works, but still confused about when and how to use it and how it compares to the #[test] fn generate_ts_types(). How do we run this export.js file and when is that supposed to occur? The goal would be to have the bindings file be gitignored but autogenerated whenever the build server starts, or when recompiling the build server based on any change to a Rust file, which should pick up the changes to any of the Specta bindings if those structures change (without having to restart the dev server). Is the idea that each wasm binary has this bindings string enclosed and I need to add something to our Vite build process (on the web bundler side of things) to run this export.js file each time the Wasm bundle updates?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mostly following how this works, but still confused about when and how to use it and how it compares to the #[test] fn generate_ts_types().

Rust requires rebuilding all of the crates before they can be used to run cargo test. So the idea with exposing the bindings from the wasm file is that we only need to run a single build to get the updated bindings. This makes the whole thing much faster.

We can drop the unit tests if we go along with this. I just kept in the code so it's possible to see both possible solutions.

goal would be to have the bindings file be gitignored but autogenerated

I would generally advise against this although feel free to do whatever you think is best. I personally think having the types available in code review and when you clone in the codebase is important.

fs.writeFileSync("bindings_from_node.ts", get_specta_types())
);
12 changes: 12 additions & 0 deletions frontend/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ pub fn init_graphite() {
log::set_max_level(log::LevelFilter::Debug);
}

#[cfg(debug_assertions)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expose a wasm function that returns a string with all the Specta types.

This is only enabled in development so Rust can strip more for production builds.

#[wasm_bindgen]
pub fn get_specta_types() -> Result<String, String> {
use specta::TypeCollection;
use specta_typescript::{BigIntExportBehavior, Typescript};

Typescript::default()
.bigint(BigIntExportBehavior::Number)
.export(TypeCollection::default().register::<FrontendMessage>())
.map_err(|err| err.to_string())
}

/// When a panic occurs, notify the user and log the error to the JS console before the backend dies
pub fn panic_hook(info: &panic::PanicHookInfo) {
let info = info.to_string();
Expand Down
Loading