-
-
Notifications
You must be signed in to change notification settings - Fork 587
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
base: master
Are you sure you want to change the base?
Reenable Specta integration #2432
Conversation
Thank you for writing the library and helping us restore this! I'm eager to do the refactor which replaces |
aa7ff13
to
e11b57a
Compare
Back to reviewing this now! Could you please help me understand the purpose of |
@@ -34,6 +34,18 @@ pub fn init_graphite() { | |||
log::set_max_level(log::LevelFilter::Debug); | |||
} | |||
|
|||
#[cfg(debug_assertions)] |
There was a problem hiding this comment.
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.
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(() => |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@@ -0,0 +1,276 @@ | |||
// This file has been generated by Specta. DO NOT EDIT. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the result of all the types that Specta generated from Graphite's codebase.
These can be used anywhere in Graphite's frontend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be gitignored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And is there a way to get Specta to generate undefined
instead of null
in this file? We avoid null
by convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be gitignored?
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.
And is there a way to get Specta to generate undefined instead of null in this file? We avoid null by convention.
I think you can use #[specta(optional)]
on each of the fields.
@@ -1,34 +1,15 @@ | |||
/// 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] | |||
|
|||
#[test] | |||
fn generate_ts_types() { |
There was a problem hiding this comment.
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)
I also removed the |
We can build the |
Be aware that will require either:
|
Yeah, was thinking that we could put the build script in the frontend |
That sounds great! |
We just need to reexport the specta types static global from the editor so we can consume in then the wasm crate but that is easy to do |
This fixes the commented out Specta integration resolving the TODO comment.
It might be worth moving the type exporting code into the startup of the application if possible when doing a debug build so that you don't need to run
cargo test ...
to generate the bindings but I don't know enough about Graphite's setup to know where is best. I also suppose any Rust running in wasm would not be a good place for that as it won't have filesystem access so a better place may not exist.TODO:
Part of #1148