Skip to content

Commit c110e64

Browse files
watch: track worker thread dependencies in --watch mode for cjs files
1 parent 4a1bab7 commit c110e64

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,28 @@ function reportModuleNotFoundToWatchMode(basePath, extensions) {
329329
}
330330
}
331331

332+
/**
333+
* Tell the watch mode that a module was required, from within a worker thread.
334+
* @param {string} filename Absolute path of the module
335+
* @returns {void}
336+
*/
337+
function reportModuleToWatchModeFromWorker(filename) {
338+
if (!shouldReportRequiredModules()) {
339+
return;
340+
}
341+
const { isMainThread } = internalBinding('worker');
342+
if (isMainThread) {
343+
return;
344+
}
345+
// Lazy require to avoid circular dependency: worker_threads is loaded after
346+
// the CJS loader is fully set up.
347+
const { parentPort } = require('worker_threads');
348+
if (!parentPort) {
349+
return;
350+
}
351+
parentPort.postMessage({ 'watch:require': [filename] });
352+
}
353+
332354
/**
333355
* Create a new module instance.
334356
* @param {string} id
@@ -1245,6 +1267,7 @@ Module._load = function(request, parent, isMain, internalResolveOptions = kEmpty
12451267
relResolveCacheIdentifier = `${parent.path}\x00${request}`;
12461268
const filename = relativeResolveCache[relResolveCacheIdentifier];
12471269
reportModuleToWatchMode(filename);
1270+
reportModuleToWatchModeFromWorker(filename);
12481271
if (filename !== undefined) {
12491272
const cachedModule = Module._cache[filename];
12501273
if (cachedModule !== undefined) {
@@ -1335,6 +1358,7 @@ Module._load = function(request, parent, isMain, internalResolveOptions = kEmpty
13351358
}
13361359

13371360
reportModuleToWatchMode(filename);
1361+
reportModuleToWatchModeFromWorker(filename);
13381362
Module._cache[filename] = module;
13391363
module[kIsCachedByESMLoader] = false;
13401364
// If there are resolve hooks, carry the context information into the

lib/internal/watch_mode/files_watcher.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ class FilesWatcher extends EventEmitter {
181181
if (ArrayIsArray(message['watch:import'])) {
182182
ArrayPrototypeForEach(message['watch:import'], (file) => this.filterFile(fileURLToPath(file), key));
183183
}
184-
if (ArrayIsArray(message['watch:worker'])) {
185-
ArrayPrototypeForEach(message['watch:worker'], (file) => this.filterFile(file, key));
186-
}
187184
} catch {
188185
// Failed watching file. ignore
189186
}

lib/internal/worker.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayIsArray,
45
ArrayPrototypeForEach,
56
ArrayPrototypeMap,
67
ArrayPrototypePush,
@@ -202,17 +203,6 @@ class HeapProfileHandle {
202203
}
203204
}
204205

205-
/**
206-
* Tell the watch mode that a worker file was instantiated.
207-
* @param {string} filename Absolute path of the worker file
208-
* @returns {void}
209-
*/
210-
function reportWorkerToWatchMode(filename) {
211-
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
212-
process.send({ 'watch:worker': [filename] });
213-
}
214-
}
215-
216206
class Worker extends EventEmitter {
217207
constructor(filename, options = kEmptyObject) {
218208
throwIfBuildingSnapshot('Creating workers');
@@ -293,11 +283,6 @@ class Worker extends EventEmitter {
293283
name = StringPrototypeTrim(options.name);
294284
}
295285

296-
// Report to watch mode if this is a regular file (not eval, internal, or data URL)
297-
if (!isInternal && doEval === false) {
298-
reportWorkerToWatchMode(filename);
299-
}
300-
301286
debug('instantiating Worker.', `url: ${url}`, `doEval: ${doEval}`);
302287
// Set up the C++ handle for the worker, as well as some internal wiring.
303288
this[kHandle] = new WorkerImpl(url,
@@ -356,6 +341,15 @@ class Worker extends EventEmitter {
356341
this[kPublicPort].on(event, (message) => this.emit(event, message));
357342
});
358343
setupPortReferencing(this[kPublicPort], this, 'message');
344+
345+
// relay events from worker thread to watcher
346+
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
347+
this[kPublicPort].on('message', (message) => {
348+
if (ArrayIsArray(message?.['watch:require'])) {
349+
process.send({ 'watch:require': message['watch:require'] });
350+
}
351+
});
352+
}
359353
this[kPort].postMessage({
360354
argv,
361355
type: messageTypes.LOAD_SCRIPT,

0 commit comments

Comments
 (0)