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

+1 -1
View File
@@ -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 }
+11 -23
View File
@@ -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("<link rel=\"stylesheet\" href=\"tailwind.css\">\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!("<link rel=\"stylesheet\" href=\"{asset_path}\">\n"))
}
assets_cli_support::FileOptions::Image(image_options) => {
if image_options.preload(){
let asset_path = file.served_location();
style_str.push_str(&format!("<link rel=\"preload\" as=\"image\" href=\"{asset_path}\">\n"))
}
}
_ => {}
}
}
}
}
style_str.push_str(&manifest.head());
replace_or_insert_before("{style_include}", &style_str, "</head", &mut html);
@@ -542,7 +525,8 @@ pub fn gen_page(config: &CrateConfig, serve: bool) -> String {
}
let title = config
.dioxus_config.web
.dioxus_config
.web
.app
.title
.clone()
@@ -710,8 +694,9 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
// 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(())
}
+2 -1
View File
@@ -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::{
+1
View File
@@ -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"
+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