Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
- This depends on an unstable flag in the Rust compiler, and thus requires a nightly toolchain of Rust
- Bevy doesn't natively utilize multi-threaded WASM, so this will only benefit plugins that support it or code you write yourself
- `bevy build web` now supports a `--skip-post-processing` flag for skipping post-build binding, optimization or bundling steps. The option can also be enabled by setting the `BEVY_WEB_SKIP_POST_PROCESSING` environment variable to `true`.
- `bevy build web` now has an option to copy the packed bundle to a specific path using `--bundle-dir`

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/src/cli/web.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You can view the [default `index.html` here](web/default-index-html.md).
To deploy your app on a web server, it's often necessary to bundle the binary, assets and web files into a single folder.
Using `bevy build web --bundle`, the CLI can create this bundle for you automatically.
It will be available in the `target/bevy_web` folder, see the command's output for the full file path.
You can also use `bevy build web --bundle --bundle-dir <path>` to copy the bundle directory to `<path>`.

## Compilation profiles

Expand Down
8 changes: 8 additions & 0 deletions src/commands/build/args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "web")]
use std::path::PathBuf;

#[cfg(feature = "web")]
use clap::ArgAction;
use clap::{Args, Subcommand};
Expand Down Expand Up @@ -160,6 +163,11 @@ pub struct BuildWebArgs {
#[arg(long = "wasm-opt", allow_hyphen_values = true)]
pub wasm_opt: Vec<String>,

/// The directory to copy final packed bundle to. Note that a copy of the Bundle can still be
/// found at `target/bevy_web`.
#[arg(long = "bundle-dir", requires = "create_packed_bundle")]
pub bundle_dir: Option<PathBuf>,

#[cfg(feature = "unstable")]
#[clap(flatten)]
pub unstable: UnstableWebArgs,
Expand Down
1 change: 1 addition & 0 deletions src/commands/run/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl From<RunArgs> for BuildArgs {
skip_post_processing: false,
#[cfg(feature = "unstable")]
unstable: web_args.unstable,
bundle_dir: None,
}),
}),
}
Expand Down
15 changes: 14 additions & 1 deletion src/web/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::fs;

use anyhow::Context as _;
use cargo_metadata::Metadata;
use fs_extra::dir::{self, CopyOptions};
use tracing::info;

use super::bundle::WebBundle;
use crate::{
bin_target::select_run_binary,
commands::build::{BuildArgs, BuildSubcommands},
commands::build::{BuildArgs, BuildSubcommands, BuildWebArgs},
external_cli::{cargo, wasm_bindgen, wasm_opt},
web::{
bundle::{PackedBundle, create_web_bundle},
Expand Down Expand Up @@ -91,6 +94,16 @@ pub fn build_web(args: &mut BuildArgs, metadata: &Metadata) -> anyhow::Result<We

if let WebBundle::Packed(PackedBundle { path }) = &web_bundle {
info!("created bundle at file://{}", path.display());
if let Some(BuildWebArgs {
bundle_dir: Some(target),
..
}) = web_args
{
fs::create_dir_all(target).context("failed to create target directory")?;
dir::copy(path, target, &CopyOptions::new().content_only(true))
.context("failed to copy packed bundle directory to target directory")?;
info!("copied bundle to file://{}", target.display());
}
}
Comment thread
musjj marked this conversation as resolved.

Ok(web_bundle)
Expand Down
31 changes: 30 additions & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use anyhow::Context;
use assert_cmd::cargo::cargo_bin_cmd;
use assert_cmd::{Command, cargo::cargo_bin_cmd};
use serial_test::serial;

/// The path to the test repository.
Expand Down Expand Up @@ -125,3 +125,32 @@ fn should_build_web_release() -> anyhow::Result<()> {
ensure_path_exists(target_artifact_path.join("bevy_default.js"))
.context("JS bindings do not exist")
}

#[test]
#[serial]
fn should_copy_web_bundle() -> anyhow::Result<()> {
let target_artifact_path = target_path()
.join("wasm32-unknown-unknown")
.join("web-release");
clean_target_artifacts(&target_artifact_path)?;

let _ = fs::remove_dir_all(test_path().join("web-dir"));
let mut cmd = Command::cargo_bin("bevy")?;
cmd.current_dir(test_path()).args([
"build",
"-p=bevy_default",
"--yes",
"web",
"--bundle",
"--bundle-dir=web-dir",
]);

cmd.assert().success();

ensure_path_exists(test_path().join("web-dir/index.html"))
.context("index.html do not exist")?;
ensure_path_exists(test_path().join("web-dir/build/bevy_default_bg.wasm"))
.context("Wasm bindings do not exist")?;
ensure_path_exists(test_path().join("web-dir/build/bevy_default.js"))
.context("JS bindings do not exist")
}