浏览代码

make debug build of fullstack applications load faster by default

Evan Almloff 1 年之前
父节点
当前提交
b26985ea6c
共有 3 个文件被更改,包括 75 次插入0 次删除
  1. 4 0
      packages/cli/src/cli/build.rs
  2. 11 0
      packages/cli/src/cli/cfg.rs
  3. 60 0
      packages/cli/src/server/fullstack/mod.rs

+ 4 - 0
packages/cli/src/cli/build.rs

@@ -1,5 +1,7 @@
 #[cfg(feature = "plugin")]
 use crate::plugin::PluginManager;
+use crate::server::fullstack::FullstackServerEnvGuard;
+use crate::server::fullstack::FullstackWebEnvGuard;
 use crate::{cfg::Platform, WebAssetConfigDropGuard};
 
 use super::*;
@@ -60,6 +62,7 @@ impl Build {
                         }
                         None => web_config.features = Some(vec![web_feature]),
                     };
+                    let _gaurd = FullstackWebEnvGuard::new(self.build.debug);
                     crate::builder::build(&crate_config, false, self.build.skip_assets)?;
                 }
                 {
@@ -72,6 +75,7 @@ impl Build {
                         }
                         None => desktop_config.features = Some(vec![desktop_feature]),
                     };
+                    let _gaurd = FullstackServerEnvGuard::new(self.build.debug);
                     crate::builder::build_desktop(&desktop_config, false, self.build.skip_assets)?;
                 }
             }

+ 11 - 0
packages/cli/src/cli/cfg.rs

@@ -15,6 +15,11 @@ pub struct ConfigOptsBuild {
     #[serde(default)]
     pub release: bool,
 
+    /// Build with all debug info [default: false]
+    #[clap(long)]
+    #[serde(default)]
+    pub debug: bool,
+
     // Use verbose output [default: false]
     #[clap(long)]
     #[serde(default)]
@@ -63,6 +68,7 @@ impl From<ConfigOptsServe> for ConfigOptsBuild {
             client_feature: serve.client_feature,
             server_feature: serve.server_feature,
             skip_assets: serve.skip_assets,
+            debug: serve.debug,
         }
     }
 }
@@ -92,6 +98,11 @@ pub struct ConfigOptsServe {
     #[serde(default)]
     pub release: bool,
 
+    /// Build with all debug info [default: false]
+    #[clap(long)]
+    #[serde(default)]
+    pub debug: bool,
+
     // Use verbose output [default: false]
     #[clap(long)]
     #[serde(default)]

+ 60 - 0
packages/cli/src/server/fullstack/mod.rs

@@ -55,6 +55,7 @@ impl Platform for FullstackPlatform {
                 }
                 None => desktop_config.features = Some(vec![desktop_feature]),
             };
+            let _gaurd = FullstackServerEnvGuard::new(self.serve.debug);
             self.desktop.rebuild(&desktop_config)
         }
     }
@@ -71,5 +72,64 @@ fn build_web(serve: ConfigOptsServe) -> Result<()> {
         None => web_config.features = Some(vec![web_feature]),
     };
     web_config.platform = Some(crate::cfg::Platform::Web);
+
+    let _gaurd = FullstackWebEnvGuard::new(web_config.debug);
     crate::cli::build::Build { build: web_config }.build(None)
 }
+
+// Debug mode web builds have a very large size by default. If debug mode is not enabled, we strip some of the debug info by default
+// This reduces a hello world from ~40MB to ~2MB
+pub(crate) struct FullstackWebEnvGuard {
+    old_rustflags: Option<String>,
+}
+
+impl FullstackWebEnvGuard {
+    pub fn new(debug_mode: bool) -> Self {
+        Self {
+            old_rustflags: (!debug_mode).then(|| {
+                let old_rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();
+
+                std::env::set_var(
+                    "RUSTFLAGS",
+                    format!("{old_rustflags} -C debuginfo=none -C strip=debuginfo"),
+                );
+                old_rustflags
+            }),
+        }
+    }
+}
+
+impl Drop for FullstackWebEnvGuard {
+    fn drop(&mut self) {
+        if let Some(old_rustflags) = self.old_rustflags.take() {
+            std::env::set_var("RUSTFLAGS", old_rustflags);
+        }
+    }
+}
+
+// Debug mode web builds have a very large size by default. If debug mode is not enabled, we strip some of the debug info by default
+// This reduces a hello world from ~40MB to ~2MB
+pub(crate) struct FullstackServerEnvGuard {
+    old_rustflags: Option<String>,
+}
+
+impl FullstackServerEnvGuard {
+    pub fn new(debug_mode: bool) -> Self {
+        Self {
+            old_rustflags: (!debug_mode).then(|| {
+                let old_rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();
+
+                std::env::set_var("RUSTFLAGS", format!("{old_rustflags} -C opt-level=2"));
+                old_rustflags
+            }),
+        }
+    }
+}
+
+impl Drop for FullstackServerEnvGuard {
+    fn drop(&mut self) {
+        if let Some(old_rustflags) = self.old_rustflags.take() {
+            std::env::set_var("RUSTFLAGS", old_rustflags);
+        }
+    }
+}