From 3a74d28a0eeb6c72f0c9773d91817ab78ecff701 Mon Sep 17 00:00:00 2001 From: Jinbao1001 Date: Wed, 17 Jul 2024 19:55:23 +0800 Subject: [PATCH] fix: watch files change of module_graph in node_modules (#1385) * fix: watch files change of module_graph in node_modules * chore: extends overrides_ingore_list --- crates/mako/src/dev/watch.rs | 55 +++++++++++++++++++++++++++--------- packages/mako/binding.d.ts | 4 --- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/crates/mako/src/dev/watch.rs b/crates/mako/src/dev/watch.rs index 108d13b62..74706daee 100644 --- a/crates/mako/src/dev/watch.rs +++ b/crates/mako/src/dev/watch.rs @@ -20,6 +20,11 @@ pub struct Watcher<'a> { pub watched_dirs: HashSet, } +pub struct IgnoreListArgs { + with_output_dir: bool, + with_node_modules: bool, +} + impl<'a> Watcher<'a> { pub fn new( root: &'a PathBuf, @@ -39,7 +44,13 @@ impl<'a> Watcher<'a> { pub fn watch(&mut self) -> anyhow::Result<()> { let t_watch = Instant::now(); - self.watch_dir_recursive(self.root.into(), &self.get_ignore_list(true))?; + self.watch_dir_recursive( + self.root.into(), + &self.get_ignore_list(IgnoreListArgs { + with_output_dir: true, + with_node_modules: false, + }), + )?; let module_graph = self.compiler.context.module_graph.read().unwrap(); let mut dirs = HashSet::new(); @@ -57,10 +68,23 @@ impl<'a> Watcher<'a> { dirs.insert(dir); } } + let _ = self.watch_file_or_dir( + resource.0.path().to_path_buf(), + &self.get_ignore_list(IgnoreListArgs { + with_output_dir: false, + with_node_modules: true, + }), + ); } }); dirs.iter().try_for_each(|dir| { - self.watch_dir_recursive(dir.into(), &self.get_ignore_list(false))?; + self.watch_dir_recursive( + dir.into(), + &self.get_ignore_list(IgnoreListArgs { + with_output_dir: false, + with_node_modules: false, + }), + )?; Ok(()) })?; @@ -92,20 +116,23 @@ impl<'a> Watcher<'a> { Ok(()) } - fn get_ignore_list(&self, with_output_dir: bool) -> Vec { - let mut ignore_list = vec![".git", "node_modules", ".DS_Store", ".node"]; - if with_output_dir { + fn get_ignore_list(&self, ignore_list_args: IgnoreListArgs) -> Vec { + let mut ignore_list: Vec<&str> = vec![".git", ".DS_Store", ".node"]; + let overrides_ignore_list = vec!["node_modules"]; + if ignore_list_args.with_output_dir { ignore_list.push(self.compiler.context.config.output.path.to_str().unwrap()); } - ignore_list.extend( - self.compiler - .context - .config - .watch - .ignore_paths - .iter() - .map(|p| p.as_str()), - ); + let config_ignore_path = &self.compiler.context.config.watch.ignore_paths; + if config_ignore_path.is_empty() { + ignore_list.extend(overrides_ignore_list); + } else { + ignore_list.extend(config_ignore_path.iter().map(|p: &String| p.as_str())); + } + + if ignore_list_args.with_node_modules { + ignore_list.retain(|&item| item != "node_modules"); + return ignore_list.into_iter().map(PathBuf::from).collect(); + } // node_modules of root dictionary and root dictionary's parent dictionaries should be ignored // for resolving the issue of "too many files open" in monorepo diff --git a/packages/mako/binding.d.ts b/packages/mako/binding.d.ts index 969219c72..2f8916ca1 100644 --- a/packages/mako/binding.d.ts +++ b/packages/mako/binding.d.ts @@ -3,10 +3,6 @@ /* auto-generated by NAPI-RS */ -export interface TransformOutput { - code: string; - map?: string; -} export interface JsHooks { name?: string; load?: (