Просмотр исходного кода

feat: add `dioxus.toml` support

mrxiaozhuox 3 лет назад
Родитель
Сommit
70adff7e44
5 измененных файлов с 98 добавлено и 22 удалено
  1. 1 0
      Cargo.toml
  2. 42 0
      Dioxus.toml
  3. 2 11
      src/builder.rs
  4. 39 1
      src/config.rs
  5. 14 10
      src/server/mod.rs

+ 1 - 0
Cargo.toml

@@ -16,6 +16,7 @@ wasm-bindgen-cli-support = "0.2.79"
 anyhow = "1.0.38"
 serde = { version = "1.0.133", features = ["derive"] }
 serde_json = "1"
+toml = "0.5.8"
 fs_extra = "1.2.0"
 cargo_toml = "0.10.0"
 futures = "0.3.12"

+ 42 - 0
Dioxus.toml

@@ -0,0 +1,42 @@
+[application]
+
+# App (Project) Name
+name = "dioxus-cli"
+
+# Dioxus App Support Platform
+# desktop, web, mobile, ssr
+platforms = ["web"]
+
+[web.app]
+
+# Web `build` & `serve` dist path
+out_dir = "dist"
+
+# resource (static) file folder
+static_dir = "static"
+
+[web.watcher]
+
+
+watch_path = ["src"]
+
+# include `assets` in web platform
+[web.resource]
+
+# CSS style file
+style = [
+    "./my-style.css"
+]
+
+# Javascript code file
+script = [
+    "./app.js"
+]
+
+[web.resource.dev]
+
+# Javascript code file
+# serve: [dev-server] only
+script = [
+    "./debug.js"
+]

+ 2 - 11
src/builder.rs

@@ -48,14 +48,12 @@ pub fn build(config: &CrateConfig, dist: PathBuf) -> Result<()> {
 
     let output = cmd.output()?;
 
-    if output.status.success() {
-        std::io::stdout().write_all(&output.stdout).unwrap();
-    } else {
+    if !output.status.success() {
         log::error!("Build failed!");
         let reason = String::from_utf8_lossy(&output.stderr).to_string();
         return Err(Error::BuildFailed(reason));
     }
-
+    
     // [2] Establish the output directory structure
     let bindgen_outdir = out_dir.join("assets");
 
@@ -88,13 +86,6 @@ pub fn build(config: &CrateConfig, dist: PathBuf) -> Result<()> {
         .out_name("module")
         .generate(&bindgen_outdir)?;
 
-    // [4]
-    // TODO: wasm-opt
-
-    // [5] Generate the html file with the module name
-    // TODO: support names via options
-
-    // log::info!("Writing to '{:#?}' directory...", out_dir);
     let copy_options = fs_extra::dir::CopyOptions::new();
     if static_dir.is_dir() {
         match fs_extra::dir::copy(static_dir, out_dir, &copy_options) {

+ 39 - 1
src/config.rs

@@ -1,6 +1,44 @@
 use crate::error::Result;
+use serde::{Deserialize, Serialize};
 use std::path::PathBuf;
 
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct DioxusConfig {
+    application: ApplicationConfig,
+    web: WebConfig,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct ApplicationConfig {
+    name: String,
+    platform: Vec<String>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct WebConfig {
+    app: WebAppConfing,
+    watcher: WebWatcherConfing,
+    resource: WebResourceConfing,
+}
+
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct WebAppConfing {
+
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct WebWatcherConfing {
+    
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct WebResourceConfing {
+    
+}
+
+
 #[derive(Debug, Clone)]
 pub struct CrateConfig {
     pub out_dir: PathBuf,
@@ -86,4 +124,4 @@ impl CrateConfig {
     //     self.release = options.release;
     //     self.out_dir = tempfile::Builder::new().tempdir().expect("").into_path();
     // }
-}
+}

+ 14 - 10
src/server/mod.rs

@@ -33,6 +33,8 @@ pub async fn startup(
 
     let (tx, rx) = channel();
 
+    let dist_path = opts.dist.clone().unwrap_or(PathBuf::from("dist"));
+
     // file watcher: check file change
     let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
     watcher
@@ -46,7 +48,7 @@ pub async fn startup(
 
     let watcher_conf = config.clone();
     let watcher_ws_state = ws_reload_state.clone();
-    let dist_path = opts.dist.clone().unwrap_or(PathBuf::from("dist"));
+    let watcher_dist_path = dist_path.clone();
     tokio::spawn(async move {
         loop {
             if let Ok(v) = rx.recv() {
@@ -55,7 +57,7 @@ pub async fn startup(
                     | DebouncedEvent::Write(_)
                     | DebouncedEvent::Remove(_)
                     | DebouncedEvent::Rename(_, _) => {
-                        if let Ok(_) = builder::build(&watcher_conf, dist_path.clone()) {
+                        if let Ok(_) = builder::build(&watcher_conf, watcher_dist_path.clone()) {
                             // change the websocket reload state to true;
                             // the page will auto-reload.
                             watcher_ws_state.lock().unwrap().change();
@@ -69,14 +71,16 @@ pub async fn startup(
 
     let app = Router::new()
         .route("/ws", get(ws_handler))
-        .fallback(get_service(ServeDir::new(config.out_dir)).handle_error(
-            |error: std::io::Error| async move {
-                (
-                    StatusCode::INTERNAL_SERVER_ERROR,
-                    format!("Unhandled internal error: {}", error),
-                )
-            },
-        ))
+        .fallback(
+            get_service(ServeDir::new(config.crate_dir.join(&dist_path))).handle_error(
+                |error: std::io::Error| async move {
+                    (
+                        StatusCode::INTERNAL_SERVER_ERROR,
+                        format!("Unhandled internal error: {}", error),
+                    )
+                },
+            ),
+        )
         .layer(AddExtensionLayer::new(ws_reload_state.clone()));
 
     // start serve dev-server at 0.0.0.0:8080