diff --git a/packages/cli-config/src/config.rs b/packages/cli-config/src/config.rs index 1478d6ed3..85d052cb5 100644 --- a/packages/cli-config/src/config.rs +++ b/packages/cli-config/src/config.rs @@ -220,10 +220,13 @@ impl Default for DioxusConfig { pub struct ApplicationConfig { #[serde(default = "default_name")] pub name: String, + #[serde(default = "default_platform")] pub default_platform: Platform, + #[serde(default = "out_dir_default")] pub out_dir: PathBuf, + #[serde(default = "asset_dir_default")] pub asset_dir: PathBuf, diff --git a/packages/cli/src/server/mod.rs b/packages/cli/src/server/mod.rs index 0c958e885..e2931d391 100644 --- a/packages/cli/src/server/mod.rs +++ b/packages/cli/src/server/mod.rs @@ -2,14 +2,12 @@ use crate::{cfg::ConfigOptsServe, BuildResult, Result}; use dioxus_cli_config::CrateConfig; use cargo_metadata::diagnostic::Diagnostic; -use dioxus_core::Template; use dioxus_hot_reload::HotReloadMsg; use dioxus_html::HtmlCtx; use dioxus_rsx::hot_reload::*; -use fs_extra::{dir::CopyOptions, file}; +use fs_extra::dir::CopyOptions; use notify::{RecommendedWatcher, Watcher}; use std::{ - ops::ControlFlow, path::PathBuf, sync::{Arc, Mutex}, }; @@ -44,7 +42,7 @@ async fn setup_file_watcher Result + Send + 'static>( // file watcher: check file change let mut allow_watch_path = config.dioxus_config.web.watcher.watch_path.clone(); - // Extend the watch path to include the assets directory + // Extend the watch path to include the assets directory - this is so we can hotreload CSS and other assets allow_watch_path.push(config.dioxus_config.application.asset_dir.clone()); // Create the file watcher @@ -168,28 +166,26 @@ fn hotreload_files( // If the file was not hotreloaded, continue if is_potentially_reloadable.is_none() { - println!("Not hotreloaded: {:?}", path); continue; } // If the file was hotreloaded, update the file map in place match rsx_file_map.update_rsx(path, &config.crate_dir) { Ok(UpdateResult::UpdatedRsx(msgs)) => { - println!("Updated: {:?}", msgs); - messages.extend( msgs.into_iter() .map(|msg| HotReloadMsg::UpdateTemplate(msg)), ); } + // If the file was not updated, we need to do a full rebuild Ok(UpdateResult::NeedsRebuild) => { - println!("Needs rebuild: {:?}", path); + log::trace!("Needs full rebuild because file changed: {:?}", path); *needs_full_rebuild = true; } + // Not necessarily a fatal error, but we should log it Err(err) => log::error!("{}", err), - // Err(err) => log::error!("{}", err), } } diff --git a/packages/rsx/src/hot_reload/hot_reloading_file_map.rs b/packages/rsx/src/hot_reload/hot_reloading_file_map.rs index c40191cfd..bf1701420 100644 --- a/packages/rsx/src/hot_reload/hot_reloading_file_map.rs +++ b/packages/rsx/src/hot_reload/hot_reloading_file_map.rs @@ -9,11 +9,7 @@ pub use proc_macro2::TokenStream; pub use std::collections::HashMap; pub use std::sync::Mutex; pub use std::time::SystemTime; -use std::{ - collections::HashSet, - hash::Hash, - path::{Display, PathBuf}, -}; +use std::{collections::HashSet, path::PathBuf}; pub use std::{fs, io, path::Path}; pub use std::{fs::File, io::Read}; pub use syn::__private::ToTokens; @@ -65,21 +61,30 @@ impl FileMap { /// Create a new FileMap from a crate directory pub fn create_with_filter( - path: PathBuf, + crate_dir: PathBuf, mut filter: impl FnMut(&Path) -> bool, ) -> io::Result> { - let FileMapSearchResult { map, errors } = find_rs_files(path, &mut filter); + let FileMapSearchResult { map, errors } = find_rs_files(crate_dir.clone(), &mut filter); - let file_map_build_result = FileMapBuildResult { - errors, - map: Self { - map, - in_workspace: HashMap::new(), - phantom: std::marker::PhantomData, - }, + let mut map = Self { + map, + in_workspace: HashMap::new(), + phantom: std::marker::PhantomData, }; - Ok(file_map_build_result) + map.load_assets(crate_dir.as_path()); + + Ok(FileMapBuildResult { errors, map }) + } + + /// Start watching assets for changes + /// + /// This just diffs every file against itself and populates the tracked assets as it goes + pub fn load_assets(&mut self, crate_dir: &Path) { + let keys = self.map.keys().cloned().collect::>(); + for file in keys { + _ = self.update_rsx(file.as_path(), crate_dir); + } } /// Try to update the rsx in a file @@ -129,7 +134,6 @@ impl FileMap { // If the changes were some code, we should insert the file into the map and rebuild // todo: not sure we even need to put the cached file into the map, but whatever DiffResult::CodeChanged(_) => { - println!("code changed"); let cached_file = CachedSynFile { raw: src.clone(), path: file_path.to_path_buf(), @@ -142,8 +146,6 @@ impl FileMap { } }; - println!("instances: {:?}", instances); - let mut messages: Vec