dioxus_config.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use super::*;
  2. use crate::Result;
  3. use anyhow::Context;
  4. use krates::{Krates, NodeId};
  5. use serde::{Deserialize, Serialize};
  6. #[derive(Debug, Clone, Serialize, Deserialize)]
  7. pub(crate) struct DioxusConfig {
  8. pub(crate) application: ApplicationConfig,
  9. #[serde(default)]
  10. pub(crate) web: WebConfig,
  11. #[serde(default)]
  12. pub(crate) desktop: DesktopConfig,
  13. #[serde(default)]
  14. pub(crate) bundle: BundleConfig,
  15. }
  16. impl Default for DioxusConfig {
  17. fn default() -> Self {
  18. Self {
  19. application: ApplicationConfig {
  20. default_platform: default_platform(),
  21. asset_dir: asset_dir_default(),
  22. sub_package: None,
  23. },
  24. web: WebConfig {
  25. app: WebAppConfig {
  26. title: default_title(),
  27. base_path: None,
  28. },
  29. proxy: vec![],
  30. watcher: Default::default(),
  31. resource: WebResourceConfig {
  32. dev: WebDevResourceConfig {
  33. style: vec![],
  34. script: vec![],
  35. },
  36. style: Some(vec![]),
  37. script: Some(vec![]),
  38. },
  39. https: WebHttpsConfig {
  40. enabled: None,
  41. mkcert: None,
  42. key_path: None,
  43. cert_path: None,
  44. },
  45. pre_compress: true,
  46. wasm_opt: Default::default(),
  47. },
  48. desktop: DesktopConfig::default(),
  49. bundle: BundleConfig::default(),
  50. }
  51. }
  52. }
  53. impl DioxusConfig {
  54. pub fn load(krates: &Krates, package: NodeId) -> Result<Option<Self>> {
  55. // Walk up from the cargo.toml to the root of the workspace looking for Dioxus.toml
  56. let mut current_dir = krates[package]
  57. .manifest_path
  58. .parent()
  59. .unwrap()
  60. .as_std_path()
  61. .to_path_buf()
  62. .canonicalize()?;
  63. let workspace_path = krates
  64. .workspace_root()
  65. .as_std_path()
  66. .to_path_buf()
  67. .canonicalize()?;
  68. let mut dioxus_conf_file = None;
  69. while current_dir.starts_with(&workspace_path) {
  70. let config = ["Dioxus.toml", "dioxus.toml"]
  71. .into_iter()
  72. .map(|file| current_dir.join(file))
  73. .find(|path| path.is_file());
  74. // Try to find Dioxus.toml in the current directory
  75. if let Some(new_config) = config {
  76. dioxus_conf_file = Some(new_config.as_path().to_path_buf());
  77. break;
  78. }
  79. // If we can't find it, go up a directory
  80. current_dir = current_dir
  81. .parent()
  82. .context("Failed to find Dioxus.toml")?
  83. .to_path_buf();
  84. }
  85. let Some(dioxus_conf_file) = dioxus_conf_file else {
  86. return Ok(None);
  87. };
  88. toml::from_str::<DioxusConfig>(&std::fs::read_to_string(&dioxus_conf_file)?)
  89. .map_err(|err| {
  90. anyhow::anyhow!("Failed to parse Dioxus.toml at {dioxus_conf_file:?}: {err}").into()
  91. })
  92. .map(Some)
  93. }
  94. }