From ce6b483c9efd3a253b35e435e77552a4e698e474 Mon Sep 17 00:00:00 2001 From: musjj <72612857+musjj@users.noreply.github.com> Date: Sat, 18 Oct 2025 18:59:42 +0700 Subject: [PATCH] add --bundle-dir option to web subcommand --- src/commands/build/args.rs | 7 +++++++ src/commands/run/args.rs | 9 +++++++++ src/web/build.rs | 14 +++++++++++++- tests/build.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/commands/build/args.rs b/src/commands/build/args.rs index 4d318373..c30aca72 100644 --- a/src/commands/build/args.rs +++ b/src/commands/build/args.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "web")] +use std::path::PathBuf; + #[cfg(feature = "web")] use clap::ArgAction; use clap::{Args, Subcommand}; @@ -156,6 +159,10 @@ pub struct BuildWebArgs { #[arg(long = "wasm-opt", allow_hyphen_values = true)] pub wasm_opt: Vec, + /// Copy packed bundle directory to this directory + #[arg(long = "bundle-dir", allow_hyphen_values = true)] + pub bundle_dir: Option, + #[cfg(feature = "unstable")] #[clap(flatten)] pub unstable: UnstableWebArgs, diff --git a/src/commands/run/args.rs b/src/commands/run/args.rs index 6b62b1f5..4cefa74a 100644 --- a/src/commands/run/args.rs +++ b/src/commands/run/args.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "web")] +use std::path::PathBuf; + #[cfg(feature = "web")] use clap::ArgAction; use clap::{Args, Subcommand}; @@ -172,6 +175,10 @@ pub struct RunWebArgs { #[arg(long = "wasm-opt", allow_hyphen_values = true)] pub wasm_opt: Vec, + /// Copy packed bundle directory to this directory + #[arg(long = "bundle-dir", allow_hyphen_values = true)] + pub bundle_dir: Option, + #[cfg(feature = "unstable")] #[clap(flatten)] pub unstable: UnstableWebArgs, @@ -187,6 +194,7 @@ impl Default for RunWebArgs { create_packed_bundle: false, headers: Vec::new(), wasm_opt: Vec::new(), + bundle_dir: None, #[cfg(feature = "unstable")] unstable: UnstableWebArgs::default(), } @@ -227,6 +235,7 @@ impl From for BuildArgs { wasm_opt: web_args.wasm_opt, #[cfg(feature = "unstable")] unstable: web_args.unstable, + bundle_dir: web_args.bundle_dir, }), }), } diff --git a/src/web/build.rs b/src/web/build.rs index 7b48d216..1a3bcb79 100644 --- a/src/web/build.rs +++ b/src/web/build.rs @@ -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::BinTarget, - commands::build::{BuildArgs, BuildSubcommands}, + commands::build::{BuildArgs, BuildSubcommands, BuildWebArgs}, external_cli::{cargo, wasm_bindgen, wasm_opt}, web::{ bundle::{PackedBundle, create_web_bundle}, @@ -82,6 +85,15 @@ pub fn build_web( 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")?; + } } Ok(web_bundle) diff --git a/tests/build.rs b/tests/build.rs index 42b88362..94df54b6 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -126,3 +126,33 @@ 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", + "--release", + "--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") +}