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:
authored and GitHub committed 2024-07-17 19:11:18 -07:00
1 parent 176e67e5b7
commit 88af3e7eff
181 files changed
+12755 -10806

No files matched your search

+53 -34
View File
@@ -148,14 +148,16 @@ impl App {
not(target_os = "ios")
))]
pub fn connect_hotreload(&self) {
let Ok(cfg) = dioxus_cli_config::CURRENT_CONFIG.as_ref() else {
return;
};
let proxy = self.shared.proxy.clone();
dioxus_hot_reload::connect_at(cfg.target_dir.join("dioxusin"), {
let proxy = self.shared.proxy.clone();
move |template| {
let _ = proxy.send_event(UserWindowEvent::HotReloadEvent(template));
tokio::task::spawn(async move {
let Some(Ok(mut receiver)) = dioxus_hot_reload::NativeReceiver::create_from_cli().await
else {
return;
};
while let Some(Ok(msg)) = receiver.next().await {
_ = proxy.send_event(UserWindowEvent::HotReloadEvent(msg));
}
});
}
@@ -327,23 +329,27 @@ impl App {
not(target_os = "android"),
not(target_os = "ios")
))]
pub fn handle_hot_reload_msg(&mut self, msg: dioxus_hot_reload::HotReloadMsg) {
pub fn handle_hot_reload_msg(&mut self, msg: dioxus_hot_reload::DevserverMsg) {
match msg {
dioxus_hot_reload::HotReloadMsg::UpdateTemplate(template) => {
dioxus_hot_reload::DevserverMsg::HotReload(hr_msg) => {
for webview in self.webviews.values_mut() {
webview.dom.replace_template(template);
dioxus_hot_reload::apply_changes(&mut webview.dom, &hr_msg);
webview.poll_vdom();
}
}
dioxus_hot_reload::HotReloadMsg::Shutdown => {
self.control_flow = ControlFlow::Exit;
}
dioxus_hot_reload::HotReloadMsg::UpdateAsset(_) => {
for webview in self.webviews.values_mut() {
webview.kick_stylsheets();
if !hr_msg.assets.is_empty() {
for webview in self.webviews.values_mut() {
webview.kick_stylsheets();
}
}
}
dioxus_hot_reload::DevserverMsg::FullReload => {
// usually only web gets this message - what are we supposed to do?
// Maybe we could just binary patch ourselves in place without losing window state?
}
dioxus_hot_reload::DevserverMsg::Shutdown => {
self.control_flow = ControlFlow::Exit;
}
}
}
@@ -458,12 +464,9 @@ impl App {
monitor: monitor.name().unwrap().to_string(),
};
// Yes... I know... we're loading a file that might not be ours... but it's a debug feature
if let Ok(state) = serde_json::to_string(&state) {
// Write this to the target dir so we can pick back up in resume_from_state
if let Ok(cfg) = dioxus_cli_config::CURRENT_CONFIG.as_ref() {
let path = cfg.target_dir.join("window_state.json");
_ = std::fs::write(path, state);
}
_ = std::fs::write(restore_file(), state);
}
}
}
@@ -471,18 +474,13 @@ impl App {
// Write this to the target dir so we can pick back up
#[cfg(debug_assertions)]
fn resume_from_state(&mut self, webview: &WebviewInstance) {
if let Ok(cfg) = dioxus_cli_config::CURRENT_CONFIG.as_ref() {
let path = cfg.target_dir.join("window_state.json");
if let Ok(state) = std::fs::read_to_string(path) {
if let Ok(state) = serde_json::from_str::<PreservedWindowState>(&state) {
let window = &webview.desktop_context.window;
let position = (state.x, state.y);
let size = (state.width, state.height);
window.set_outer_position(tao::dpi::PhysicalPosition::new(
position.0, position.1,
));
window.set_inner_size(tao::dpi::PhysicalSize::new(size.0, size.1));
}
if let Ok(state) = std::fs::read_to_string(restore_file()) {
if let Ok(state) = serde_json::from_str::<PreservedWindowState>(&state) {
let window = &webview.desktop_context.window;
let position = (state.x, state.y);
let size = (state.width, state.height);
window.set_outer_position(tao::dpi::PhysicalPosition::new(position.0, position.1));
window.set_inner_size(tao::dpi::PhysicalSize::new(size.0, size.1));
}
}
}
@@ -552,3 +550,24 @@ pub fn hide_app_window(window: &wry::WebView) {
});
}
}
/// Return the location of a tempfile with our window state in it such that we can restore it later
#[cfg(debug_assertions)]
fn restore_file() -> std::path::PathBuf {
/// Get the name of the program or default to "dioxus" so we can hash it
fn get_prog_name_or_default() -> Option<String> {
Some(
std::env::current_exe()
.ok()?
.file_name()?
.to_str()?
.to_string(),
)
}
let name = get_prog_name_or_default().unwrap_or_else(|| "dioxus".to_string());
let hashed_id = name.chars().map(|c| c as usize).sum::<usize>();
let mut path = std::env::temp_dir();
path.push(format!("{}-window-state.json", hashed_id));
path
}
+1 -4
View File
@@ -1,4 +1,4 @@
use dioxus_core::prelude::{Runtime, RuntimeGuard, ScopeId};
use dioxus_core::prelude::{Runtime, ScopeId};
use rustc_hash::FxHashMap;
use std::{cell::RefCell, rc::Rc};
use wry::{http::Request, RequestAsyncResponder};
@@ -36,9 +36,6 @@ impl AssetHandlerRegistry {
responder: RequestAsyncResponder,
) {
if let Some(handler) = self.handlers.borrow().get(name) {
// Push the runtime onto the stack
let _guard = RuntimeGuard::new(self.dom_rt.clone());
// And run the handler in the scope of the component that created it
self.dom_rt
.on_scope(handler.scope, || (handler.f)(request, responder));
+1 -1
View File
@@ -53,7 +53,7 @@ impl Config {
.with_title(
dioxus_cli_config::CURRENT_CONFIG
.as_ref()
.map(|c| c.dioxus_config.application.name.clone())
.map(|c| c.application.name.clone())
.unwrap_or("Dioxus App".to_string()),
)
// During development we want the window to be on top so we can see it while we work
+1 -1
View File
@@ -24,7 +24,7 @@ pub enum UserWindowEvent {
not(target_os = "android"),
not(target_os = "ios")
))]
HotReloadEvent(dioxus_hot_reload::HotReloadMsg),
HotReloadEvent(dioxus_hot_reload::DevserverMsg),
/// Create a new window
NewWindow,
+1 -1
View File
@@ -255,7 +255,7 @@ fn get_asset_root() -> Option<PathBuf> {
if running_in_dev_mode() {
return dioxus_cli_config::CURRENT_CONFIG
.as_ref()
.map(|c| c.out_dir())
.map(|c| c.application.out_dir.clone())
.ok();
}