Hotreloading of for/if/body, formatted strings, literals, component props, nested rsx, light CLI rewrite, cli TUI (#2258)
Hotreload the contents of for loops, if chains, component bodies, props, attributes, and any literals discovered in rsx! Add a TUI renderer to the CLI. Improve the CLI build system to be async and parallel. Refactor RSX to allow partial expansion of expressions. Merge autofmt implementations for consistency. Merge the representation of elements and components under the hood. Add a diagnostics system for rsx for improved error messages. Drop interprocess and move to websockets for communication between the CLI and the server. Assign IDs to nodes and attributes in a stable way to be used in non compiler contexts. Add hotreloading to any body of component/for loop/if chain/etc. --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com> Co-authored-by: Liam Mitchell <liamkarlmitchell@gmail.com>
This commit is contained in:
181 files changed
+12755
-10806
No files matched your search
+43
-102
@@ -1,124 +1,65 @@
|
||||
use std::{
|
||||
io::{BufRead, BufReader},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use dioxus_core::Template;
|
||||
#[cfg(feature = "file_watcher")]
|
||||
pub use dioxus_html::HtmlCtx;
|
||||
use interprocess::local_socket::LocalSocketStream;
|
||||
use dioxus_rsx::HotReloadedTemplate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(feature = "custom_file_watcher")]
|
||||
mod file_watcher;
|
||||
#[cfg(feature = "custom_file_watcher")]
|
||||
pub use file_watcher::*;
|
||||
#[cfg(feature = "client")]
|
||||
mod client;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
pub use client::*;
|
||||
|
||||
#[cfg(feature = "serve")]
|
||||
mod websocket;
|
||||
#[cfg(feature = "serve")]
|
||||
pub use websocket::*;
|
||||
mod ws_receiver;
|
||||
|
||||
#[cfg(feature = "serve")]
|
||||
/// The script to inject into the page to reconnect to server if the connection is lost
|
||||
pub const RECONNECT_SCRIPT: &str = include_str!("assets/autoreload.js");
|
||||
pub use ws_receiver::*;
|
||||
|
||||
/// A message the hot reloading server sends to the client
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
#[serde(bound(deserialize = "'de: 'static"))]
|
||||
pub enum HotReloadMsg {
|
||||
/// A template has been updated
|
||||
UpdateTemplate(Template),
|
||||
pub enum DevserverMsg {
|
||||
/// Attempt a hotreload
|
||||
/// This includes all the templates/literals/assets/binary patches that have changed in one shot
|
||||
HotReload(HotReloadMsg),
|
||||
|
||||
/// An asset discovered by rsx! has been updated
|
||||
UpdateAsset(PathBuf),
|
||||
/// The app should reload completely if it can
|
||||
FullReload,
|
||||
|
||||
/// The program needs to be recompiled, and the client should shut down
|
||||
/// The program is shutting down completely - maybe toss up a splash screen or something?
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
/// Connect to the hot reloading listener. The callback provided will be called every time a template change is detected
|
||||
pub fn connect(callback: impl FnMut(HotReloadMsg) + Send + 'static) {
|
||||
if cfg!(windows) {
|
||||
connect_at(PathBuf::from("@dioxusin"), callback);
|
||||
} else {
|
||||
// FIXME: this is falling back onto the current directory when not running under cargo, which is how the CLI runs this.
|
||||
// This needs to be fixed.
|
||||
let _manifest_dir = std::env::var("CARGO_MANIFEST_DIR");
|
||||
|
||||
// get the cargo manifest directory, where the target dir lives
|
||||
let mut path = match _manifest_dir {
|
||||
Ok(manifest_dir) => PathBuf::from(manifest_dir),
|
||||
Err(_) => std::env::current_dir().unwrap(),
|
||||
};
|
||||
|
||||
// walk the path until we a find a socket named `dioxusin` inside that folder's target directory
|
||||
loop {
|
||||
let maybe = path.join("target").join("dioxusin");
|
||||
|
||||
if maybe.exists() {
|
||||
path = maybe;
|
||||
break;
|
||||
}
|
||||
|
||||
// It's likely we're running under just cargo and not dx
|
||||
path = match path.parent() {
|
||||
Some(parent) => parent.to_path_buf(),
|
||||
None => return,
|
||||
};
|
||||
}
|
||||
|
||||
println!("connecting to {:?}", path);
|
||||
connect_at(path, callback);
|
||||
}
|
||||
/// A message the client sends from the frontend to the devserver
|
||||
///
|
||||
/// This is used to communicate with the devserver
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub enum ClientMsg {
|
||||
Log {
|
||||
level: String,
|
||||
messages: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn connect_at(socket: PathBuf, mut callback: impl FnMut(HotReloadMsg) + Send + 'static) {
|
||||
std::thread::spawn(move || {
|
||||
// There might be a socket since the we're not running under the hot reloading server
|
||||
let stream = if cfg!(windows) {
|
||||
LocalSocketStream::connect("@dioxusin")
|
||||
} else {
|
||||
LocalSocketStream::connect(socket.clone())
|
||||
};
|
||||
let Ok(socket) = stream else {
|
||||
println!(
|
||||
"could not find hot reloading server at {:?}, make sure it's running",
|
||||
socket
|
||||
);
|
||||
return;
|
||||
};
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
#[serde(bound(deserialize = "'de: 'static"))]
|
||||
pub struct HotReloadMsg {
|
||||
pub templates: Vec<HotReloadedTemplate>,
|
||||
pub assets: Vec<PathBuf>,
|
||||
|
||||
let mut buf_reader = BufReader::new(socket);
|
||||
|
||||
loop {
|
||||
let mut buf = String::new();
|
||||
|
||||
if let Err(err) = buf_reader.read_line(&mut buf) {
|
||||
if err.kind() != std::io::ErrorKind::WouldBlock {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let Ok(template) = serde_json::from_str(Box::leak(buf.into_boxed_str())) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
callback(template);
|
||||
}
|
||||
});
|
||||
/// A file changed that's not an asset or a rust file - best of luck!
|
||||
pub unknown_files: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
/// Start the hot reloading server with the current directory as the root
|
||||
#[macro_export]
|
||||
macro_rules! hot_reload_init {
|
||||
() => {
|
||||
#[cfg(debug_assertions)]
|
||||
dioxus_hot_reload::init(dioxus_hot_reload::Config::new().root(env!("CARGO_MANIFEST_DIR")));
|
||||
#[test]
|
||||
fn serialize_client_msg() {
|
||||
let msg = ClientMsg::Log {
|
||||
level: "info".to_string(),
|
||||
messages: vec!["hello world".to_string()],
|
||||
};
|
||||
|
||||
($cfg: expr) => {
|
||||
#[cfg(debug_assertions)]
|
||||
dioxus_hot_reload::init($cfg.root(env!("CARGO_MANIFEST_DIR")));
|
||||
};
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert_eq!(
|
||||
json,
|
||||
r#"{"Log":{"level":"info","messages":["hello world"]}}"#
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user