Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e16a9a

Browse files
authored
fix(watcher): run first build after listener is attached (#2782)
<!-- Thank you for contributing! --> ### Description close #2779 Here using `process.nextTick` to ensure the behavior, it follows rollup https://github.com/rollup/rollup/blob/master/src/watch/watch.ts#L54 <!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
1 parent 31f81af commit 0e16a9a

8 files changed

Lines changed: 27 additions & 22 deletions

File tree

crates/rolldown/src/bundler.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,6 @@ impl Bundler {
174174
pub fn watch(bundler: Arc<Mutex<Bundler>>) -> Result<Arc<Watcher>> {
175175
let watcher = Arc::new(Watcher::new(bundler)?);
176176

177-
// using async task to run first build, it could be report first build error.
178-
let cloned_watcher = Arc::clone(&watcher);
179-
let future = async move {
180-
let _ = cloned_watcher.run().await;
181-
};
182-
#[cfg(target_family = "wasm")]
183-
{
184-
let handle = tokio::runtime::Handle::current();
185-
// could not block_on/spawn the main thread in WASI
186-
std::thread::spawn(move || {
187-
handle.spawn(future);
188-
});
189-
}
190-
#[cfg(not(target_family = "wasm"))]
191-
tokio::spawn(future);
192-
193177
wait_for_change(Arc::clone(&watcher));
194178

195179
Ok(watcher)

crates/rolldown/src/watcher/watcher.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ impl Watcher {
213213

214214
Ok(())
215215
}
216+
217+
pub async fn start(&self) {
218+
let _ = self.run().await;
219+
}
216220
}
217221

218222
pub async fn on_change(watcher: &Arc<Watcher>, path: &str, kind: WatcherChangeKind) {

crates/rolldown_binding/src/types/watcher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ impl BindingWatcher {
4141
);
4242
Ok(())
4343
}
44+
45+
#[napi]
46+
pub async fn start(&self) -> napi::Result<()> {
47+
self.inner.start().await;
48+
Ok(())
49+
}
4450
}
4551

4652
#[napi]

packages/rolldown/src/binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export declare class BindingTransformPluginContext {
8282
export declare class BindingWatcher {
8383
close(): Promise<void>
8484
on(event: BindingWatcherEvent, listener: (data?: Record<string, string>) => void): void
85+
start(): Promise<void>
8586
}
8687

8788
export declare class Bundler {

packages/rolldown/src/rolldown-binding.wasi-browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ function __napi_rs_initialize_modules(__napiInstance) {
151151
__napiInstance.exports['__napi_register__BindingSourcemap_struct_121']?.()
152152
__napiInstance.exports['__napi_register__BindingJsonSourcemap_struct_122']?.()
153153
__napiInstance.exports['__napi_register__BindingWatcher_struct_123']?.()
154-
__napiInstance.exports['__napi_register__BindingWatcher_impl_126']?.()
155-
__napiInstance.exports['__napi_register__BindingWatcherEvent_127']?.()
154+
__napiInstance.exports['__napi_register__BindingWatcher_impl_127']?.()
155+
__napiInstance.exports['__napi_register__BindingWatcherEvent_128']?.()
156156
}
157157
export const BindingCallableBuiltinPlugin = __napiModule.exports.BindingCallableBuiltinPlugin
158158
export const BindingLog = __napiModule.exports.BindingLog

packages/rolldown/src/rolldown-binding.wasi.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ function __napi_rs_initialize_modules(__napiInstance) {
175175
__napiInstance.exports['__napi_register__BindingSourcemap_struct_121']?.()
176176
__napiInstance.exports['__napi_register__BindingJsonSourcemap_struct_122']?.()
177177
__napiInstance.exports['__napi_register__BindingWatcher_struct_123']?.()
178-
__napiInstance.exports['__napi_register__BindingWatcher_impl_126']?.()
179-
__napiInstance.exports['__napi_register__BindingWatcherEvent_127']?.()
178+
__napiInstance.exports['__napi_register__BindingWatcher_impl_127']?.()
179+
__napiInstance.exports['__napi_register__BindingWatcherEvent_128']?.()
180180
}
181181
module.exports.BindingCallableBuiltinPlugin = __napiModule.exports.BindingCallableBuiltinPlugin
182182
module.exports.BindingLog = __napiModule.exports.BindingLog

packages/rolldown/src/watcher.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export class Watcher {
9191
this.controller.signal.addEventListener('abort', () => {
9292
clearInterval(timer)
9393
})
94+
// run first build after listener is attached
95+
process.nextTick(() => this.inner.start())
9496
}
9597
}
9698

packages/rolldown/tests/watch/watch.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ test.sequential('watch event', async () => {
7373
cwd: import.meta.dirname,
7474
})
7575

76-
await waitBuildFinished()
77-
7876
const events: any[] = []
7977
watcher.on('event', (event) => {
8078
if (event.code === 'BUNDLE_END') {
@@ -99,7 +97,17 @@ test.sequential('watch event', async () => {
9997
}
10098
})
10199

100+
await waitBuildFinished()
101+
// test first build event
102+
expect(events.slice(0, 4)).toEqual([
103+
{ code: 'START' },
104+
{ code: 'BUNDLE_START' },
105+
{ code: 'BUNDLE_END' },
106+
{ code: 'END' },
107+
])
108+
102109
// edit file
110+
events.length = 0
103111
fs.writeFileSync(input, 'console.log(3)')
104112
await waitBuildFinished()
105113
await watcher.close()

0 commit comments

Comments
 (0)