Skip to content

Commit

Permalink
fix issues with latest getrandom 0.3 and wasm target
Browse files Browse the repository at this point in the history
 - reworked the wasm use case and removed the wasm feature flag, putting
  everything behind the target specific dependencies

Signed-off-by: Andrei Gherghescu <[email protected]>
  • Loading branch information
andrei-ng committed Feb 1, 2025
1 parent b9884f0 commit c6b1e3a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ jobs:
with:
components: clippy
targets: wasm32-unknown-unknown
# lint the main library workspace excluding the wasm feature
- run: cargo clippy --features plotly_ndarray,plotly_image,kaleido -- -D warnings
# lint the plotly library with wasm enabled
- run: cargo clippy --package plotly --features wasm --target wasm32-unknown-unknown -- -D warnings
# lint the main library workspace for non-wasm target
- run: cargo clippy --all-features -- -D warnings
# lint the non-wasm examples
- run: cd ${{ github.workspace }}/examples && cargo clippy --workspace --exclude "wasm*" -- -D warnings
# lint the plotly library for wasm target
- run: cargo clippy --package plotly --target wasm32-unknown-unknown -- -D warnings
# lint the wasm examples
- run: cd ${{ github.workspace }}/examples && cargo clippy --target wasm32-unknown-unknown --package "wasm*"

Expand Down Expand Up @@ -83,8 +83,6 @@ jobs:
with:
components: llvm-tools-preview
- uses: taiki-e/install-action@cargo-llvm-cov
# we are skipping anything to do with wasm here
- run: cargo llvm-cov --workspace --features plotly_ndarray,plotly_image,kaleido --lcov --output-path lcov.info
- uses: codecov/codecov-action@v3

build_examples:
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm-yew-minimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = [
edition = "2021"

[dependencies]
plotly = { path = "../../plotly", features = ["wasm"] }
plotly = { path = "../../plotly" }
yew = "0.21"
yew-hooks = "0.3"
log = "0.4"
Expand Down
10 changes: 5 additions & 5 deletions plotly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ plotly_ndarray = ["ndarray"]
plotly_image = ["image"]
plotly_embed_js = []

wasm = ["getrandom", "js-sys", "wasm-bindgen", "wasm-bindgen-futures"]
with-axum = ["rinja/with-axum", "rinja_axum"]

[dependencies]
rinja = { version = "0.3", features = ["serde_json"] }
rinja_axum = { version = "0.3", optional = true }
dyn-clone = "1"
erased-serde = "0.4"
getrandom = { version = "0.2", features = ["js"], optional = true }
image = { version = "0.25", optional = true }
js-sys = { version = "0.3", optional = true }
plotly_derive = { version = "0.12", path = "../plotly_derive" }
plotly_kaleido = { version = "0.12", path = "../plotly_kaleido", optional = true }
ndarray = { version = "0.16", optional = true }
Expand All @@ -41,8 +38,11 @@ serde_json = "1.0"
serde_repr = "0.1"
serde_with = ">=2, <4"
rand = "0.9"
wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.3", features = ["wasm_js"] }
wasm-bindgen-futures = { version = "0.4" }
wasm-bindgen = { version = "0.2"}

[dev-dependencies]
csv = "1.1"
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//! context, where it is assumed that a remote copy of the Javascript Plotly
//! library is available, (i.e. via a CDN).
use js_sys::Object;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::js_sys::Object;

use crate::Plot;

Expand Down
6 changes: 3 additions & 3 deletions plotly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ extern crate rand;
extern crate rinja;
extern crate serde;

#[cfg(all(feature = "kaleido", feature = "wasm"))]
#[cfg(all(feature = "kaleido", target_family = "wasm"))]
compile_error!(
r#"The "kaleido" and "wasm" features are mutually exclusive and cannot be activated at the same time. Please disable one or the other."#
r#"The "kaleido" feature is not available on "wasm" targets. Please compile without this feature for the wasm target family."#
);

#[cfg(feature = "plotly_ndarray")]
pub mod ndarray;
#[cfg(feature = "plotly_ndarray")]
pub use crate::ndarray::ArrayTraces;

#[cfg(feature = "wasm")]
#[cfg(target_family = "wasm")]
pub mod bindings;

pub mod common;
Expand Down
9 changes: 5 additions & 4 deletions plotly/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,11 @@ impl Plot {
serde_json::to_string(self).unwrap()
}

#[cfg(feature = "wasm")]
#[cfg(target_family = "wasm")]
/// Convert a `Plot` to a native Javasript `js_sys::Object`.
pub fn to_js_object(&self) -> js_sys::Object {
use wasm_bindgen::JsCast;
pub fn to_js_object(&self) -> wasm_bindgen_futures::js_sys::Object {
use wasm_bindgen_futures::js_sys;
use wasm_bindgen_futures::wasm_bindgen::JsCast;
// The only reason this could fail is if to_json() produces structurally
// incorrect JSON. That would be a bug, and would require fixing in the
// to_json()/serialization methods, rather than here
Expand Down Expand Up @@ -734,7 +735,7 @@ mod tests {

#[test]
#[ignore] // Don't really want it to try and open a browser window every time we run a test.
#[cfg(not(feature = "wasm"))]
#[cfg(not(target_family = "wasm"))]
fn show_image() {
let plot = create_test_plot();
plot.show_image(ImageFormat::PNG, 1024, 680);
Expand Down

0 comments on commit c6b1e3a

Please sign in to comment.