Przeglądaj źródła

collect assets in desktop debug mode

Evan Almloff 1 rok temu
rodzic
commit
c402b6cd36

+ 1 - 1
packages/cli/Cargo.toml

@@ -81,7 +81,7 @@ toml_edit = "0.19.11"
 tauri-bundler = { version = "1.2", features = ["native-tls-vendored"] }
 tauri-bundler = { version = "1.2", features = ["native-tls-vendored"] }
 tauri-utils = "1.3"
 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-autofmt = { workspace = true }
 dioxus-check = { workspace = true }
 dioxus-check = { workspace = true }

+ 11 - 23
packages/cli/src/builder.rs

@@ -464,7 +464,8 @@ pub fn gen_page(config: &CrateConfig, serve: bool) -> String {
             &style.to_str().unwrap(),
             &style.to_str().unwrap(),
         ))
         ))
     }
     }
-    if config.dioxus_config
+    if config
+        .dioxus_config
         .application
         .application
         .tools
         .tools
         .clone()
         .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");
         style_str.push_str("<link rel=\"stylesheet\" href=\"tailwind.css\">\n");
     }
     }
     let manifest = config.asset_manifest();
     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);
     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
     let title = config
-    .dioxus_config.web
+        .dioxus_config
+        .web
         .app
         .app
         .title
         .title
         .clone()
         .clone()
@@ -710,8 +694,9 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
     // SASS END
     // SASS END
 
 
     // Set up the collect asset config
     // 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)
     Ok(result)
 }
 }
@@ -733,6 +718,9 @@ fn process_assets(config: &CrateConfig) -> anyhow::Result<()> {
 
 
     manifest.copy_static_assets_to(&static_asset_output_dir)?;
     manifest.copy_static_assets_to(&static_asset_output_dir)?;
 
 
+    // Reset the config
+    assets_cli_support::Config::default().save();
+
     Ok(())
     Ok(())
 }
 }
 
 

+ 2 - 1
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 assets_cli_support::AssetManifestExt;
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
 use std::{
 use std::{

+ 1 - 0
packages/desktop/Cargo.toml

@@ -38,6 +38,7 @@ futures-util = { workspace = true }
 urlencoding = "2.1.2"
 urlencoding = "2.1.2"
 async-trait = "0.1.68"
 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]
 [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"
 rfd = "0.11.3"

+ 31 - 0
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"
+        );
+    }
+}

+ 4 - 0
packages/desktop/src/lib.rs

@@ -4,6 +4,7 @@
 #![deny(missing_docs)]
 #![deny(missing_docs)]
 
 
 mod cfg;
 mod cfg;
+mod collect_assets;
 mod desktop_context;
 mod desktop_context;
 mod element;
 mod element;
 mod escape;
 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*
     // We start the tokio runtime *on this thread*
     // Any future we poll later will use this runtime to spawn tasks and for IO
     // Any future we poll later will use this runtime to spawn tasks and for IO
     let rt = tokio::runtime::Builder::new_multi_thread()
     let rt = tokio::runtime::Builder::new_multi_thread()

+ 19 - 2
packages/desktop/src/protocol.rs

@@ -51,10 +51,12 @@ fn module_loader(root_name: &str) -> String {
     )
     )
 }
 }
 
 
+#[allow(unused_variables)]
 pub(super) fn desktop_handler(
 pub(super) fn desktop_handler(
     request: &Request<Vec<u8>>,
     request: &Request<Vec<u8>>,
     custom_head: Option<String>,
     custom_head: Option<String>,
     custom_index: Option<String>,
     custom_index: Option<String>,
+    assets_head: Option<String>,
     root_name: &str,
     root_name: &str,
 ) -> Result<Response<Cow<'static, [u8]>>> {
 ) -> Result<Response<Cow<'static, [u8]>>> {
     // If the request is for the root, we'll serve the index.html file.
     // 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.
                 // 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();
                 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
                 template
                     .replace("<!-- MODULE LOADER -->", &module_loader(root_name))
                     .replace("<!-- MODULE LOADER -->", &module_loader(root_name))

+ 17 - 1
packages/desktop/src/webview.rs

@@ -18,6 +18,16 @@ pub fn build(
     let custom_head = cfg.custom_head.clone();
     let custom_head = cfg.custom_head.clone();
     let index_file = cfg.custom_index.clone();
     let index_file = cfg.custom_index.clone();
     let root_name = cfg.root_name.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
     // 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() {
     if cfg.window.window.window_icon.is_none() {
@@ -45,7 +55,13 @@ pub fn build(
             }
             }
         })
         })
         .with_custom_protocol(String::from("dioxus"), move |r| {
         .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| {
         .with_file_drop_handler(move |window, evet| {
             file_handler
             file_handler