Skip to content

Avoid using browser-specific features when building for WASM #8520

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ default = [
"tonemapping_luts",
"default_font",
"webgl2",
"browser",
]

# Force dynamic linking, which improves iterative compile times
Expand Down Expand Up @@ -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 }
Expand Down
7 changes: 3 additions & 4 deletions crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
8 changes: 4 additions & 4 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 {
Expand All @@ -123,7 +123,7 @@ impl Plugin for ScheduleRunnerPlugin {
}
}

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", feature = "browser"))]
{
fn set_timeout(f: &Closure<dyn FnMut()>, dur: Duration) {
web_sys::window()
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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 }
7 changes: 5 additions & 2 deletions crates/bevy_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
12 changes: 6 additions & 6 deletions crates/bevy_tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ 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;
#[cfg(not(target_arch = "wasm32"))]
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;
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["bevy"]

[features]
detailed_trace = []
browser = ["dep:getrandom"]

[dependencies]
ahash = "0.8.3"
Expand All @@ -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 }
1 change: 1 addition & 0 deletions docs/cargo_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down