feat: add Dioxus.toml

This commit is contained in:
mrxiaozhuox committed 2022-01-23 17:35:59 +08:00
1 parent 70adff7e44
commit 9bb10c65a3
13 files changed
+269 -170

No files matched your search

-19
View File
@@ -1,19 +0,0 @@
<html>
<head></head>
<body>
<div>
<h1>
Sorry, but building your application failed.
</h1>
<p>
Here's the error:
</p>
<code>
{err}
</code>
</div>
</body>
</html>
+33 -21
View File
@@ -13,7 +13,7 @@ use std::{
};
use tower_http::services::ServeDir;
use crate::{builder, CrateConfig};
use crate::{builder, crate_root, CrateConfig};
struct WsRelodState {
update: bool,
@@ -25,42 +25,54 @@ impl WsRelodState {
}
}
pub async fn startup(
config: CrateConfig,
opts: &crate::cfg::ConfigOptsServe,
) -> anyhow::Result<()> {
pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
log::info!("🚀 Starting development server...");
let (tx, rx) = channel();
let dist_path = opts.dist.clone().unwrap_or(PathBuf::from("dist"));
let dist_path = config.out_dir.clone();
// file watcher: check file change
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
watcher
.watch(
config.crate_dir.join("src").clone(),
notify::RecursiveMode::Recursive,
)
.watch(config.crate_dir.clone(), notify::RecursiveMode::Recursive)
.unwrap();
let ws_reload_state = Arc::new(Mutex::new(WsRelodState { update: false }));
let watcher_conf = config.clone();
let watcher_ws_state = ws_reload_state.clone();
let watcher_dist_path = dist_path.clone();
tokio::spawn(async move {
let allow_watch_path = watcher_conf
.dioxus_config
.web
.watcher
.watch_path
.clone()
.unwrap_or(vec![PathBuf::from("src")]);
let crate_dir = watcher_conf.crate_dir.clone();
loop {
if let Ok(v) = rx.recv() {
match v {
DebouncedEvent::Create(_)
| DebouncedEvent::Write(_)
| DebouncedEvent::Remove(_)
| DebouncedEvent::Rename(_, _) => {
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();
DebouncedEvent::Create(e)
| DebouncedEvent::Write(e)
| DebouncedEvent::Remove(e)
| DebouncedEvent::Rename(e, _) => {
let mut reload = false;
for path in &allow_watch_path {
let temp = crate_dir.clone().join(path);
if e.starts_with(temp) {
reload = true;
break;
}
}
if reload {
if let Ok(_) = builder::build(&watcher_conf) {
// change the websocket reload state to true;
// the page will auto-reload.
watcher_ws_state.lock().unwrap().change();
}
}
}
_ => {}
@@ -70,7 +82,7 @@ pub async fn startup(
});
let app = Router::new()
.route("/ws", get(ws_handler))
.route("/_dioxus/ws", get(ws_handler))
.fallback(
get_service(ServeDir::new(config.crate_dir.join(&dist_path))).handle_error(
|error: std::io::Error| async move {
-39
View File
@@ -1,39 +0,0 @@
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta charset="UTF-8" />
<title>Dioxus-CLI Dev Server</title>
</head>
<body>
<div id="main"></div>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
import init from "./assets/module.js";
init("./assets/module_bg.wasm");
</script>
<script>
(function () {
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
var url = protocol + "//" + window.location.host + "/ws";
var poll_interval = 2000;
var reload_upon_connect = () => {
window.setTimeout(() => {
var ws = new WebSocket(url);
ws.onopen = () => window.location.reload();
ws.onclose = reload_upon_connect;
}, poll_interval);
};
var ws = new WebSocket(url);
ws.onmessage = (ev) => {
if (ev.data == "reload") {
window.location.reload();
}
};
ws.onclose = reload_upon_connect;
})();
</script>
</body>
</html>