UNPKG

898 BJavaScriptView Raw
1/* eslint-disable node/no-deprecated-api */
2'use strict';
3const ipc = require('./ipc');
4
5const seenDependencies = new Set();
6let newDependencies = [];
7function flush() {
8 if (newDependencies.length === 0) {
9 return;
10 }
11
12 ipc.send({type: 'dependencies', dependencies: newDependencies});
13 newDependencies = [];
14}
15
16exports.flush = flush;
17
18function track(filename) {
19 if (seenDependencies.has(filename)) {
20 return;
21 }
22
23 if (newDependencies.length === 0) {
24 process.nextTick(flush);
25 }
26
27 seenDependencies.add(filename);
28 newDependencies.push(filename);
29}
30
31exports.track = track;
32
33function install(testPath) {
34 for (const ext of Object.keys(require.extensions)) {
35 const wrappedHandler = require.extensions[ext];
36
37 require.extensions[ext] = (module, filename) => {
38 if (filename !== testPath) {
39 track(filename);
40 }
41
42 wrappedHandler(module, filename);
43 };
44 }
45}
46
47exports.install = install;