Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions crates/mako/src/dev/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ impl<'a> Watcher<'a> {
pub fn watch(&mut self) -> anyhow::Result<()> {
let t_watch = Instant::now();

let ignore_list = [".git", "node_modules", ".DS_Store", ".node"];

let mut root_ignore_list = ignore_list.to_vec();
root_ignore_list.push(self.compiler.context.config.output.path.to_str().unwrap());
self.watch_dir_recursive(self.root.into(), &root_ignore_list)?;
self.watch_dir_recursive(self.root.into(), &self.get_ignore_list(true))?;

let module_graph = self.compiler.context.module_graph.read().unwrap();
let mut dirs = HashSet::new();
Expand All @@ -64,7 +60,7 @@ impl<'a> Watcher<'a> {
}
});
dirs.iter().try_for_each(|dir| {
self.watch_dir_recursive(dir.into(), ignore_list.as_slice())?;
self.watch_dir_recursive(dir.into(), &self.get_ignore_list(false))?;
Ok(())
})?;

Expand Down Expand Up @@ -99,7 +95,31 @@ impl<'a> Watcher<'a> {
Ok(())
}

fn watch_dir_recursive(&mut self, path: PathBuf, ignore_list: &[&str]) -> anyhow::Result<()> {
fn get_ignore_list(&self, with_output_dir: bool) -> Vec<PathBuf> {
let mut ignore_list = vec![".git", "node_modules", ".DS_Store", ".node"];
if with_output_dir {
ignore_list.push(self.compiler.context.config.output.path.to_str().unwrap());
}

// 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
let mut dirs = vec![];
self.root.ancestors().for_each(|path| {
ignore_list.iter().for_each(|ignore| {
let mut path = PathBuf::from(path);
path.push(ignore);
dirs.push(path);
})
});

dirs
}

fn watch_dir_recursive(
&mut self,
path: PathBuf,
ignore_list: &[PathBuf],
) -> anyhow::Result<()> {
let items = std::fs::read_dir(path)?;
items
.into_iter()
Expand All @@ -111,7 +131,7 @@ impl<'a> Watcher<'a> {
Ok(())
}

fn watch_file_or_dir(&mut self, path: PathBuf, ignore_list: &[&str]) -> anyhow::Result<()> {
fn watch_file_or_dir(&mut self, path: PathBuf, ignore_list: &[PathBuf]) -> anyhow::Result<()> {
if Self::should_ignore_watch(&path, ignore_list) {
return Ok(());
}
Expand All @@ -131,9 +151,11 @@ impl<'a> Watcher<'a> {
Ok(())
}

fn should_ignore_watch(path: &Path, ignore_list: &[&str]) -> bool {
fn should_ignore_watch(path: &Path, ignore_list: &[PathBuf]) -> bool {
let path = path.to_string_lossy();
ignore_list.iter().any(|ignored| path.ends_with(ignored))
ignore_list
.iter()
.any(|ignored| path.strip_prefix(ignored.to_str().unwrap()).is_some())
}

fn should_ignore_event(path: &Path, kind: &EventKind) -> bool {
Expand Down