diff --git a/Cargo.toml b/Cargo.toml index 770fd7b1d..7f1223520 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,12 +46,10 @@ flate2 = "1.0.22" tar = "0.4.38" tower = "0.4.12" -syn = { version = "1.0", optional = true } -dioxus-rsx-interpreter = { path = "../dioxus/packages/rsx_interpreter", optional = true } -proc-macro2 = { version = "1.0", features = ["span-locations"], optional = true } +syn = { version = "1.0" } +dioxus-rsx-interpreter = { path = "../dioxus/packages/rsx_interpreter" } +proc-macro2 = { version = "1.0", features = ["span-locations"] } -[features] -hot_reload = ["dioxus-rsx-interpreter", "proc-macro2", "syn"] [[bin]] path = "src/main.rs" diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index f5ee45b87..3f284cbbf 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -39,6 +39,11 @@ pub struct ConfigOptsServe { /// Build platform: support Web & Desktop [default: "default_platform"] #[clap(long)] pub platform: Option, + + /// Build with hot reloading rsx [default: false] + #[clap(long)] + #[serde(default)] + pub hot_reload: bool, } /// Ensure the given value for `--public-url` is formatted correctly. diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 85bd92c74..391e646d4 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -18,7 +18,9 @@ impl Serve { let mut crate_config = crate::CrateConfig::new()?; // change the relase state. - crate_config.with_release(self.serve.release); + crate_config + .with_release(self.serve.release) + .with_hot_reload(self.serve.hot_reload); if self.serve.example.is_some() { crate_config.as_example(self.serve.example.unwrap()); diff --git a/src/config.rs b/src/config.rs index 783c72ccd..aa2a03440 100644 --- a/src/config.rs +++ b/src/config.rs @@ -114,6 +114,7 @@ pub struct CrateConfig { pub executable: ExecutableType, pub dioxus_config: DioxusConfig, pub release: bool, + pub hot_reload: bool, } #[derive(Debug, Clone)] @@ -162,6 +163,7 @@ impl CrateConfig { let executable = ExecutableType::Binary(output_filename); let release = false; + let hot_reload = false; Ok(Self { out_dir, @@ -173,6 +175,7 @@ impl CrateConfig { executable, release, dioxus_config, + hot_reload, }) } @@ -186,6 +189,11 @@ impl CrateConfig { self } + pub fn with_hot_reload(&mut self, hot_reload: bool) -> &mut Self { + self.hot_reload = hot_reload; + self + } + // pub fn with_build_options(&mut self, options: &BuildOptions) { // if let Some(name) = &options.example { // self.as_example(name.clone()); diff --git a/src/lib.rs b/src/lib.rs index 683b7251a..6d009c18c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,5 +19,4 @@ pub use error::*; pub mod logging; pub use logging::*; -#[cfg(feature = "hot_reload")] pub mod hot_reload; diff --git a/src/server/mod.rs b/src/server/mod.rs index 262dbf17d..8a98fd9d9 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -15,20 +15,24 @@ use tower_http::services::fs::{ServeDir, ServeFileSystemResponseBody}; use crate::{builder, serve::Serve, CrateConfig, Result}; use tokio::sync::broadcast; -#[cfg(feature = "hot_reload")] mod hot_reload; -#[cfg(feature = "hot_reload")] use hot_reload::*; struct WsReloadState { update: broadcast::Sender, - #[cfg(feature = "hot_reload")] - last_file_rebuild: Arc>, + last_file_rebuild: Option>>, watcher_config: CrateConfig, } -#[cfg(feature = "hot_reload")] pub async fn startup(config: CrateConfig) -> Result<()> { + if config.hot_reload { + startup_hot_reload(config).await + } else { + startup_default(config).await + } +} + +pub async fn startup_hot_reload(config: CrateConfig) -> Result<()> { log::info!("🚀 Starting development server..."); let dist_path = config.out_dir.clone(); @@ -46,7 +50,7 @@ pub async fn startup(config: CrateConfig) -> Result<()> { let ws_reload_state = Arc::new(WsReloadState { update: reload_tx.clone(), - last_file_rebuild: last_file_rebuild.clone(), + last_file_rebuild: Some(last_file_rebuild.clone()), watcher_config: config.clone(), }); @@ -186,8 +190,7 @@ pub async fn startup(config: CrateConfig) -> Result<()> { Ok(()) } -#[cfg(not(feature = "hot_reload"))] -pub async fn startup(config: CrateConfig) -> Result<()> { +pub async fn startup_default(config: CrateConfig) -> Result<()> { log::info!("🚀 Starting development server..."); let dist_path = config.out_dir.clone(); @@ -196,6 +199,8 @@ pub async fn startup(config: CrateConfig) -> Result<()> { let ws_reload_state = Arc::new(WsReloadState { update: reload_tx.clone(), + + last_file_rebuild: None, watcher_config: config.clone(), }); @@ -313,9 +318,8 @@ async fn ws_handler( { let _ = Serve::regen_dev_page(&state.watcher_config); } - #[cfg(feature = "hot_reload")] - { - let mut write = state.last_file_rebuild.lock().unwrap(); + if let Some(file_map) = &state.last_file_rebuild { + let mut write = file_map.lock().unwrap(); *write = FileMap::new(state.watcher_config.crate_dir.clone()); } }