cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Rust - w.a.s.m. support

Open jordems opened this issue 3 years ago • 2 comments

https://rustwasm.github.io/book/introduction.html

Not entirely sure of the schematics of how this would work with nx, or if this would even be considered in the scope of this project. But, I'm more so bringing it up as I'm curious. As this may be a really cool thing to have as an nx library, as then it could export typescript from a barrel file to be used in a frontend application.

Any who, let me know what you think c:

jordems avatar Feb 06 '22 07:02 jordems

This is definitely doable and probably a pretty common use case — I'll look into it when I get the chance.

dannymcgee avatar Feb 06 '22 09:02 dannymcgee

Sorry for taking so long to update this issue! As of #15 you can build a wasm library by adding the target option to your project.json's build target and the crate-type to your Cargo manifest:

[package]
name = "my-wasm-library"
# ...

[lib]
crate-type = ["cdylib", "rlib"]
# ...
{
  "name": "my-wasm-library",
  "targets": {
    "build": {
      "executor": "@nxrs/cargo:build",
      "options": {
        "target": "wasm32-unknown-unknown",
        // ...
      }
    }
    // ...
  }
}

To be usable in a TypeScript project, you would also want to do some other stuff, like run wasm-bindgen after the build and update your tsconfig.json to add a path alias for the build output directory.

This is honestly not a huge priority for me, but nowadays Nx makes it pretty straightforward to do this kind of thing with custom local executors, so it would be fairly straightforward to write one that first runs the @nxrs/cargo:build executor and then runs wasm-bindgen on the build output. It would be cool to have this feature built in to the plugin someday, so I'll leave this open in the meantime. :)

dannymcgee avatar May 08 '24 02:05 dannymcgee