Load tracked assets

This commit is contained in:
Jonathan Kelley committed 2024-03-13 14:35:28 -07:00
1 parent e4f65b7260
commit 1bdc9d132e
3 files changed
+31 -27

No files matched your search

+3
View File
@@ -220,10 +220,13 @@ impl Default for DioxusConfig {
pub struct ApplicationConfig { pub struct ApplicationConfig {
#[serde(default = "default_name")] #[serde(default = "default_name")]
pub name: String, pub name: String,
#[serde(default = "default_platform")] #[serde(default = "default_platform")]
pub default_platform: Platform, pub default_platform: Platform,
#[serde(default = "out_dir_default")] #[serde(default = "out_dir_default")]
pub out_dir: PathBuf, pub out_dir: PathBuf,
#[serde(default = "asset_dir_default")] #[serde(default = "asset_dir_default")]
pub asset_dir: PathBuf, pub asset_dir: PathBuf,
+5 -9
View File
@@ -2,14 +2,12 @@ use crate::{cfg::ConfigOptsServe, BuildResult, Result};
use dioxus_cli_config::CrateConfig; use dioxus_cli_config::CrateConfig;
use cargo_metadata::diagnostic::Diagnostic; use cargo_metadata::diagnostic::Diagnostic;
use dioxus_core::Template;
use dioxus_hot_reload::HotReloadMsg; use dioxus_hot_reload::HotReloadMsg;
use dioxus_html::HtmlCtx; use dioxus_html::HtmlCtx;
use dioxus_rsx::hot_reload::*; use dioxus_rsx::hot_reload::*;
use fs_extra::{dir::CopyOptions, file}; use fs_extra::dir::CopyOptions;
use notify::{RecommendedWatcher, Watcher}; use notify::{RecommendedWatcher, Watcher};
use std::{ use std::{
ops::ControlFlow,
path::PathBuf, path::PathBuf,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
@@ -44,7 +42,7 @@ async fn setup_file_watcher<F: Fn() -> Result<BuildResult> + Send + 'static>(
// file watcher: check file change // file watcher: check file change
let mut allow_watch_path = config.dioxus_config.web.watcher.watch_path.clone(); 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()); allow_watch_path.push(config.dioxus_config.application.asset_dir.clone());
// Create the file watcher // Create the file watcher
@@ -168,28 +166,26 @@ fn hotreload_files(
// If the file was not hotreloaded, continue // If the file was not hotreloaded, continue
if is_potentially_reloadable.is_none() { if is_potentially_reloadable.is_none() {
println!("Not hotreloaded: {:?}", path);
continue; continue;
} }
// If the file was hotreloaded, update the file map in place // If the file was hotreloaded, update the file map in place
match rsx_file_map.update_rsx(path, &config.crate_dir) { match rsx_file_map.update_rsx(path, &config.crate_dir) {
Ok(UpdateResult::UpdatedRsx(msgs)) => { Ok(UpdateResult::UpdatedRsx(msgs)) => {
println!("Updated: {:?}", msgs);
messages.extend( messages.extend(
msgs.into_iter() msgs.into_iter()
.map(|msg| HotReloadMsg::UpdateTemplate(msg)), .map(|msg| HotReloadMsg::UpdateTemplate(msg)),
); );
} }
// If the file was not updated, we need to do a full rebuild
Ok(UpdateResult::NeedsRebuild) => { Ok(UpdateResult::NeedsRebuild) => {
println!("Needs rebuild: {:?}", path); log::trace!("Needs full rebuild because file changed: {:?}", path);
*needs_full_rebuild = true; *needs_full_rebuild = true;
} }
// Not necessarily a fatal error, but we should log it
Err(err) => log::error!("{}", err), Err(err) => log::error!("{}", err),
// Err(err) => log::error!("{}", err),
} }
} }
@@ -9,11 +9,7 @@ pub use proc_macro2::TokenStream;
pub use std::collections::HashMap; pub use std::collections::HashMap;
pub use std::sync::Mutex; pub use std::sync::Mutex;
pub use std::time::SystemTime; pub use std::time::SystemTime;
use std::{ use std::{collections::HashSet, path::PathBuf};
collections::HashSet,
hash::Hash,
path::{Display, PathBuf},
};
pub use std::{fs, io, path::Path}; pub use std::{fs, io, path::Path};
pub use std::{fs::File, io::Read}; pub use std::{fs::File, io::Read};
pub use syn::__private::ToTokens; pub use syn::__private::ToTokens;
@@ -65,21 +61,30 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
/// Create a new FileMap from a crate directory /// Create a new FileMap from a crate directory
pub fn create_with_filter( pub fn create_with_filter(
path: PathBuf, crate_dir: PathBuf,
mut filter: impl FnMut(&Path) -> bool, mut filter: impl FnMut(&Path) -> bool,
) -> io::Result<FileMapBuildResult<Ctx>> { ) -> io::Result<FileMapBuildResult<Ctx>> {
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 { let mut map = Self {
errors, map,
map: Self { in_workspace: HashMap::new(),
map, phantom: std::marker::PhantomData,
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::<Vec<_>>();
for file in keys {
_ = self.update_rsx(file.as_path(), crate_dir);
}
} }
/// Try to update the rsx in a file /// Try to update the rsx in a file
@@ -129,7 +134,6 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
// If the changes were some code, we should insert the file into the map and rebuild // 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 // todo: not sure we even need to put the cached file into the map, but whatever
DiffResult::CodeChanged(_) => { DiffResult::CodeChanged(_) => {
println!("code changed");
let cached_file = CachedSynFile { let cached_file = CachedSynFile {
raw: src.clone(), raw: src.clone(),
path: file_path.to_path_buf(), path: file_path.to_path_buf(),
@@ -142,8 +146,6 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
} }
}; };
println!("instances: {:?}", instances);
let mut messages: Vec<Template> = Vec::new(); let mut messages: Vec<Template> = Vec::new();
for calls in instances.into_iter() { for calls in instances.into_iter() {
@@ -355,6 +357,9 @@ fn find_rs_files(root: PathBuf, filter: &mut impl FnMut(&Path) -> bool) -> FileM
templates: HashMap::new(), templates: HashMap::new(),
tracked_assets: HashSet::new(), tracked_assets: HashSet::new(),
}; };
// track assets while we're here
files.insert(root, cached_file); files.insert(root, cached_file);
} }
Err(err) => { Err(err) => {