Skip to content

Commit db4875c

Browse files
wacth: track worker thread dependencies in --watch mode for cjs files
1 parent 06d754b commit db4875c

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

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
@@ -179,9 +179,6 @@ class FilesWatcher extends EventEmitter {
179179
if (ArrayIsArray(message['watch:import'])) {
180180
ArrayPrototypeForEach(message['watch:import'], (file) => this.filterFile(fileURLToPath(file), key));
181181
}
182-
if (ArrayIsArray(message['watch:worker'])) {
183-
ArrayPrototypeForEach(message['watch:worker'], (file) => this.filterFile(file, key));
184-
}
185182
} catch {
186183
// Failed watching file. ignore
187184
}

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,
@@ -195,17 +196,6 @@ class HeapProfileHandle {
195196
}
196197
}
197198

198-
/**
199-
* Tell the watch mode that a worker file was instantiated.
200-
* @param {string} filename Absolute path of the worker file
201-
* @returns {void}
202-
*/
203-
function reportWorkerToWatchMode(filename) {
204-
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
205-
process.send({ 'watch:worker': [filename] });
206-
}
207-
}
208-
209199
class Worker extends EventEmitter {
210200
constructor(filename, options = kEmptyObject) {
211201
throwIfBuildingSnapshot('Creating workers');
@@ -286,11 +276,6 @@ class Worker extends EventEmitter {
286276
name = StringPrototypeTrim(options.name);
287277
}
288278

289-
// Report to watch mode if this is a regular file (not eval, internal, or data URL)
290-
if (!isInternal && doEval === false) {
291-
reportWorkerToWatchMode(filename);
292-
}
293-
294279
debug('instantiating Worker.', `url: ${url}`, `doEval: ${doEval}`);
295280
// Set up the C++ handle for the worker, as well as some internal wiring.
296281
this[kHandle] = new WorkerImpl(url,
@@ -349,6 +334,15 @@ class Worker extends EventEmitter {
349334
this[kPublicPort].on(event, (message) => this.emit(event, message));
350335
});
351336
setupPortReferencing(this[kPublicPort], this, 'message');
337+
338+
// relay events from worker thread to watcher
339+
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
340+
this[kPublicPort].on('message', (message) => {
341+
if (ArrayIsArray(message?.['watch:require'])) {
342+
process.send({ 'watch:require': message['watch:require'] });
343+
}
344+
});
345+
}
352346
this[kPort].postMessage({
353347
argv,
354348
type: messageTypes.LOAD_SCRIPT,

0 commit comments

Comments
 (0)