Browse Source

Fix asset bundling in the CLI (#2145)

* Fix asset bundling in the CLI

* fix clippy
Evan Almloff 1 năm trước cách đây
mục cha
commit
9942c8bfd1
2 tập tin đã thay đổi với 25 bổ sung15 xóa
  1. 14 12
      packages/cli/src/cli/bundle.rs
  2. 11 3
      packages/desktop/src/protocol.rs

+ 14 - 12
packages/cli/src/cli/bundle.rs

@@ -66,7 +66,7 @@ impl Bundle {
         let mut crate_config = dioxus_cli_config::CrateConfig::new(bin)?;
 
         // change the release state.
-        crate_config.with_release(self.build.release);
+        crate_config.with_release(true);
         crate_config.with_verbose(self.build.verbose);
 
         if self.build.example.is_some() {
@@ -140,17 +140,19 @@ impl Bundle {
             }
         }
 
-        // Add all assets from collect assets to the bundle
-        {
-            let config = manganis_cli_support::Config::current();
-            let location = config.assets_serve_location().to_string();
-            let location = format!("./{}", location);
-            println!("Adding assets from {} to bundle", location);
-            if let Some(resources) = &mut bundle_settings.resources {
-                resources.push(location);
-            } else {
-                bundle_settings.resources = Some(vec![location]);
-            }
+        // Copy the assets in the dist directory to the bundle
+        let static_asset_output_dir = &crate_config.dioxus_config.application.out_dir;
+        // Make sure the dist directory is relative to the crate directory
+        let static_asset_output_dir = static_asset_output_dir
+            .strip_prefix(&crate_config.crate_dir)
+            .unwrap_or(static_asset_output_dir);
+
+        let static_asset_output_dir = static_asset_output_dir.display().to_string();
+        println!("Adding assets from {} to bundle", static_asset_output_dir);
+        if let Some(resources) = &mut bundle_settings.resources {
+            resources.push(static_asset_output_dir);
+        } else {
+            bundle_settings.resources = Some(vec![static_asset_output_dir]);
         }
 
         let mut settings = SettingsBuilder::new()

+ 11 - 3
packages/desktop/src/protocol.rs

@@ -82,7 +82,7 @@ fn assets_head() -> Option<String> {
     ))]
     {
         let head = crate::protocol::get_asset_root_or_default();
-        let head = head.join("__assets_head.html");
+        let head = head.join("dist").join("__assets_head.html");
         match std::fs::read_to_string(&head) {
             Ok(s) => Some(s),
             Err(err) => {
@@ -142,7 +142,9 @@ pub(super) fn desktop_handler(
     // Else, try to serve a file from the filesystem.
     match serve_from_fs(path) {
         Ok(res) => responder.respond(res),
-        Err(e) => tracing::error!("Error serving request from filesystem {}", e),
+        Err(e) => {
+            tracing::error!("Error serving request from filesystem {}", e);
+        }
     }
 }
 
@@ -152,7 +154,13 @@ fn serve_from_fs(path: PathBuf) -> Result<Response<Vec<u8>>> {
 
     // If we can't find it, make it absolute and try again
     if !asset.exists() {
-        asset = PathBuf::from("/").join(path);
+        asset = PathBuf::from("/").join(&path);
+    }
+
+    // If we can't find it, add the dist directory and try again
+    // When bundling we currently copy the whole dist directory to the output directory instead of the individual files because of a limitation of cargo bundle2
+    if !asset.exists() {
+        asset = get_asset_root_or_default().join("dist").join(&path);
     }
 
     if !asset.exists() {