philidem / node-hot-reload

Utility code for watching for source file and changes and reloading modules
17 stars 1 forks source link

very little documentation #1

Open clux opened 11 years ago

clux commented 11 years ago

Hey, I've managed to get this module to work over at gu, but I'm unsure if my usage of your module is correct/sane.

In my case a one file change leads to 93 modules being uncached, is that expected? Any chance you could document some of the public methods a bit more?

clux commented 11 years ago

Where I am using your module (if you are interested)

-watch usage -afterReload usage

So afterReload, essentially try-catch requiring the all the watched files.

patrick-steele-idem commented 11 years ago

The documentation is lacking and needs to be improved. In the meantime, hopefully the following helps:

The directories that are watched are independent of which modules get cleared out of the module cache when a file is modified. The reason for this is that if one module is modified, all if its dependent modules will likely need to be reloaded as well because they will otherwise hold a reference to the old module. Also, there are a finite set of directories that can be watched (limited by the OS).

If you don't specify an "uncache" or "uncacheExclude" then, by default, all loaded modules will be uncached. If you want to limit which modules get reloaded you can do the following:

var reloader = hotReload.create(require);
files.forEach(function (f) {
  reloader.watch(path.join(scriptPath, f));
});

// Uncache only the modules in the "scriptPath" directory (recursive):
reloader.uncacheInclude(scriptPath, true /* recursive */); 

reloader
  .afterReload(this._reload.bind(this))
  .start();

When a module is uncached, it simply means that the next time it is "required", it will be loaded from disk. There's also an option to pro-actively reload an uncached module, but I don't use that feature.

I hope that helps.

--Patrick

clux commented 11 years ago

Thanks, that helps a lot. Adding that one debug line you commented out with which specific files were being uncached was helpful too for figuring out how the different methods work together.

Appreciate you taking the time to write this. Gotten it to work sensibly now.

--Eirik