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
7 changes: 7 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 @@ -156,6 +159,10 @@ pub struct BuildWebArgs {
#[arg(long = "wasm-opt", allow_hyphen_values = true)]
pub wasm_opt: Vec<String>,

/// Copy packed bundle directory to this directory
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Copy packed bundle directory to this directory
/// 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", allow_hyphen_values = true)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: While technically directories could start with a - I think we can avoid problems by not allowing that, what do you think?

Suggested change
#[arg(long = "bundle-dir", allow_hyphen_values = true)]
#[arg(long = "bundle-dir")]

pub bundle_dir: Option<PathBuf>,

#[cfg(feature = "unstable")]
#[clap(flatten)]
pub unstable: UnstableWebArgs,
Expand Down
9 changes: 9 additions & 0 deletions src/commands/run/args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "web")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to support this for cargo run?

use std::path::PathBuf;

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

/// Copy packed bundle directory to this directory
#[arg(long = "bundle-dir", allow_hyphen_values = true)]
pub bundle_dir: Option<PathBuf>,

#[cfg(feature = "unstable")]
#[clap(flatten)]
pub unstable: UnstableWebArgs,
Expand All @@ -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(),
}
Expand Down Expand Up @@ -227,6 +235,7 @@ impl From<RunArgs> for BuildArgs {
wasm_opt: web_args.wasm_opt,
#[cfg(feature = "unstable")]
unstable: web_args.unstable,
bundle_dir: web_args.bundle_dir,
}),
}),
}
Expand Down
14 changes: 13 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::BinTarget,
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 @@ -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")?;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would like an additional log line that explains that the cli copied the files to the new dir.

}

Ok(web_bundle)
Expand Down
30 changes: 30 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We could save CI time by not using the release profile here.

"--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")
}