require-loader
Watch the specified directory and dynamically add/remove module when there is any change. [require-loader] is local to the module.
Purpose
To allow dynamically add / remove module in a simple way. When new file is put into the directory, it uses "require()" to add module as usual, and it can be referenced in the [require-loader] map. When file is removed from the directory, it will be removed from require.cache and the [require-loader] map.
[require-loader] is local to the module implementing it. Therefore you can set up require-loader in 'test-a.js' and 'test-b.js' watching on different directories, and not interferring each other.
Installation
$ npm install require-loader
Quick Start
Assume you have a folder 'loader' you want to watch for adding/removing module, and you already have /loader/test-1.js , in your test.js file, you can:
/* test-.js */var rl = ; var t1 = rl'test-1.js';t1; // assume run() is export from test-1.js
Features & Options
You can pass in the following options when you require the 'require-loader':
var options = path : String /* The full path of the directory you want to watch, default to './' */ init : Boolean /* To run an initial load from the directory or not, default to false */ isWatch : Boolean /* To watch the directory or not. Default to true */ {} /* Call when any module is added or removed. It will data as: {action:String, filename:String, resolve_name:String, ref:Object }, action is either "add" or "remove", ref is the required Module just added. */
Example:
var rl = ; var { console; if dataaction == 'add' dataref; } // Set the absolute path of the directory to watch, and start initial function right away.var rmanager = rl; // get the mapvar map = rmanager; // check the map keys. for var m in map console; // test run it. var t1 = map'test-1.js';t1;
Use cases
One of the use cases is for loading multiple configs for different domains from a directory. See test example "hosts.js". It uses express.js and load different website "app" from the hosts-loader folder.
var express = ;var app = ;var RL = ; var { if dataaction == 'add' // data.ref is the reference of the app object. app; } // uses require-loader to watch hosts-loader folder and load the different apps to the main app server. var reqLoader = RL; // testing for output.var host1 = reqLoader'host1.js';host1; app;