-
Notifications
You must be signed in to change notification settings - Fork 86
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: hmr supports linked npm packages changes #864
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bba1061
feat: hmr supports linked npm packages changes
f5ea2d1
feat: switch the way to get dependecies's paths to be watched
f8c4c23
feat: optimize the way to get modules
d90af3c
feat: optimize the way to watch linked npm package
3d8139a
feat: optimize watch performance and add logs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#![feature(box_patterns)] | ||
#![feature(let_chains)] | ||
#![feature(result_option_inspect)] | ||
|
||
use std::sync::Arc; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,113 @@ | ||
use std::collections::HashMap; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::mpsc::Sender; | ||
use std::sync::Arc; | ||
|
||
use mako_core::anyhow; | ||
use mako_core::notify::{self, EventKind, Watcher}; | ||
use mako_core::anyhow::{self, Ok}; | ||
use mako_core::notify::{self, EventKind, Watcher as NotifyWatcher}; | ||
use mako_core::notify_debouncer_full::DebouncedEvent; | ||
|
||
pub struct Watch { | ||
pub root: PathBuf, | ||
pub delay: u64, | ||
pub tx: Sender<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>, | ||
use crate::compiler::Compiler; | ||
use crate::resolve::ResolverResource; | ||
|
||
pub struct Watcher<'a> { | ||
pub watcher: &'a mut dyn NotifyWatcher, | ||
pub root: &'a PathBuf, | ||
pub compiler: &'a Compiler, | ||
pub watched_files: HashSet<PathBuf>, | ||
pub watched_dirs: HashSet<PathBuf>, | ||
} | ||
|
||
impl Watch { | ||
impl<'a> Watcher<'a> { | ||
pub fn new( | ||
root: &'a PathBuf, | ||
watcher: &'a mut notify::RecommendedWatcher, | ||
compiler: &'a Arc<Compiler>, | ||
) -> Self { | ||
Self { | ||
root, | ||
watcher, | ||
compiler, | ||
watched_dirs: HashSet::new(), | ||
watched_files: HashSet::new(), | ||
} | ||
} | ||
|
||
// pub fn watch(root: &PathBuf, watcher: &mut notify::RecommendedWatcher) -> anyhow::Result<()> { | ||
pub fn watch(root: &PathBuf, watcher: &mut notify::RecommendedWatcher) -> anyhow::Result<()> { | ||
let items = std::fs::read_dir(root)?; | ||
pub fn watch(&mut self) -> anyhow::Result<()> { | ||
self.watch_dir_recursive( | ||
self.root.into(), | ||
&[".git", "node_modules", ".DS_Store", "dist", ".node"], | ||
)?; | ||
|
||
let module_graph = self.compiler.context.module_graph.read().unwrap(); | ||
module_graph | ||
.modules() | ||
.iter() | ||
.try_for_each(|module| -> anyhow::Result<()> { | ||
if let Some(ResolverResource::Resolved(resource)) = module | ||
.info | ||
.as_ref() | ||
.and_then(|info| info.resolved_resource.as_ref()) | ||
{ | ||
if let Some(dir) = &resource.0.description { | ||
let dir = dir.dir().as_ref(); | ||
// not in root dir or is root's parent dir | ||
if dir.strip_prefix(self.root).is_err() | ||
&& self.root.clone().strip_prefix(dir).is_err() | ||
{ | ||
self.watch_dir_recursive( | ||
dir.into(), | ||
&[".git", "node_modules", ".DS_Store", ".node"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个和前面的冲突了,提一下,可能经常要调整。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 提为了一个变量 |
||
)?; | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn refresh_watch(&mut self) -> anyhow::Result<()> { | ||
self.watch()?; | ||
Ok(()) | ||
} | ||
|
||
fn watch_dir_recursive(&mut self, path: PathBuf, ignore_list: &[&str]) -> anyhow::Result<()> { | ||
let items = std::fs::read_dir(path)?; | ||
items | ||
.into_iter() | ||
.try_for_each(|item| -> anyhow::Result<()> { | ||
let path = item.unwrap().path(); | ||
if Self::should_ignore_watch(&path) { | ||
return Ok(()); | ||
} | ||
if path.is_file() { | ||
watcher.watch(path.as_path(), notify::RecursiveMode::NonRecursive)?; | ||
} else if path.is_dir() { | ||
watcher.watch(path.as_path(), notify::RecursiveMode::Recursive)?; | ||
} else { | ||
// others like symlink? should be ignore? | ||
} | ||
self.watch_file_or_dir(path, ignore_list)?; | ||
Ok(()) | ||
})?; | ||
Ok(()) | ||
} | ||
|
||
fn should_ignore_watch(path: &Path) -> bool { | ||
fn watch_file_or_dir(&mut self, path: PathBuf, ignore_list: &[&str]) -> anyhow::Result<()> { | ||
if Self::should_ignore_watch(&path, ignore_list) { | ||
return Ok(()); | ||
} | ||
|
||
if path.is_file() && !self.watched_files.contains(&path) { | ||
self.watcher | ||
.watch(path.as_path(), notify::RecursiveMode::NonRecursive)?; | ||
self.watched_files.insert(path); | ||
} else if path.is_dir() && !self.watched_dirs.contains(&path) { | ||
self.watcher | ||
.watch(path.as_path(), notify::RecursiveMode::Recursive)?; | ||
self.watched_dirs.insert(path); | ||
} else { | ||
// others like symlink? should be ignore? | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn should_ignore_watch(path: &Path, ignore_list: &[&str]) -> bool { | ||
let path = path.to_string_lossy(); | ||
let ignore_list = [".git", "node_modules", ".DS_Store", "dist", ".node"]; | ||
ignore_list.iter().any(|ignored| path.ends_with(ignored)) | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 clone 看能不能省,因为每个 module 都会跑一遍,几万个 module 应该还是有消耗的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已更新