collect assets in desktop debug mode

This commit is contained in:
Evan Almloff committed 2023-08-16 17:01:51 -05:00
1 parent 8d3c11ef15
commit c402b6cd36
8 files changed
+86 -28

No files matched your search

+31
View File
@@ -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"
);
}
}
+4
View File
@@ -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<P: 'static>(root: Component<P>, 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()
+19 -2
View File
@@ -51,10 +51,12 @@ fn module_loader(root_name: &str) -> String {
)
}
#[allow(unused_variables)]
pub(super) fn desktop_handler(
request: &Request<Vec<u8>>,
custom_head: Option<String>,
custom_index: Option<String>,
assets_head: Option<String>,
root_name: &str,
) -> Result<Response<Cow<'static, [u8]>>> {
// 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 -->", &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("<!-- CUSTOM HEAD -->", &head);
template
.replace("<!-- MODULE LOADER -->", &module_loader(root_name))
+17 -1
View File
@@ -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