diff --git a/Cargo.toml b/Cargo.toml index 70817549915b7..930661f06921e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,7 @@ default = [ "tonemapping_luts", "default_font", "webgl2", + "browser", ] # Force dynamic linking, which improves iterative compile times @@ -244,6 +245,9 @@ shader_format_spirv = ["bevy_internal/shader_format_spirv"] # Enable some limitations to be able to use WebGL2. If not enabled, it will default to WebGPU in Wasm webgl2 = ["bevy_internal/webgl"] +# When compiling for WebAssembly, enable support for web features +browser = ["bevy_internal/browser"] + [dependencies] bevy_dylib = { path = "crates/bevy_dylib", version = "0.11.0-dev", default-features = false, optional = true } bevy_internal = { path = "crates/bevy_internal", version = "0.11.0-dev", default-features = false } diff --git a/crates/bevy_app/Cargo.toml b/crates/bevy_app/Cargo.toml index 2a3f604e7b05f..ec817a3a8efec 100644 --- a/crates/bevy_app/Cargo.toml +++ b/crates/bevy_app/Cargo.toml @@ -13,6 +13,7 @@ trace = [] bevy_ci_testing = ["serde", "ron"] default = ["bevy_reflect"] bevy_reflect = ["dep:bevy_reflect", "bevy_ecs/bevy_reflect"] +browser = ["dep:wasm-bindgen", "dep:web-sys"] [dependencies] # bevy @@ -26,8 +27,6 @@ serde = { version = "1.0", features = ["derive"], optional = true } ron = { version = "0.8.0", optional = true } downcast-rs = "1.2.0" - [target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = { version = "0.2" } -web-sys = { version = "0.3", features = [ "Window" ] } - +wasm-bindgen = { version = "0.2", optional = true } +web-sys = { version = "0.3", features = [ "Window" ], optional = true } diff --git a/crates/bevy_app/src/schedule_runner.rs b/crates/bevy_app/src/schedule_runner.rs index c143f75c42f53..ddc656824fda0 100644 --- a/crates/bevy_app/src/schedule_runner.rs +++ b/crates/bevy_app/src/schedule_runner.rs @@ -5,9 +5,9 @@ use crate::{ use bevy_ecs::event::{Events, ManualEventReader}; use bevy_utils::{Duration, Instant}; -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", feature = "browser"))] use std::{cell::RefCell, rc::Rc}; -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", feature = "browser"))] use wasm_bindgen::{prelude::*, JsCast}; /// Determines the method used to run an [`App`]'s [`Schedule`](bevy_ecs::schedule::Schedule). @@ -114,7 +114,7 @@ impl Plugin for ScheduleRunnerPlugin { Ok(None) }; - #[cfg(not(target_arch = "wasm32"))] + #[cfg(any(not(target_arch = "wasm32"), not(feature = "browser")))] { while let Ok(delay) = tick(&mut app, wait) { if let Some(delay) = delay { @@ -123,7 +123,7 @@ impl Plugin for ScheduleRunnerPlugin { } } - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", feature = "browser"))] { fn set_timeout(f: &Closure, dur: Duration) { web_sys::window() diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index 21e305cf04c4d..52fcf95363c31 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -12,6 +12,7 @@ keywords = ["bevy"] default = [] filesystem_watcher = ["notify"] debug_asset_server = ["filesystem_watcher"] +browser = ["dep:wasm-bindgen", "dep:web-sys", "dep:wasm-bindgen-futures", "dep:js-sys"] [dependencies] # bevy @@ -37,10 +38,10 @@ parking_lot = "0.12.1" bevy_winit = { path = "../bevy_winit", version = "0.11.0-dev" } [target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = { version = "0.2" } -web-sys = { version = "0.3", features = ["Request", "Window", "Response"] } -wasm-bindgen-futures = "0.4" -js-sys = "0.3" +wasm-bindgen = { version = "0.2", optional = true } +web-sys = { version = "0.3", features = ["Request", "Window", "Response"], optional = true } +wasm-bindgen-futures = { version = "0.4", optional = true } +js-sys = { version = "0.3", optional = true } [dev-dependencies] futures-lite = "1.4.0" diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index d3e9a532a44cf..8378bb814231c 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -77,6 +77,9 @@ subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"] # Optimise for WebGL2 webgl = ["bevy_core_pipeline?/webgl", "bevy_pbr?/webgl", "bevy_render?/webgl"] +# When compiling for WebAssembly, enable support for web features +browser = ["bevy_app/browser", "bevy_asset?/browser", "bevy_tasks/browser", "bevy_log/browser", "bevy_utils/browser"] + # enable systems that allow for automated testing on CI bevy_ci_testing = ["bevy_app/bevy_ci_testing", "bevy_time/bevy_ci_testing", "bevy_render?/bevy_ci_testing", "bevy_render?/ci_limits"] diff --git a/crates/bevy_log/Cargo.toml b/crates/bevy_log/Cargo.toml index 3e8700b813299..0f9026443f5d6 100644 --- a/crates/bevy_log/Cargo.toml +++ b/crates/bevy_log/Cargo.toml @@ -11,6 +11,7 @@ keywords = ["bevy"] [features] trace = [ "tracing-error" ] trace_tracy_memory = ["dep:tracy-client"] +browser = ["dep:console_error_panic_hook", "dep:tracing-wasm"] [dependencies] bevy_app = { path = "../bevy_app", version = "0.11.0-dev" } @@ -28,5 +29,5 @@ tracy-client = { version = "0.15", optional = true } android_log-sys = "0.2.0" [target.'cfg(target_arch = "wasm32")'.dependencies] -console_error_panic_hook = "0.1.6" -tracing-wasm = "0.2.1" +console_error_panic_hook = { version = "0.1.6", optional = true } +tracing-wasm = { version = "0.2.1", optional = true } diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index 8f8e841d30dba..9af76acd91f9e 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -130,7 +130,10 @@ impl Plugin for LogPlugin { #[cfg(feature = "trace")] let subscriber = subscriber.with(tracing_error::ErrorLayer::default()); - #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] + #[cfg(all( + any(not(target_arch = "wasm32"), not(feature = "browser")), + not(target_os = "android") + ))] { #[cfg(feature = "tracing-chrome")] let chrome_layer = { @@ -179,7 +182,7 @@ impl Plugin for LogPlugin { finished_subscriber = subscriber; } - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", feature = "browser"))] { console_error_panic_hook::set_once(); finished_subscriber = subscriber.with(tracing_wasm::WASMLayer::new( diff --git a/crates/bevy_tasks/Cargo.toml b/crates/bevy_tasks/Cargo.toml index da5156be0463c..77890b52fd66a 100644 --- a/crates/bevy_tasks/Cargo.toml +++ b/crates/bevy_tasks/Cargo.toml @@ -8,6 +8,10 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT OR Apache-2.0" keywords = ["bevy"] +[features] +default = [] +browser = ["dep:wasm-bindgen-futures"] + [dependencies] futures-lite = "1.4.0" async-executor = "1.3.0" @@ -17,7 +21,7 @@ once_cell = "1.7" concurrent-queue = "2.0.0" [target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen-futures = "0.4" +wasm-bindgen-futures = { version = "0.4", optional = true } [dev-dependencies] instant = { version = "0.1", features = ["wasm-bindgen"] } diff --git a/crates/bevy_tasks/src/lib.rs b/crates/bevy_tasks/src/lib.rs index 538222883071f..9c6e56f98d1a9 100644 --- a/crates/bevy_tasks/src/lib.rs +++ b/crates/bevy_tasks/src/lib.rs @@ -8,14 +8,14 @@ pub use slice::{ParallelSlice, ParallelSliceMut}; mod task; pub use task::Task; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), not(feature = "browser")))] mod task_pool; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), not(feature = "browser")))] pub use task_pool::{Scope, TaskPool, TaskPoolBuilder}; -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", feature = "browser"))] mod single_threaded_task_pool; -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", feature = "browser"))] pub use single_threaded_task_pool::{Scope, TaskPool, TaskPoolBuilder, ThreadExecutor}; mod usages; @@ -23,9 +23,9 @@ mod usages; pub use usages::tick_global_task_pools_on_main_thread; pub use usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool}; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), not(feature = "browser")))] mod thread_executor; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), not(feature = "browser")))] pub use thread_executor::{ThreadExecutor, ThreadExecutorTicker}; mod iter; diff --git a/crates/bevy_tasks/src/task_pool.rs b/crates/bevy_tasks/src/task_pool.rs index 5903e67ea1205..f2a04b2c4882a 100644 --- a/crates/bevy_tasks/src/task_pool.rs +++ b/crates/bevy_tasks/src/task_pool.rs @@ -130,9 +130,12 @@ impl TaskPool { let executor = Arc::new(async_executor::Executor::new()); + #[cfg(not(target_arch = "wasm32"))] let num_threads = builder .num_threads .unwrap_or_else(crate::available_parallelism); + #[cfg(target_arch = "wasm32")] + let num_threads = 0; let threads = (0..num_threads) .map(|i| { diff --git a/crates/bevy_utils/Cargo.toml b/crates/bevy_utils/Cargo.toml index cc97bb332bda6..efda7a4ee93fb 100644 --- a/crates/bevy_utils/Cargo.toml +++ b/crates/bevy_utils/Cargo.toml @@ -10,6 +10,7 @@ keywords = ["bevy"] [features] detailed_trace = [] +browser = ["dep:getrandom"] [dependencies] ahash = "0.8.3" @@ -22,4 +23,4 @@ petgraph = "0.6" thiserror = "1.0" [target.'cfg(target_arch = "wasm32")'.dependencies] -getrandom = {version = "0.2.0", features = ["js"]} +getrandom = { version = "0.2.0", features = ["js"], optional = true } diff --git a/docs/cargo_features.md b/docs/cargo_features.md index bf6a5e55fc643..a33d3053eff66 100644 --- a/docs/cargo_features.md +++ b/docs/cargo_features.md @@ -27,6 +27,7 @@ The default feature set enables most of the expected features of a game engine, |bevy_text|Provides text functionality| |bevy_ui|A custom ECS-driven UI framework| |bevy_winit|winit window and input backend| +|browser|When compiling for WebAssembly, enable support for web features| |default_font|Include a default font, containing only ASCII characters, at the cost of a 20kB binary size increase| |filesystem_watcher|Enable watching file system for asset hot reload| |hdr|HDR image format support|