Skip to content

Commit

Permalink
feat: add regexes from node_modules watch (#1439)
Browse files Browse the repository at this point in the history
* feat: add regexes from node_modules watch

* fix: self add node_modules_regexes

* chore: ignorePath to Options

* chore: judge contains node_modules first
  • Loading branch information
Jinbao1001 authored Jul 25, 2024
1 parent d866363 commit e215fed
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ pub struct BuildParams {
};
watch?: {
ignoredPaths?: string[];
nodeModulesRegexes?: string[];
};
}"#)]
pub config: serde_json::Value,
Expand Down
5 changes: 3 additions & 2 deletions crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ pub struct ExperimentalConfig {
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct WatchConfig {
pub ignore_paths: Vec<String>,
pub ignore_paths: Option<Vec<String>>,
pub node_modules_regexes: Option<Vec<String>>,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down Expand Up @@ -722,7 +723,7 @@ const DEFAULT_CONFIG: &str = r#"
},
"useDefineForClassFields": true,
"emitDecoratorMetadata": false,
"watch": { "ignorePaths": [] },
"watch": { "ignorePaths": [], "nodeModulesRegexes": [] },
"devServer": { "host": "127.0.0.1", "port": 3000 }
}
"#;
Expand Down
28 changes: 28 additions & 0 deletions crates/mako/src/dev/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{self, Ok};
use colored::Colorize;
use notify::{self, EventKind, Watcher as NotifyWatcher};
use notify_debouncer_full::DebouncedEvent;
use regex::Regex;
use tracing::debug;

use crate::compiler::Compiler;
Expand All @@ -18,6 +19,7 @@ pub struct Watcher<'a> {
pub compiler: &'a Compiler,
pub watched_files: HashSet<PathBuf>,
pub watched_dirs: HashSet<PathBuf>,
node_modules_regexes: Vec<Regex>,
}

impl<'a> Watcher<'a> {
Expand All @@ -32,6 +34,16 @@ impl<'a> Watcher<'a> {
compiler,
watched_dirs: HashSet::new(),
watched_files: HashSet::new(),
node_modules_regexes: compiler
.context
.config
.watch
.node_modules_regexes
.clone()
.unwrap_or_default()
.iter()
.map(|s| Regex::new(s).unwrap())
.collect::<Vec<Regex>>(),
}
}

Expand All @@ -57,6 +69,20 @@ impl<'a> Watcher<'a> {
dirs.insert(dir);
}
}
if !self.node_modules_regexes.is_empty() {
let file_path = resource.0.path().to_str().unwrap();
let is_match = file_path.contains("node_modules")
&& self
.node_modules_regexes
.iter()
.any(|regex| regex.is_match(file_path));
if is_match {
let _ = self.watcher.watch(
resource.0.path().to_path_buf().as_path(),
notify::RecursiveMode::NonRecursive,
);
}
}
}
});
dirs.iter().try_for_each(|dir| {
Expand Down Expand Up @@ -103,6 +129,8 @@ impl<'a> Watcher<'a> {
.config
.watch
.ignore_paths
.as_deref()
.unwrap_or(&[])
.iter()
.map(|p| p.as_str()),
);
Expand Down
1 change: 1 addition & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export interface BuildParams {
};
watch?: {
ignoredPaths?: string[];
nodeModulesRegexes?: string[];
};
};
plugins: Array<JsHooks>;
Expand Down

0 comments on commit e215fed

Please sign in to comment.