Merge pull request #1908 from ealmloff/fix-assets

Fix manganis support for dioxus desktop
This commit is contained in:
Jonathan Kelley 2024-02-07 12:17:12 -08:00 committed by GitHub
commit 25d103c1a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 63 additions and 58 deletions

8
Cargo.lock generated
View file

@ -5902,7 +5902,7 @@ dependencies = [
[[package]]
name = "manganis"
version = "0.1.0"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=e0093a4#e0093a47f0fa3bb50c49cd21aee5aa674faa24ad"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=f982698#f982698027fc27d22acfb4121fba389ec125d538"
dependencies = [
"manganis-macro",
]
@ -5910,7 +5910,7 @@ dependencies = [
[[package]]
name = "manganis-cli-support"
version = "0.1.0"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=e0093a4#e0093a47f0fa3bb50c49cd21aee5aa674faa24ad"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=f982698#f982698027fc27d22acfb4121fba389ec125d538"
dependencies = [
"anyhow",
"cargo-lock 9.0.0",
@ -5936,7 +5936,7 @@ dependencies = [
[[package]]
name = "manganis-common"
version = "0.1.0"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=e0093a4#e0093a47f0fa3bb50c49cd21aee5aa674faa24ad"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=f982698#f982698027fc27d22acfb4121fba389ec125d538"
dependencies = [
"anyhow",
"base64 0.21.7",
@ -5951,7 +5951,7 @@ dependencies = [
[[package]]
name = "manganis-macro"
version = "0.0.1"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=e0093a4#e0093a47f0fa3bb50c49cd21aee5aa674faa24ad"
source = "git+https://github.com/DioxusLabs/collect-assets?rev=f982698#f982698027fc27d22acfb4121fba389ec125d538"
dependencies = [
"base64 0.21.7",
"manganis-cli-support",

View file

@ -99,11 +99,11 @@ thiserror = "1.0.40"
prettyplease = { package = "prettier-please", version = "0.2", features = [
"verbatim",
] }
manganis-cli-support = { git = "https://github.com/DioxusLabs/collect-assets", rev = "e0093a4", features = [
manganis-cli-support = { git = "https://github.com/DioxusLabs/collect-assets", rev = "f982698", features = [
"webp",
"html",
] }
manganis = { git = "https://github.com/DioxusLabs/collect-assets", rev = "e0093a4" }
manganis = { git = "https://github.com/DioxusLabs/collect-assets", rev = "f982698" }
# This is a "virtual package"
# It is not meant to be published, but is used so "cargo run --example XYZ" works properly

View file

@ -4,7 +4,8 @@ use dioxus::prelude::*;
static ASSET_PATH: &str = "examples/assets/logo.png";
#[cfg(feature = "collect-assets")]
static ASSET_PATH: &str = manganis::mg!(image("examples/assets/logo.png").format(ImageType::Avif));
static ASSET_PATH: &str =
manganis::mg!(image("examples/assets/logo.png").format(ImageType::Avif)).path();
fn main() {
launch(app);

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@
use dioxus::prelude::*;
const _STYLE: &str = manganis::mg!(file("./public/tailwind.css"));
const _STYLE: &str = manganis::mg!(file("public/tailwind.css"));
fn main() {
launch(app);

View file

@ -37,9 +37,9 @@ pub(crate) fn process_assets(config: &CrateConfig, manifest: &AssetManifest) ->
}
/// A guard that sets up the environment for the web renderer to compile in. This guard sets the location that assets will be served from
pub(crate) struct WebAssetConfigDropGuard;
pub(crate) struct AssetConfigDropGuard;
impl WebAssetConfigDropGuard {
impl AssetConfigDropGuard {
pub fn new() -> Self {
// Set up the collect asset config
manganis_cli_support::Config::default()
@ -49,7 +49,7 @@ impl WebAssetConfigDropGuard {
}
}
impl Drop for WebAssetConfigDropGuard {
impl Drop for AssetConfigDropGuard {
fn drop(&mut self) {
// Reset the config
manganis_cli_support::Config::default().save();

View file

@ -1,5 +1,5 @@
use crate::{
assets::{asset_manifest, create_assets_head, process_assets, WebAssetConfigDropGuard},
assets::{asset_manifest, create_assets_head, process_assets, AssetConfigDropGuard},
error::{Error, Result},
tools::Tool,
};
@ -87,7 +87,7 @@ pub fn build(
let out_dir = config.out_dir();
let asset_dir = config.asset_dir();
let _guard = WebAssetConfigDropGuard::new();
let _guard = AssetConfigDropGuard::new();
let _manganis_support = ManganisSupportGuard::default();
// start to build the assets
@ -339,6 +339,7 @@ pub fn build_desktop(
let ignore_files = build_assets(config)?;
let _guard = dioxus_cli_config::__private::save_config(config);
let _manganis_support = ManganisSupportGuard::default();
let _guard = AssetConfigDropGuard::new();
let mut cmd = subprocess::Exec::cmd("cargo")
.set_rust_flags(rust_flags)
@ -535,7 +536,7 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result<Vec<Diagnostic>> {
}
pub fn gen_page(config: &CrateConfig, manifest: Option<&AssetManifest>, serve: bool) -> String {
let _gaurd = WebAssetConfigDropGuard::new();
let _guard = AssetConfigDropGuard::new();
let crate_root = crate_root().unwrap();
let custom_html_file = crate_root.join("index.html");

View file

@ -1,6 +1,7 @@
use crate::assets::AssetConfigDropGuard;
#[cfg(feature = "plugin")]
use crate::plugin::PluginManager;
use crate::{assets::WebAssetConfigDropGuard, server::fullstack};
use crate::server::fullstack;
use dioxus_cli_config::Platform;
use super::*;
@ -69,7 +70,7 @@ impl Build {
Platform::Fullstack => {
// Fullstack mode must be built with web configs on the desktop
// (server) binary as well as the web binary
let _config = WebAssetConfigDropGuard::new();
let _config = AssetConfigDropGuard::new();
let client_rust_flags = fullstack::client_rust_flags(&self.build);
let server_rust_flags = fullstack::server_rust_flags(&self.build);
{

View file

@ -105,7 +105,16 @@ pub(super) fn index_request(
// Insert a custom head if provided
// We look just for the closing head tag. If a user provided a custom index with weird syntax, this might fail
if let Some(head) = custom_head {
let head = match custom_head {
Some(mut head) => {
if let Some(assets_head) = assets_head() {
head.push_str(&assets_head);
}
Some(head)
}
None => assets_head(),
};
if let Some(head) = head {
index.insert_str(index.find("</head>").expect("Head element to exist"), &head);
}
@ -124,46 +133,40 @@ pub(super) fn index_request(
.ok()
}
// let assets_head = {
// #[cfg(all(
// debug_assertions,
// any(
// target_os = "windows",
// target_os = "macos",
// target_os = "linux",
// target_os = "dragonfly",
// target_os = "freebsd",
// target_os = "netbsd",
// target_os = "openbsd"
// )
// ))]
// {
// None
// }
// #[cfg(not(all(
// debug_assertions,
// any(
// target_os = "windows",
// target_os = "macos",
// target_os = "linux",
// target_os = "dragonfly",
// target_os = "freebsd",
// target_os = "netbsd",
// target_os = "openbsd"
// )
// )))]
// {
// let head = crate::protocol::get_asset_root_or_default();
// let head = head.join("dist/__assets_head.html");
// match std::fs::read_to_string(&head) {
// Ok(s) => Some(s),
// Err(err) => {
// tracing::error!("Failed to read {head:?}: {err}");
// None
// }
// }
// }
// };
fn assets_head() -> Option<String> {
#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
{
let head = crate::protocol::get_asset_root_or_default();
let head = head.join("__assets_head.html");
match std::fs::read_to_string(&head) {
Ok(s) => Some(s),
Err(err) => {
tracing::error!("Failed to read {head:?}: {err}");
None
}
}
}
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)))]
{
None
}
}
/// Handle a request from the webview
///