浏览代码

use cli flag instead of feature flag

Evan Almloff 3 年之前
父节点
当前提交
fb7ea5915b
共有 6 个文件被更改,包括 34 次插入18 次删除
  1. 3 5
      Cargo.toml
  2. 5 0
      src/cli/cfg.rs
  3. 3 1
      src/cli/serve/mod.rs
  4. 8 0
      src/config.rs
  5. 0 1
      src/lib.rs
  6. 15 11
      src/server/mod.rs

+ 3 - 5
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"

+ 5 - 0
src/cli/cfg.rs

@@ -39,6 +39,11 @@ pub struct ConfigOptsServe {
     /// Build platform: support Web & Desktop [default: "default_platform"]
     #[clap(long)]
     pub platform: Option<String>,
+
+    /// 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.

+ 3 - 1
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());

+ 8 - 0
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());

+ 0 - 1
src/lib.rs

@@ -19,5 +19,4 @@ pub use error::*;
 pub mod logging;
 pub use logging::*;
 
-#[cfg(feature = "hot_reload")]
 pub mod hot_reload;

+ 15 - 11
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<String>,
-    #[cfg(feature = "hot_reload")]
-    last_file_rebuild: Arc<Mutex<FileMap>>,
+    last_file_rebuild: Option<Arc<Mutex<FileMap>>>,
     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());
                         }
                     }