This package is useful when watching file changes and hot-reload modules. It helps us retrieve all the dependency or the dependent files that related to the changed file, and we can reload them all at once.
import { findDependents, findDependencies } from "require-chain";
import * as path from "path";
const filename = path.join(__dirname, "test", "foo.js");
const dependents = findDependents(filename);
console.log(dependents);
// [...] filenames that rely on the given file
const dependencies = findDependencies(filename);
console.log(dependencies);
// [...] filenames that imported by the given file
findDependents(filename[, includes])
Finds all the CommonJS files and their ancestors that require the filename
.
filename: string | string[]
The absolute path of the file (or files).includes: string[] | ((files: string[]) => string[])
By default, the function searches every cached file except the ones innode_modules
and therequire.main.filename
. We can provide this argument to set more specific files that can be searched.
findDependencies(filename[, includes])
Finds all the CommonJS files and their descendants that required by the
filename
.
filename: string | string[]
The absolute path of the file (or files).includes: string[] | ((files: string[]) => string[])
By default, the function searches every cached file except the ones innode_modules
and therequire.main.filename
. We can provide this argument to set more specific files that can be searched.
When a module (CommonJS file) is required, the module.parent
property is
assigned to the first dependent module (AKA the initiator). However, there may
be many dependent files that require the same module, so reversing the
module.parent
to find its ancestors cannot retrieve all the dependents.
module.children
, on the other hand, stores all the child modules that the
current module requires, which can be used to scan for the dependency map
through the require.cache
and retrieve all the related files.
And of course, this package use module.children
under the hood.