assets.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. use std::{fs::File, io::Write, path::PathBuf};
  2. use crate::Result;
  3. use dioxus_cli_config::CrateConfig;
  4. use manganis_cli_support::{AssetManifest, AssetManifestExt};
  5. pub fn asset_manifest(crate_config: &CrateConfig) -> AssetManifest {
  6. AssetManifest::load_from_path(
  7. crate_config.crate_dir.join("Cargo.toml"),
  8. crate_config.workspace_dir.join("Cargo.lock"),
  9. )
  10. }
  11. /// Create a head file that contains all of the imports for assets that the user project uses
  12. pub fn create_assets_head(config: &CrateConfig, manifest: &AssetManifest) -> Result<()> {
  13. let mut file = File::create(config.out_dir().join("__assets_head.html"))?;
  14. file.write_all(manifest.head().as_bytes())?;
  15. Ok(())
  16. }
  17. /// Process any assets collected from the binary
  18. pub(crate) fn process_assets(config: &CrateConfig, manifest: &AssetManifest) -> anyhow::Result<()> {
  19. let static_asset_output_dir = PathBuf::from(
  20. config
  21. .dioxus_config
  22. .web
  23. .app
  24. .base_path
  25. .clone()
  26. .unwrap_or_default(),
  27. );
  28. let static_asset_output_dir = config.out_dir().join(static_asset_output_dir);
  29. manifest.copy_static_assets_to(static_asset_output_dir)?;
  30. Ok(())
  31. }
  32. /// 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
  33. pub(crate) struct AssetConfigDropGuard;
  34. impl AssetConfigDropGuard {
  35. pub fn new() -> Self {
  36. // Set up the collect asset config
  37. manganis_cli_support::Config::default()
  38. .with_assets_serve_location("/")
  39. .save();
  40. Self {}
  41. }
  42. }
  43. impl Drop for AssetConfigDropGuard {
  44. fn drop(&mut self) {
  45. // Reset the config
  46. manganis_cli_support::Config::default().save();
  47. }
  48. }