Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add regexes from node_modules watch #1439

Merged
merged 5 commits into from
Jul 25, 2024
Merged

Conversation

Jinbao1001
Copy link
Member

@Jinbao1001 Jinbao1001 commented Jul 23, 2024

watch module in node_modules directly make too many files os error

add config node_modules_regexes to watch node_modules

Summary by CodeRabbit

Summary by CodeRabbit

  • 新功能
    • 增加了对 node_modules 目录中路径的正则表达式匹配功能,用户可以在配置中指定要忽略的文件和目录,从而提高文件监视的性能和相关性。
    • 更新了 BuildParams 结构,新增可选属性 nodeModulesRegexes,允许用户定义路径匹配模式以优化构建过程。
    • 更新了默认配置以包含新的正则表达式选项,确保用户在使用默认设置时能够利用这一新功能。

Copy link
Contributor

coderabbitai bot commented Jul 23, 2024

Walkthrough

此次更改增强了 WatchConfig 结构体和 Watcher 的功能,允许用户通过新的 node_modules_regexes 字段配置正则表达式,以便更精确地监控文件变化。修改使得在监控过程中能够忽略特定的文件和目录,从而提升了性能和相关性。

Changes

文件路径 更改摘要
crates/mako/src/config/config.rs 增加了 node_modules_regexes 字段到 WatchConfig 结构体,并更新 DEFAULT_CONFIG 常量以包含此字段的默认值。
crates/mako/src/dev/watch.rs Watcher 实现中增加了正则表达式匹配机制,根据配置中指定的模式过滤目录监视事件,提升了监控效率。
crates/binding/src/lib.rs BuildParams 结构体中新增了可选字段 nodeModulesRegexes,以增强对节点模块的路径忽略配置。
packages/mako/binding.d.ts BuildParams 接口中新增了可选属性 nodeModulesRegexes,允许用户定义用于忽略特定模块的正则表达式。

Poem

🐰 在代码的草地上跳跃,
新增功能真是妙绝,
正则表达式来助阵,
忽略杂乱提高效,
监控更灵活,快乐无边! 🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (3)
crates/mako/src/dev/watch.rs (1)

61-61: 建议:检查配置项 node_modules_regexes 是否存在

在使用 node_modules_regexes 之前,建议检查配置项是否存在,以避免潜在的 None 值错误。

+ let node_modules_regexes = &self.compiler.context.config.watch.node_modules_regexes.unwrap_or(&vec![]);
crates/mako/src/config/config.rs (2)

449-449: 建议:为新字段 node_modules_regexes 添加文档注释

为了提高代码可读性和可维护性,建议为新字段 node_modules_regexes 添加文档注释。

/// 正则表达式列表,用于过滤 `node_modules` 目录中的文件
pub node_modules_regexes: Vec<String>,

723-723: 建议:为新字段 nodeModulesRegexes 添加默认值

为了确保配置的一致性,建议为新字段 nodeModulesRegexes 添加一个默认值。

- "nodeModulesRegexes": []
+ "nodeModulesRegexes": [],
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e8a1748 and 85d12f5.

Files selected for processing (2)
  • crates/mako/src/config/config.rs (2 hunks)
  • crates/mako/src/dev/watch.rs (2 hunks)

Comment on lines 62 to 63
if !node_modules_regexes.is_empty() {
let regexes = node_modules_regexes.iter().map(|s| Regex::new(s).unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:处理正则表达式编译错误

当前代码假设正则表达式总是有效的。如果正则表达式无效,会导致 unwrap 引发恐慌。建议处理正则表达式编译错误。

- let regexes = node_modules_regexes.iter().map(|s| Regex::new(s).unwrap());
+ let regexes = node_modules_regexes.iter().map(|s| Regex::new(s)).collect::<Result<Vec<_>, _>>()?;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if !node_modules_regexes.is_empty() {
let regexes = node_modules_regexes.iter().map(|s| Regex::new(s).unwrap());
if !node_modules_regexes.is_empty() {
let regexes = node_modules_regexes.iter().map(|s| Regex::new(s)).collect::<Result<Vec<_>, _>>()?;

Comment on lines 64 to 66
let is_match = regexes
.into_iter()
.any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:优化正则表达式匹配逻辑

将正则表达式编译和匹配逻辑分开,以提高代码可读性和维护性。

- let is_match = regexes.into_iter().any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));
+ let is_match = regexes.iter().any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let is_match = regexes
.into_iter()
.any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));
let is_match = regexes
.iter()
.any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 85d12f5 and 4e5eff8.

Files selected for processing (1)
  • crates/mako/src/dev/watch.rs (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/mako/src/dev/watch.rs

Copy link
Member

@sorrycc sorrycc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1、补充下背景?
2、介绍下 node_modules_regexes 的作用?可能得加个前缀,现在评名字不太能理解他的作用
3、配置需要同步调整 bindings/src/lib.rs 里的类型
4、配置需要同步更新 config.md 和 config.zh-CN.md

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4e5eff8 and b8abf47.

Files selected for processing (4)
  • crates/binding/src/lib.rs (1 hunks)
  • crates/mako/src/config/config.rs (2 hunks)
  • crates/mako/src/dev/watch.rs (5 hunks)
  • packages/mako/binding.d.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/mako/src/config/config.rs
Additional comments not posted (3)
packages/mako/binding.d.ts (1)

186-186: 新增属性 nodeModulesRegexes 看起来很好。

这个更改允许用户通过正则表达式来匹配 node_modules 目录中的路径,从而提高监控文件变化的灵活性和性能。

crates/binding/src/lib.rs (1)

171-171: 新增字段 nodeModulesRegexes 看起来很好。

这个更改允许用户通过正则表达式来匹配 node_modules 目录中的路径,从而提高监控文件变化的灵活性和性能。

crates/mako/src/dev/watch.rs (1)

72-83: 建议:优化正则表达式匹配逻辑

将正则表达式编译和匹配逻辑分开,以提高代码可读性和维护性。

- let is_match = regexes.into_iter().any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));
+ let is_match = regexes.iter().any(|regex| regex.is_match(resource.0.path().to_str().unwrap()));

