Skip to content

Commit

Permalink
Add bundle option to build web command
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Dec 23, 2024
1 parent 953e833 commit 70b55fc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/build/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Args, Subcommand};
use clap::{ArgAction, Args, Subcommand};

use crate::external_cli::{arg_builder::ArgBuilder, cargo::build::CargoBuildArgs};

Expand All @@ -16,7 +16,7 @@ pub struct BuildArgs {
impl BuildArgs {
/// Determine if the app is being built for the web.
pub(crate) fn is_web(&self) -> bool {
matches!(self.subcommand, Some(BuildSubcommands::Web))
matches!(self.subcommand, Some(BuildSubcommands::Web(_)))
}

/// The profile used to compile the app.
Expand All @@ -38,5 +38,12 @@ impl BuildArgs {
#[derive(Debug, Subcommand)]
pub enum BuildSubcommands {
/// Build your app for the browser.
Web,
Web(BuildWebArgs),
}

#[derive(Debug, Args)]
pub struct BuildWebArgs {
// Bundle all web artifacts into a single folder.
#[arg(short = 'b', long = "bundle", action = ArgAction::SetTrue, default_value_t = false)]
pub create_packed_bundle: bool,
}
13 changes: 12 additions & 1 deletion src/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use args::BuildSubcommands;

use crate::{
external_cli::{cargo, rustup, wasm_bindgen, CommandHelpers},
run::select_run_binary,
web::bundle::{create_web_bundle, PackedBundle, WebBundle},
};

pub use self::args::BuildArgs;
Expand All @@ -10,7 +13,7 @@ mod args;
pub fn build(args: &BuildArgs) -> anyhow::Result<()> {
let cargo_args = args.cargo_args_builder();

if args.is_web() {
if let Some(BuildSubcommands::Web(web_args)) = &args.subcommand {
ensure_web_setup()?;

let metadata = cargo::metadata::metadata_with_args(["--no-deps"])?;
Expand All @@ -28,6 +31,14 @@ pub fn build(args: &BuildArgs) -> anyhow::Result<()> {
args.profile(),
)?;
wasm_bindgen::bundle(&bin_target)?;

if web_args.create_packed_bundle {
let web_bundle = create_web_bundle(&metadata, args.profile(), bin_target, true)?;

if let WebBundle::Packed(PackedBundle { path }) = &web_bundle {
println!("Created bundle at file://{}", path.display());
}
}
} else {
cargo::build::command().args(cargo_args).ensure_status()?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/run/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ pub struct RunWebArgs {

// Bundle all web artifacts into a single folder.
#[arg(short = 'b', long = "bundle", action = ArgAction::SetTrue, default_value_t = false)]
pub create_bundle: bool,
pub create_packed_bundle: bool,
}
8 changes: 6 additions & 2 deletions src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
cargo::{self, metadata::Metadata},
wasm_bindgen, CommandHelpers,
},
web::bundle::create_web_bundle,
web::bundle::{create_web_bundle, PackedBundle, WebBundle},
};

pub use self::args::RunArgs;
Expand Down Expand Up @@ -44,10 +44,14 @@ pub fn run(args: &RunArgs) -> anyhow::Result<()> {
&metadata,
args.profile(),
bin_target,
web_args.create_bundle,
web_args.create_packed_bundle,
)
.context("Failed to create web bundle")?;

if let WebBundle::Packed(PackedBundle { path }) = &web_bundle {
println!("Created bundle at file://{}", path.display());
}

let port = web_args.port;
let url = format!("http://localhost:{port}");

Expand Down

0 comments on commit 70b55fc

Please sign in to comment.