diff --git a/packages/cli/Cargo.toml b/packages/cli/Cargo.toml index 87301100c..3d38bfc01 100644 --- a/packages/cli/Cargo.toml +++ b/packages/cli/Cargo.toml @@ -81,7 +81,7 @@ toml_edit = "0.19.11" tauri-bundler = { version = "1.2", features = ["native-tls-vendored"] } tauri-utils = "1.3" -assets-cli-support = { git = "https://github.com/DioxusLabs/collect-assets" } +assets-cli-support = { git = "https://github.com/DioxusLabs/collect-assets", features = ["webp", "html"] } dioxus-autofmt = { workspace = true } dioxus-check = { workspace = true } diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index ee5e5ff89..ddfa95dfe 100644 --- a/packages/cli/src/builder.rs +++ b/packages/cli/src/builder.rs @@ -464,7 +464,8 @@ pub fn gen_page(config: &CrateConfig, serve: bool) -> String { &style.to_str().unwrap(), )) } - if config.dioxus_config + if config + .dioxus_config .application .tools .clone() @@ -474,25 +475,7 @@ pub fn gen_page(config: &CrateConfig, serve: bool) -> String { style_str.push_str("\n"); } let manifest = config.asset_manifest(); - for package in manifest.packages() { - for asset in package.assets(){ - if let assets_cli_support::AssetType::File(file) = asset { - match file.options(){ - assets_cli_support::FileOptions::Css(_) => { - let asset_path = file.served_location(); - style_str.push_str(&format!("\n")) - } - assets_cli_support::FileOptions::Image(image_options) => { - if image_options.preload(){ - let asset_path = file.served_location(); - style_str.push_str(&format!("\n")) - } - } - _ => {} - } - } - } - } + style_str.push_str(&manifest.head()); replace_or_insert_before("{style_include}", &style_str, " String { } let title = config - .dioxus_config.web + .dioxus_config + .web .app .title .clone() @@ -710,8 +694,9 @@ fn build_assets(config: &CrateConfig) -> Result> { // SASS END // Set up the collect asset config - let config = assets_cli_support::Config::default().with_assets_serve_location("/"); - config.save(); + assets_cli_support::Config::default() + .with_assets_serve_location("/") + .save(); Ok(result) } @@ -733,6 +718,9 @@ fn process_assets(config: &CrateConfig) -> anyhow::Result<()> { manifest.copy_static_assets_to(&static_asset_output_dir)?; + // Reset the config + assets_cli_support::Config::default().save(); + Ok(()) } diff --git a/packages/cli/src/config.rs b/packages/cli/src/config.rs index a93570803..b5345a8a4 100644 --- a/packages/cli/src/config.rs +++ b/packages/cli/src/config.rs @@ -1,4 +1,5 @@ -use assets_cli_support::AssetManifest;use crate::{cfg::Platform, error::Result}; +use crate::{cfg::Platform, error::Result}; +use assets_cli_support::AssetManifest; use assets_cli_support::AssetManifestExt; use serde::{Deserialize, Serialize}; use std::{ diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index a85c2e045..6254c7ff8 100644 --- a/packages/desktop/Cargo.toml +++ b/packages/desktop/Cargo.toml @@ -38,6 +38,7 @@ futures-util = { workspace = true } urlencoding = "2.1.2" async-trait = "0.1.68" +assets-cli-support = { git = "https://github.com/DioxusLabs/collect-assets", features = ["webp", "html"] } [target.'cfg(any(target_os = "windows",target_os = "macos",target_os = "linux",target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies] rfd = "0.11.3" diff --git a/packages/desktop/src/collect_assets.rs b/packages/desktop/src/collect_assets.rs new file mode 100644 index 000000000..65b36fede --- /dev/null +++ b/packages/desktop/src/collect_assets.rs @@ -0,0 +1,31 @@ +pub fn copy_assets() { + #[cfg(debug_assertions)] + { + use assets_cli_support::AssetManifest; + use assets_cli_support::AssetManifestExt; + use assets_cli_support::Config; + use std::path::PathBuf; + let config = Config::current(); + let asset_location = config.assets_serve_location(); + let asset_location = PathBuf::from(asset_location); + let _ = std::fs::remove_dir_all(&asset_location); + + let manifest = AssetManifest::load(); + let has_assets = manifest + .packages() + .iter() + .any(|package| !package.assets().is_empty()); + + if has_assets { + println!("Copying and optimizing assets..."); + manifest.copy_static_assets_to(&asset_location).unwrap(); + println!("Copied assets to {}", asset_location.display()); + } + } + #[cfg(not(debug_assertions))] + { + println!( + "Skipping assets in release mode. You compile assets with the dioxus-cli in release mode" + ); + } +} diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index 070bfa7d1..3fd1d6ed2 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -4,6 +4,7 @@ #![deny(missing_docs)] mod cfg; +mod collect_assets; mod desktop_context; mod element; mod escape; @@ -136,6 +137,9 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) } }); + // Copy over any assets we find + crate::collect_assets::copy_assets(); + // We start the tokio runtime *on this thread* // Any future we poll later will use this runtime to spawn tasks and for IO let rt = tokio::runtime::Builder::new_multi_thread() diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index 53652f82f..4046ee42f 100644 --- a/packages/desktop/src/protocol.rs +++ b/packages/desktop/src/protocol.rs @@ -51,10 +51,12 @@ fn module_loader(root_name: &str) -> String { ) } +#[allow(unused_variables)] pub(super) fn desktop_handler( request: &Request>, custom_head: Option, custom_index: Option, + assets_head: Option, root_name: &str, ) -> Result>> { // If the request is for the root, we'll serve the index.html file. @@ -70,9 +72,24 @@ pub(super) fn desktop_handler( // Otherwise, we'll serve the default index.html and apply a custom head if that's specified. let mut template = include_str!("./index.html").to_string(); - if let Some(custom_head) = custom_head { - template = template.replace("", &custom_head); + #[allow(unused_mut)] + let mut head = custom_head.unwrap_or_default(); + #[cfg(debug_assertions)] + { + use assets_cli_support::AssetManifestExt; + let manifest = assets_cli_support::AssetManifest::load(); + head += &manifest.head(); } + #[cfg(not(debug_assertions))] + { + if let Some(assets_head) = assets_head { + head += &assets_head; + } else { + log::warn!("No assets head found. You can compile assets with the dioxus-cli in release mode"); + } + } + + template = template.replace("", &head); template .replace("", &module_loader(root_name)) diff --git a/packages/desktop/src/webview.rs b/packages/desktop/src/webview.rs index 469aa8068..49475e903 100644 --- a/packages/desktop/src/webview.rs +++ b/packages/desktop/src/webview.rs @@ -18,6 +18,16 @@ pub fn build( let custom_head = cfg.custom_head.clone(); let index_file = cfg.custom_index.clone(); let root_name = cfg.root_name.clone(); + let assets_head = { + #[cfg(debug_assertions)] + { + None + } + #[cfg(not(debug_assertions))] + { + std::fs::read_to_string("public/assets_head.html").ok() + } + }; // We assume that if the icon is None in cfg, then the user just didnt set it if cfg.window.window.window_icon.is_none() { @@ -45,7 +55,13 @@ pub fn build( } }) .with_custom_protocol(String::from("dioxus"), move |r| { - protocol::desktop_handler(r, custom_head.clone(), index_file.clone(), &root_name) + protocol::desktop_handler( + r, + custom_head.clone(), + index_file.clone(), + assets_head.clone(), + &root_name, + ) }) .with_file_drop_handler(move |window, evet| { file_handler