1 |
|
2 | 'use strict';
|
3 | const ipc = require('./ipc');
|
4 |
|
5 | const seenDependencies = new Set();
|
6 | let newDependencies = [];
|
7 | function flush() {
|
8 | if (newDependencies.length === 0) {
|
9 | return;
|
10 | }
|
11 |
|
12 | ipc.send({type: 'dependencies', dependencies: newDependencies});
|
13 | newDependencies = [];
|
14 | }
|
15 |
|
16 | exports.flush = flush;
|
17 |
|
18 | function 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 |
|
31 | exports.track = track;
|
32 |
|
33 | function 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 |
|
47 | exports.install = install;
|