feat: add dioxus.toml support

This commit is contained in:
mrxiaozhuox committed 2022-01-23 12:11:17 +08:00
1 parent 5395e7693c
commit 70adff7e44
5 files changed
+96 -20

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ wasm-bindgen-cli-support = "0.2.79"
anyhow = "1.0.38" anyhow = "1.0.38"
serde = { version = "1.0.133", features = ["derive"] } serde = { version = "1.0.133", features = ["derive"] }
serde_json = "1" serde_json = "1"
toml = "0.5.8"
fs_extra = "1.2.0" fs_extra = "1.2.0"
cargo_toml = "0.10.0" cargo_toml = "0.10.0"
futures = "0.3.12" futures = "0.3.12"
+42
View File
@@ -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"
]
+1 -10
View File
@@ -48,9 +48,7 @@ pub fn build(config: &CrateConfig, dist: PathBuf) -> Result<()> {
let output = cmd.output()?; let output = cmd.output()?;
if output.status.success() { if !output.status.success() {
std::io::stdout().write_all(&output.stdout).unwrap();
} else {
log::error!("Build failed!"); log::error!("Build failed!");
let reason = String::from_utf8_lossy(&output.stderr).to_string(); let reason = String::from_utf8_lossy(&output.stderr).to_string();
return Err(Error::BuildFailed(reason)); return Err(Error::BuildFailed(reason));
@@ -88,13 +86,6 @@ pub fn build(config: &CrateConfig, dist: PathBuf) -> Result<()> {
.out_name("module") .out_name("module")
.generate(&bindgen_outdir)?; .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(); let copy_options = fs_extra::dir::CopyOptions::new();
if static_dir.is_dir() { if static_dir.is_dir() {
match fs_extra::dir::copy(static_dir, out_dir, &copy_options) { match fs_extra::dir::copy(static_dir, out_dir, &copy_options) {
+38
View File
@@ -1,6 +1,44 @@
use crate::error::Result; use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf; 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)] #[derive(Debug, Clone)]
pub struct CrateConfig { pub struct CrateConfig {
pub out_dir: PathBuf, pub out_dir: PathBuf,
+14 -10
View File
@@ -33,6 +33,8 @@ pub async fn startup(
let (tx, rx) = channel(); let (tx, rx) = channel();
let dist_path = opts.dist.clone().unwrap_or(PathBuf::from("dist"));
// file watcher: check file change // file watcher: check file change
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap(); let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
watcher watcher
@@ -46,7 +48,7 @@ pub async fn startup(
let watcher_conf = config.clone(); let watcher_conf = config.clone();
let watcher_ws_state = ws_reload_state.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 { tokio::spawn(async move {
loop { loop {
if let Ok(v) = rx.recv() { if let Ok(v) = rx.recv() {
@@ -55,7 +57,7 @@ pub async fn startup(
| DebouncedEvent::Write(_) | DebouncedEvent::Write(_)
| DebouncedEvent::Remove(_) | DebouncedEvent::Remove(_)
| DebouncedEvent::Rename(_, _) => { | 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; // change the websocket reload state to true;
// the page will auto-reload. // the page will auto-reload.
watcher_ws_state.lock().unwrap().change(); watcher_ws_state.lock().unwrap().change();
@@ -69,14 +71,16 @@ pub async fn startup(
let app = Router::new() let app = Router::new()
.route("/ws", get(ws_handler)) .route("/ws", get(ws_handler))
.fallback(get_service(ServeDir::new(config.out_dir)).handle_error( .fallback(
|error: std::io::Error| async move { 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), StatusCode::INTERNAL_SERVER_ERROR,
) format!("Unhandled internal error: {}", error),
}, )
)) },
),
)
.layer(AddExtensionLayer::new(ws_reload_state.clone())); .layer(AddExtensionLayer::new(ws_reload_state.clone()));
// start serve dev-server at 0.0.0.0:8080 // start serve dev-server at 0.0.0.0:8080