Likely invalid or redundant comment.

Comment on lines +77 to +82
if is_match {
let _ = self.watcher.watch(
resource.0.path().to_path_buf().as_path(),
notify::RecursiveMode::NonRecursive,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:处理 watch 方法的错误

当前代码忽略了 watch 方法的错误。建议处理这些错误,以便在发生错误时采取适当的行动。

- let _ = self.watcher.watch(resource.0.path().to_path_buf().as_path(), notify::RecursiveMode::NonRecursive);
+ if let Err(e) = self.watcher.watch(resource.0.path().to_path_buf().as_path(), notify::RecursiveMode::NonRecursive) {
+     eprintln!("Failed to watch path: {:?}", e);
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if is_match {
let _ = self.watcher.watch(
resource.0.path().to_path_buf().as_path(),
notify::RecursiveMode::NonRecursive,
);
}
if is_match {
if let Err(e) = self.watcher.watch(
resource.0.path().to_path_buf().as_path(),
notify::RecursiveMode::NonRecursive,
) {
eprintln!("Failed to watch path: {:?}", e);
}
}

@@ -18,6 +19,7 @@
pub compiler: &'a Compiler,
pub watched_files: HashSet<PathBuf>,
pub watched_dirs: HashSet<PathBuf>,
node_modules_regexes: Vec<Regex>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:处理正则表达式编译错误

当前代码假设正则表达式总是有效的。如果正则表达式无效,会导致 unwrap 引发恐慌。建议处理正则表达式编译错误。

- let regexes = node_modules_regexes.iter().map(|s| Regex::new(s).unwrap());
+ let regexes = node_modules_regexes.iter().map(|s| Regex::new(s)).collect::<Result<Vec<_>, _>>()?;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
node_modules_regexes: Vec<Regex>,
node_modules_regexes: Vec<Regex>,
```
```diff
- let regexes = node_modules_regexes.iter().map(|s| Regex::new(s).unwrap());
+ let regexes = node_modules_regexes.iter().map(|s| Regex::new(s)).collect::<Result<Vec<_>, _>>()?;

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b8abf47 and 34538fb.

Files selected for processing (1)
  • crates/mako/src/dev/watch.rs (5 hunks)
Additional comments not posted (2)
crates/mako/src/dev/watch.rs (2)

10-10: 导入 regex crate

导入 regex crate 是为了支持正则表达式功能,这是必要的。


22-22: 添加新字段 node_modules_regexes

添加 node_modules_regexes 字段是为了存储编译后的正则表达式模式,这是必要的。

Comment on lines +37 to +46
node_modules_regexes: compiler
.context
.config
.watch
.node_modules_regexes
.clone()
.unwrap_or_default()
.iter()
.map(|s| Regex::new(s).unwrap())
.collect::<Vec<Regex>>(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:处理正则表达式编译错误

当前代码假设正则表达式总是有效的。如果正则表达式无效,会导致 unwrap 引发恐慌。建议处理正则表达式编译错误。

- let regexes = node_modules_regexes.iter().map(|s| Regex::new(s).unwrap());
+ let regexes = node_modules_regexes.iter().map(|s| Regex::new(s)).collect::<Result<Vec<_>, _>>()?;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
node_modules_regexes: compiler
.context
.config
.watch
.node_modules_regexes
.clone()
.unwrap_or_default()
.iter()
.map(|s| Regex::new(s).unwrap())
.collect::<Vec<Regex>>(),
node_modules_regexes: compiler
.context
.config
.watch
.node_modules_regexes
.clone()
.unwrap_or_default()
- .iter()
- .map(|s| Regex::new(s).unwrap())
- .collect::<Vec<Regex>>(),
+ .iter()
+ .map(|s| Regex::new(s))
+ .collect::<Result<Vec<_>, _>>()?,

Comment on lines +72 to +85
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,
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优化正则表达式匹配逻辑和处理 watch 方法的错误

将正则表达式编译和匹配逻辑分开,以提高代码可读性和维护性。同时,当前代码忽略了 watch 方法的错误,建议处理这些错误。

- let is_match = file_path.contains("node_modules")
-    && self.node_modules_regexes.iter().any(|regex| regex.is_match(file_path));
+ let is_match = file_path.contains("node_modules") && self.node_modules_regexes.iter().any(|regex| regex.is_match(file_path));

- let _ = self.watcher.watch(resource.0.path().to_path_buf().as_path(), notify::RecursiveMode::NonRecursive);
+ if let Err(e) = self.watcher.watch(resource.0.path().to_path_buf().as_path(), notify::RecursiveMode::NonRecursive) {
+     eprintln!("Failed to watch path: {:?}", e);
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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,
);
}
}
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 {
if let Err(e) = self.watcher.watch(
resource.0.path().to_path_buf().as_path(),
notify::RecursiveMode::NonRecursive,
) {
eprintln!("Failed to watch path: {:?}", e);
}
}
}

@Jinbao1001 Jinbao1001 merged commit e215fed into master Jul 25, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants