Custom Require
With this module you can receive a callback for any non-native dependecies an specific module loads, including the dependencies of his dependencies, and so on.
To check for module and dependencies modifications, see https://github.com/Llorx/watcher-require
To always receive an updated version of your modules, checking files and dependencies modifications, see https://github.com/Llorx/updated-require
Installation
npm install custom-require
Usage
require("react");
module.exports = "yay";
require("react");
require("redux");
var CustomRequire = require("custom-require").CustomRequire;
import { CustomRequire } from "custom-require";
var firstWalker = new CustomRequire(function(module) {
console.log("First walker", module.filename);
});
firstWalker.require("./test");
firstWalker.require("./test2");
var secondWalker = new CustomRequire(function(module) {
console.log("Second walker", module.filename);
});
var yay = secondWalker.require("./test");
console.log(yay);
firstWalker.dispose();
secondWalker.dispose();
Also, it works with asynchronous requires
require("react");
setTimeout(function() {
require("redux");
}, 1000);
Limitations
As the nature of Node.js, required modules are cached, so doing this will not work as expected:
require("./test");
var CustomRequire = require("custom-require").CustomRequire;
var walker = new CustomRequire(function(module) {
console.log("Walker", module.filename);
});
walker.require("./test2");
Custom Require will be only able to track modules loaded after it has been required for the first time, so is recommendable to require it at the top of the entry-point file. Is not needed to add it to each file. Only at the entry-point.
NOTE: Custom Require will start tracking right after requiring it. Is not necessary to immediately create an instance.