mod.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. use dioxus_cli_config::CrateConfig;
  2. use crate::{
  3. cfg::{ConfigOptsBuild, ConfigOptsServe},
  4. Result,
  5. };
  6. use super::{desktop, Platform};
  7. static CLIENT_RUST_FLAGS: &str = "-C debuginfo=none -C strip=debuginfo";
  8. // The `opt-level=2` increases build times, but can noticeably decrease time
  9. // between saving changes and being able to interact with an app. The "overall"
  10. // time difference (between having and not having the optimization) can be
  11. // almost imperceptible (~1 s) but also can be very noticeable (~6 s) — depends
  12. // on setup (hardware, OS, browser, idle load).
  13. static SERVER_RUST_FLAGS: &str = "-C opt-level=2";
  14. static DEBUG_RUST_FLAG: &str = "-C debug-assertions";
  15. fn rust_flags(build: &ConfigOptsBuild, base_flags: &str) -> String {
  16. let mut rust_flags = base_flags.to_string();
  17. if !build.release {
  18. rust_flags += " ";
  19. rust_flags += DEBUG_RUST_FLAG;
  20. };
  21. rust_flags
  22. }
  23. pub fn client_rust_flags(build: &ConfigOptsBuild) -> String {
  24. rust_flags(build, CLIENT_RUST_FLAGS)
  25. }
  26. pub fn server_rust_flags(build: &ConfigOptsBuild) -> String {
  27. rust_flags(build, SERVER_RUST_FLAGS)
  28. }
  29. pub async fn startup(config: CrateConfig, serve: &ConfigOptsServe) -> Result<()> {
  30. desktop::startup_with_platform::<FullstackPlatform>(config, serve).await
  31. }
  32. fn start_web_build_thread(
  33. config: &CrateConfig,
  34. serve: &ConfigOptsServe,
  35. ) -> std::thread::JoinHandle<Result<()>> {
  36. let serve = serve.clone();
  37. let target_directory = config.client_target_dir();
  38. std::fs::create_dir_all(&target_directory).unwrap();
  39. std::thread::spawn(move || build_web(serve, &target_directory))
  40. }
  41. fn make_desktop_config(config: &CrateConfig, serve: &ConfigOptsServe) -> CrateConfig {
  42. let mut desktop_config = config.clone();
  43. desktop_config.target_dir = config.server_target_dir();
  44. let desktop_feature = serve.server_feature.clone();
  45. let features = &mut desktop_config.features;
  46. match features {
  47. Some(features) => {
  48. features.push(desktop_feature);
  49. }
  50. None => desktop_config.features = Some(vec![desktop_feature]),
  51. };
  52. desktop_config
  53. }
  54. struct FullstackPlatform {
  55. serve: ConfigOptsServe,
  56. desktop: desktop::DesktopPlatform,
  57. server_rust_flags: String,
  58. }
  59. impl Platform for FullstackPlatform {
  60. fn start(config: &CrateConfig, serve: &ConfigOptsServe) -> Result<Self>
  61. where
  62. Self: Sized,
  63. {
  64. let thread_handle = start_web_build_thread(config, serve);
  65. let desktop_config = make_desktop_config(config, serve);
  66. let server_rust_flags = server_rust_flags(&serve.clone().into());
  67. let desktop = desktop::DesktopPlatform::start_with_options(
  68. &desktop_config,
  69. serve,
  70. Some(server_rust_flags.clone()),
  71. )?;
  72. thread_handle
  73. .join()
  74. .map_err(|_| anyhow::anyhow!("Failed to join thread"))??;
  75. Ok(Self {
  76. desktop,
  77. serve: serve.clone(),
  78. server_rust_flags,
  79. })
  80. }
  81. fn rebuild(&mut self, crate_config: &CrateConfig) -> Result<crate::BuildResult> {
  82. let thread_handle = start_web_build_thread(crate_config, &self.serve);
  83. let desktop_config = make_desktop_config(crate_config, &self.serve);
  84. let result = self
  85. .desktop
  86. .rebuild_with_options(&desktop_config, Some(self.server_rust_flags.clone()));
  87. thread_handle
  88. .join()
  89. .map_err(|_| anyhow::anyhow!("Failed to join thread"))??;
  90. result
  91. }
  92. }
  93. fn build_web(serve: ConfigOptsServe, target_directory: &std::path::Path) -> Result<()> {
  94. let mut web_config: ConfigOptsBuild = serve.into();
  95. let web_feature = web_config.client_feature.clone();
  96. let features = &mut web_config.features;
  97. match features {
  98. Some(features) => {
  99. features.push(web_feature);
  100. }
  101. None => web_config.features = Some(vec![web_feature]),
  102. };
  103. web_config.platform = Some(dioxus_cli_config::Platform::Web);
  104. crate::cli::build::Build {
  105. build: web_config.clone(),
  106. }
  107. .build(
  108. None,
  109. Some(target_directory),
  110. Some(client_rust_flags(&web_config)),
  111. )
  112. }