Skip to content

Commit d26b727

Browse files
watch: re-run worker when its dependencies change
1 parent ba11d26 commit d26b727

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

lib/internal/watch_mode/files_watcher.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const { TIMEOUT_MAX } = require('internal/timers');
1717

1818
const EventEmitter = require('events');
1919
const { addAbortListener } = require('internal/events/abort_listener');
20-
const { watch } = require('fs');
20+
const { watch, readFileSync } = require('fs');
2121
const { fileURLToPath } = require('internal/url');
2222
const { resolve, dirname, sep } = require('path');
2323
const { setTimeout, clearTimeout } = require('timers');
@@ -166,6 +166,29 @@ class FilesWatcher extends EventEmitter {
166166
}
167167
}
168168

169+
#parseWorkerImports(workerFile) {
170+
try {
171+
const content = readFileSync(workerFile, 'utf8');
172+
const imports = [];
173+
// Match: import x from 'path', import('path'), require('path')
174+
const importRegex = /(?:import\s+(?:.*?\s+from\s+)?['"`]([^'"`]+)['"`]|import\(\s*['"`]([^'"`]+)['"`]\s*\)|require\(\s*['"`]([^'"`]+)['"`]\s*\))/g;
175+
let match;
176+
while ((match = importRegex.exec(content)) !== null) {
177+
const importPath = match[1] || match[2] || match[3];
178+
if (importPath && !importPath.startsWith('node:') && !StringPrototypeStartsWith(importPath, '.')) {
179+
// Skip Node.js built-ins and only process relative paths
180+
continue;
181+
}
182+
if (importPath) {
183+
imports.push(importPath);
184+
}
185+
}
186+
return imports;
187+
} catch (err) {
188+
return [];
189+
}
190+
}
191+
169192
watchChildProcessModules(child, key = null) {
170193
if (this.#passthroughIPC) {
171194
this.#setupIPC(child);
@@ -180,7 +203,17 @@ class FilesWatcher extends EventEmitter {
180203
ArrayPrototypeForEach(message['watch:import'], (file) => this.filterFile(fileURLToPath(file), key));
181204
}
182205
if (ArrayIsArray(message['watch:worker'])) {
183-
ArrayPrototypeForEach(message['watch:worker'], (file) => this.filterFile(file, key));
206+
ArrayPrototypeForEach(message['watch:worker'], (workerFile) => {
207+
// Add worker file itself
208+
this.filterFile(workerFile, key);
209+
210+
// Parse and watch worker dependencies
211+
const imports = this.#parseWorkerImports(workerFile);
212+
ArrayPrototypeForEach(imports, (importPath) => {
213+
const resolvedPath = resolve(dirname(workerFile), importPath);
214+
this.filterFile(resolvedPath, key);
215+
});
216+
});
184217
}
185218
} catch {
186219
// Failed watching file. ignore

0 commit comments

Comments
 (0)