Skip to content

Commit d38ce42

Browse files
jstarrydaxpeddajplattejetli
authored
Add support for building with web-sys (#961)
* Enable travis * `web-sys` general conversion (#826) * Moved patches from different PRs. * Add bits & pieces and some services. * Rename `stdweb` feature to `std_web`. * Move tests and examples to different PR. * Revert some `cargo_web` handling removal. * Missed something. * Implement `console_error_panic_hook`. * Update Cargo.toml Co-authored-by: Justin Starry <justin.m.starry@gmail.com> * Move document creation to util convenience method (#855) * `web-sys` listener conversion (#813) * `web-sys` listener initial try. * Improve macros? * Remove generic from `EventListenerHandle`. * Fix build. * A cleaner solution? * Even cleaner. * Fix `build.rs`. * Minor improvements. * Following the yew toml style. * Fixing visibility. * Fix `rustfmt`. * Add `web-sys` re-exports. * Move general changes to different PR. * Remove compat. * Actually remove `compat.rs`. * Rename `stdweb` feature to `std_web`. * Move to gloo's `EventListener` and some polish. * Remove outdated comment. * Change `EventHandler` to be cancelled on drop. * `web-sys` html conversion (#817) * Converting all `html` parts. * Format. * Move general changes to different PR. * Removed compat. * Rename `stdweb` feature to `std_web`. * Remove redudant function copy. * Some polish. * Move to gloo's `EventListener`. * Replace `unwrap`s with `expect`s. * `web-sys` agent conversion (#818) * Converting `agent`. * Remove wrong `cfg` in imports. * Move general changes to different PR. * Some optimisations. * Rename `stdweb` feature to `std_web`. * Fix `ArrayBuffer` to `Uint8Array` conversions. * Add js module worker. * Use `cfg-if`` and `cfg-match` to make things clearer. * Fix `std_web` build. * Add some polish. * Add build guards for invalid build configs (#866) * `web_sys` cfg conversion (#862) * Use `cfg-if` and `cfg-match` and some polish. * Mistakes were made. * Missed line during rebasing. * Mistakes were undone. * Remove global. * Remove part of `global!`. * `web-sys` services conversion (#827) * Convert `console`. * Finish services. * Some polish. * Fix `ArrayBuffer` to `Uint8Array` conversions. * Fix aborting fetch leading to error and some polish. * Replaced some `unwrap`s with `expect`s. * Use `cfg_if` and `cfg_match` and do some polish. * Proper scoping. * Some fixes. * Move fetch and reader services to different PR. * Revert split. * Fix CI builds (#877) * Fix derive_props_test * Move tests (#897) (#898) * `web-sys` fetch service conversion (#867) * Split implementation. * Import global. * Import global. * Revert split. * Make fetch available again. * Revert "Revert split." This reverts commit 6e3f101. * Re-revert split. * Some polish. * Move to `wasm_bindgen_futures`. * Switch to `thiserror`. * wip * Update src/services/fetch/web_sys.rs Co-Authored-By: daxpedda <daxpedda@gmail.com> * Some more polish. Co-authored-by: Justin Starry <justin.m.starry@gmail.com> * `web-sys` reader service conversion (#868) * Split reader implementation. * Revert split. * Remove leftover files. * Make reader available again. * Revert "Revert split." This reverts commit 8abdc9c. * Revert "Remove leftover files." This reverts commit 188c6eb. * Re-revert split. * Polish. * Forgot some part. * Some polish. * Some polish. * `web-sys` examples/tests conversion (#841) * Fix examples/tests to work with `web_sys`. * Update `StorageService` usage. * Split `stdweb` and `web-sys` examples. * Fixing the shell script. * Trying to reset file permissions. * Update to new reader API. * Update to new fetch API. * Update to new fetch API. * Re-enable examples CI. * Deleted duplicate example. * Some fixes. * Fix rand build. * Fix spawning workers in combination with `wasm-bindgen`. (#901) * Fix component rendering process (#913) * wip * Fix component rendering process * Simplify yew-macro a bit (#902) * yew-macro: Simplify Properties validation * Fix most clippy warnings * Fix clippy warnings (#912) * Import Task trait in dashboard example * Remove duplicate vtag tests * Fix prevent_default() by non-passive (#958) * Fix prevent_default() by non-passive * Fix cargo fmt * Remove `Option` from most services. * Remove `Option` from resize service. * Apply fetch changes. * Apply reader service changes. * Fix `node_refs` example. * Remove web-sys travis branch Co-authored-by: daxpedda <daxpedda@gmail.com> Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com> Co-authored-by: Jet Li <jing.i.qin@icloud.com>
1 parent 2a7d7ac commit d38ce42

File tree

195 files changed

+4953
-1242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+4953
-1242
lines changed

Cargo.toml

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ travis-ci = { repository = "yewstack/yew" }
2222
anyhow = "1"
2323
anymap = "0.12"
2424
bincode = { version = "~1.2.1", optional = true }
25+
cfg-if = "0.1"
26+
cfg-match = "0.2"
27+
console_error_panic_hook = { version = "0.1", optional = true }
28+
futures = { version = "0.3", optional = true }
29+
gloo = { version = "0.2", optional = true }
2530
http = "0.2"
2631
indexmap = "1.0.2"
32+
js-sys = { version = "0.3", optional = true }
2733
log = "0.4"
2834
proc-macro-hack = "0.5"
2935
proc-macro-nested = "0.1"
@@ -33,14 +39,73 @@ serde_cbor = { version = "0.11.1", optional = true }
3339
serde_json = "1.0"
3440
serde_yaml = { version = "0.8.3", optional = true }
3541
slab = "0.4"
36-
stdweb = "0.4.20"
42+
stdweb = { version = "0.4.20", optional = true }
3743
thiserror = "1"
3844
toml = { version = "0.5", optional = true }
45+
wasm-bindgen = { version = "0.2.58", optional = true }
46+
wasm-bindgen-futures = { version = "0.4", optional = true }
3947
yew-macro = { version = "0.12.0", path = "crates/macro" }
4048

49+
[dependencies.web-sys]
50+
version = "0.3"
51+
optional = true
52+
features = [
53+
"AbortController",
54+
"AbortSignal",
55+
"BinaryType",
56+
"Blob",
57+
"BlobPropertyBag",
58+
"console",
59+
"DedicatedWorkerGlobalScope",
60+
"Document",
61+
"DomException",
62+
"DomTokenList",
63+
"DragEvent",
64+
"Element",
65+
"Event",
66+
"EventTarget",
67+
"File",
68+
"FileList",
69+
"FileReader",
70+
"FocusEvent",
71+
"Headers",
72+
"HtmlElement",
73+
"HtmlInputElement",
74+
"HtmlSelectElement",
75+
"HtmlTextAreaElement",
76+
"KeyboardEvent",
77+
"Location",
78+
"MessageEvent",
79+
"MouseEvent",
80+
"Node",
81+
"ObserverCallback",
82+
"PointerEvent",
83+
"ReferrerPolicy",
84+
"Request",
85+
"RequestCache",
86+
"RequestCredentials",
87+
"RequestInit",
88+
"RequestMode",
89+
"RequestRedirect",
90+
"Response",
91+
"Storage",
92+
"Text",
93+
"TouchEvent",
94+
"UiEvent",
95+
"Url",
96+
"WebSocket",
97+
"WheelEvent",
98+
"Window",
99+
"Worker",
100+
"WorkerGlobalScope",
101+
"WorkerOptions",
102+
]
103+
104+
# Changes here must be reflected in `build.rs`
41105
[target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dependencies]
42106
wasm-bindgen = "0.2.58"
43107

108+
# Changes here must be reflected in `build.rs`
44109
[target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dev-dependencies]
45110
wasm-bindgen-test = "0.3.4"
46111
base64 = "0.11.0"
@@ -57,9 +122,10 @@ rmp-serde = "0.14.0"
57122
bincode = "~1.2.1"
58123

59124
[features]
60-
default = ["services", "agent"]
125+
default = []
126+
std_web = ["stdweb"]
127+
web_sys = ["console_error_panic_hook", "futures", "gloo", "js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures"]
61128
doc_test = []
62-
web_test = []
63129
wasm_test = []
64130
services = []
65131
agent = ["bincode"]

build.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
use std::env;
22

33
pub fn main() {
4-
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
4+
if cfg!(all(feature = "web_sys", feature = "std_web")) {
5+
panic!("Yew does not allow the `web_sys` and `std_web` cargo features to be used simultaneously");
6+
} else if cfg!(not(any(feature = "web_sys", feature = "std_web"))) {
7+
panic!("Yew requires selecting either the `web_sys` or `std_web` cargo feature");
8+
}
9+
10+
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
11+
let using_wasi = target_os == "wasi";
12+
513
let cargo_web = env::var("COMPILING_UNDER_CARGO_WEB").unwrap_or_default();
6-
if target_arch == "wasm32" && cargo_web != "1" {
7-
println!("cargo:rustc-cfg=feature=\"wasm_bindgen_test\"");
14+
let using_cargo_web = cargo_web == "1";
15+
if using_cargo_web && cfg!(feature = "web_sys") {
16+
panic!("cargo-web is not compatible with web-sys");
17+
}
18+
19+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
20+
let using_wasm_bindgen = target_arch == "wasm32" && !using_cargo_web && !using_wasi;
21+
if !using_wasm_bindgen && cfg!(all(feature = "web_sys", not(feature = "doc_test"))) {
22+
let target = env::var("TARGET").unwrap_or_default();
23+
panic!(
24+
"Selected target `{}` is not compatible with web-sys",
25+
target
26+
);
827
}
928
}

ci/check_examples.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ cd examples/showcase
1010

1111
if [ "$emscripten_supported" == "0" ]; then
1212
# TODO - Emscripten builds are broken on rustc > 1.39.0
13-
cargo web build --target asmjs-unknown-emscripten
14-
cargo web build --target wasm32-unknown-emscripten
13+
cargo web build --target asmjs-unknown-emscripten --features std_web
14+
cargo web build --target wasm32-unknown-emscripten --features std_web
1515
fi
1616

17-
# TODO showcase doesn't support wasm-bindgen yet
18-
cargo web build --target wasm32-unknown-unknown
17+
cargo web build --target wasm32-unknown-unknown --features std_web
18+
cargo build --target wasm32-unknown-unknown --features web_sys
1919

2020
# Reset cwd
2121
cd ../..

ci/run_checks.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ fi
88

99
set -euxo pipefail
1010
cargo fmt --all -- --check
11-
cargo clippy -- --deny=warnings
11+
cargo clippy --features std_web -- --deny=warnings
12+
cargo clippy --target wasm32-unknown-unknown --features web_sys -- --deny=warnings

ci/run_tests.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ set -euxo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_
55

66
if [ "$emscripten_supported" == "0" ]; then
77
# TODO - Emscripten builds are broken on rustc > 1.39.0
8-
cargo web test --features web_test --target asmjs-unknown-emscripten
9-
cargo web test --features web_test --target wasm32-unknown-emscripten
8+
cargo web test --target asmjs-unknown-emscripten --features std_web
9+
cargo web test --target wasm32-unknown-emscripten --features std_web
1010
fi
1111

12-
cargo test --features wasm_test --target wasm32-unknown-unknown
13-
cargo test --test macro_test
14-
cargo test --test derive_props_test
15-
cargo test --doc --all-features
16-
(cd crates/macro && cargo test --doc)
12+
cargo test --target wasm32-unknown-unknown --features wasm_test,std_web
13+
cargo test --target wasm32-unknown-unknown --features wasm_test,web_sys
14+
cargo test --doc --features doc_test,wasm_test,yaml,msgpack,cbor,std_web
15+
cargo test --doc --features doc_test,wasm_test,yaml,msgpack,cbor,web_sys
16+
17+
(cd crates/macro \
18+
&& cargo test --test macro_test \
19+
&& cargo test --test derive_props_test \
20+
&& cargo test --doc)

crates/macro/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ proc-macro2 = "1.0"
2525
quote = "1.0"
2626
syn = { version = "1.0", features = ["full", "extra-traits"] }
2727

28+
# testing
2829
[dev-dependencies]
29-
yew = { path = "../.." }
30+
rustversion = "1.0"
31+
trybuild = "1.0"
32+
yew = { path = "../..", features = ["std_web"] }
3033

3134
[build-dependencies]
3235

File renamed without changes.

tests/derive_props/fail.stderr renamed to crates/macro/tests/derive_props/fail.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ error[E0599]: no method named `build` found for type `t3::PropsBuilder<t3::Props
5151
...
5252
35 | Props::builder().build();
5353
| ^^^^^ method not found in `t3::PropsBuilder<t3::PropsBuilderStep_missing_required_prop_value>`
54+
|
55+
= help: items from traits can only be used if the trait is implemented and in scope
56+
= note: the following trait defines an item `build`, perhaps you need to implement it:
57+
candidate #1: `proc_macro::bridge::server::TokenStreamBuilder`
5458

5559
error[E0599]: no method named `b` found for type `t4::PropsBuilder<t4::PropsBuilderStep_missing_required_prop_a>` in the current scope
5660
--> $DIR/fail.rs:49:26
File renamed without changes.

tests/derive_props_test.rs renamed to crates/macro/tests/derive_props_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[allow(dead_code)]
2-
#[rustversion::attr(stable(1.41.0), cfg_attr(not(feature = "web_test"), test))]
2+
#[rustversion::attr(stable(1.41.0), test)]
33
fn tests() {
44
let t = trybuild::TestCases::new();
55
t.pass("tests/derive_props/pass.rs");

0 commit comments

Comments
 (0)