Watcher Require
With this module you will receive a callback when a module or its dependencies are modified.
To receive a callback for any non-native dependecies an specific module loads, see https://github.com/Llorx/custom-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 watcher-require
If you want a lightweight installation, but less consistent as uses nodejs fs.watch()
instead of chokidar
library, install with:
npm install watcher-require --no-optional
Usage
/* FILE: test1.js */// Load any non-native module;
/* FILE: test2.js */// Load any non-native module;;
/* FILE: main.js */// Load the module at the top of the entry point filevar WatcherRequire = WatcherRequire; // If you are using TypeScript, you can use import; // Instantiate an object with a callback that will be called with all the new changesvar watcherRequire = { console; console;} delay: 300 // The callback will not be called instantly. You will receive one callback per each bunch of files changed persistent: true // Keep the process open, watching files; // Require a module to start watchingwatcherRequire; // You can also require another file to watchwatcherRequire; // You can create different Watcher Require instances together in the same scriptvar secondWatcher = { console;}; // Requiring modules already required by another instance will not be a problem// Second watcher will receive all the dependencies too// The require method will work as the default require one, returning the exports contentsvar yay = secondWatcher; // Yay itconsole; // After you have finished, call dispose() to clean resources attached to moduleswatcherRequire;secondWatcher;
Also, it works with asynchronous requires
/* FILE: async_test.js */// Load any non-native module;;
Limitations
See Custom Require limitations: https://github.com/Llorx/custom-require#limitations