From 70adff7e443500e05ce8ff32a803971c4f816aff Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Sun, 23 Jan 2022 12:11:17 +0800 Subject: [PATCH] feat: add `dioxus.toml` support --- Cargo.toml | 1 + Dioxus.toml | 42 ++++++++++++++++++++++++++++++++++++++++++ src/builder.rs | 13 ++----------- src/config.rs | 40 +++++++++++++++++++++++++++++++++++++++- src/server/mod.rs | 24 ++++++++++++++---------- 5 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 Dioxus.toml diff --git a/Cargo.toml b/Cargo.toml index cd1dec98b..8bb6fd3ce 100644 --- a/Cargo.toml +++ b/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" diff --git a/Dioxus.toml b/Dioxus.toml new file mode 100644 index 000000000..abd2d1ee4 --- /dev/null +++ b/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" +] \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index aad275ff2..58ee085fc 100644 --- a/src/builder.rs +++ b/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, ©_options) { diff --git a/src/config.rs b/src/config.rs index d17294b18..9d38160f2 100644 --- a/src/config.rs +++ b/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, +} + +#[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(); // } -} +} \ No newline at end of file diff --git a/src/server/mod.rs b/src/server/mod.rs index 4f04d3268..6b91a2e71 100644 --- a/src/server/mod.rs +++ b/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