Skip to content
Closed
Show file tree
Hide file tree
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
87 changes: 41 additions & 46 deletions crates/rolldown/src/watch/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,53 +171,7 @@ impl WatcherImpl {
}

pub async fn start(&self) {
let build_delay = {
let mut build_delay: u32 = 0;
for bundler in &self.bundlers {
let bundler = bundler.lock().await;
if let Some(delay) = bundler.options.watch.build_delay {
if delay > build_delay {
build_delay = delay;
}
}
}
build_delay
};

let _ = self.run(&[]).await;
let future = async move {
while let Ok(msg) = self.exec_rx.lock().await.recv() {
match msg {
ExecChannelMsg::Exec => {
tokio::time::sleep(Duration::from_millis(u64::from(build_delay))).await;
tracing::debug!(name= "watcher invalidate", watch_changes = ?self.watch_changes);
let watch_changes =
self.watch_changes.iter().map(|v| v.deref().clone()).collect::<Vec<_>>();
for change in &watch_changes {
for task in &self.tasks {
task.on_change(change.path.as_str(), change.kind).await;
task.invalidate(change.path.as_str());
}
self.watch_changes.remove(change);
}
let changed_files =
watch_changes.iter().map(|item| item.path.clone()).collect::<Vec<_>>();
let _ = self.run(&changed_files).await;
}
ExecChannelMsg::Close => break,
}
}
};
#[cfg(target_family = "wasm")]
{
futures::executor::block_on(future);
}
#[cfg(not(target_family = "wasm"))]
{
tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(future);
});
}
}
}

Expand Down Expand Up @@ -270,3 +224,44 @@ pub fn wait_for_change(watcher: Arc<WatcherImpl>) {
};
tokio::spawn(future);
}

pub fn wait_for_invalidate_run(watcher: Arc<WatcherImpl>) {
let future = async move {
let build_delay = {
let mut build_delay: u32 = 0;
for bundler in &watcher.bundlers {
let bundler = bundler.lock().await;
if let Some(delay) = bundler.options.watch.build_delay {
if delay > build_delay {
build_delay = delay;
}
}
}
build_delay
};

while let Ok(msg) = watcher.exec_rx.lock().await.recv() {
match msg {
ExecChannelMsg::Exec => {
tokio::time::sleep(Duration::from_millis(u64::from(build_delay))).await;

tracing::debug!(name= "watcher invalidate", watch_changes = ?watcher.watch_changes);
let watch_changes =
watcher.watch_changes.iter().map(|v| v.deref().clone()).collect::<Vec<_>>();
for change in &watch_changes {
for task in &watcher.tasks {
task.on_change(change.path.as_str(), change.kind).await;
task.invalidate(change.path.as_str());
}
watcher.watch_changes.remove(change);
}
let changed_files =
watch_changes.iter().map(|item| item.path.clone()).collect::<Vec<_>>();
let _ = watcher.run(&changed_files).await;
}
ExecChannelMsg::Close => break,
}
}
};
tokio::spawn(future);
}
3 changes: 2 additions & 1 deletion crates/rolldown/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rolldown_common::NotifyOption;
use tokio::sync::Mutex;

use crate::{
watch::watcher::{wait_for_change, WatcherImpl},
watch::watcher::{wait_for_change, wait_for_invalidate_run, WatcherImpl},
Bundler,
};

Expand All @@ -22,6 +22,7 @@ impl Watcher {
}

pub async fn start(&self) {
wait_for_invalidate_run(Arc::clone(&self.0));
wait_for_change(Arc::clone(&self.0));
self.0.start().await;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rolldown/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"private": true,
"type": "module",
"scripts": {
"test:main": "cross-env RUST_BACKTRACE=1 ROLLDOWN_TEST=1 vitest run --exclude '**/watch.test.ts' --reporter verbose --hideSkippedTests",
"test:watcher": "cross-env RUST_BACKTRACE=1 ROLLDOWN_TEST=1 vitest run watch.test.ts --reporter verbose --hideSkippedTests",
"test:main": "",
"test:watcher": "cross-env RD_LOG=DEBUG RUST_BACKTRACE=1 ROLLDOWN_TEST=1 vitest run watch.test.ts --reporter verbose --hideSkippedTests",
"test:stability": "node ./stability/issue-3453/src/build.mjs && node ./stability/issue-3453/verify.mjs",
"type-check": "tsc --noEmit"
},
Expand Down
Loading
Loading