@@ -17,7 +17,7 @@ const { TIMEOUT_MAX } = require('internal/timers');
1717
1818const EventEmitter = require ( 'events' ) ;
1919const { addAbortListener } = require ( 'internal/events/abort_listener' ) ;
20- const { watch } = require ( 'fs' ) ;
20+ const { watch, readFileSync } = require ( 'fs' ) ;
2121const { fileURLToPath } = require ( 'internal/url' ) ;
2222const { resolve, dirname, sep } = require ( 'path' ) ;
2323const { 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 = / (?: i m p o r t \s + (?: .* ?\s + f r o m \s + ) ? [ ' " ` ] ( [ ^ ' " ` ] + ) [ ' " ` ] | i m p o r t \( \s * [ ' " ` ] ( [ ^ ' " ` ] + ) [ ' " ` ] \s * \) | r e q u i r e \( \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