mod-loader
Lite synchronous and asynchronous module loader to make a dynamic use of require on a specified directory.
Installation
npm install mod-loader --save
General
mod-loader is a node module that can be used to easyly include your own code digging directories using native node fs module.
Usage
mod-loader has to basic methods
loadModulesSync: ({ baseDirectory = null, moduleHolder = null, doNotInclude = [], namingStrategy = defaultNamingStrategy },loaderFunction)
Load modules in a synchronous way, this method receives two params
-
options: Options to perform the module loading;
- baseDirectory(String): Base directory where all the loading will begin
- moduleHolder(Object): Target Object, this object will receive every require(..) as a property according to the naming strategy
- doNotInclude(Array): An array of files or paths to be excluded
- namingStrategy(Function): A function used to name each property in moduleHolder object, the namingStrategy is a function which receives the loaded module full path, and the module itself. Inside this function you will have the freedom to determine which name the module will have over the holder, by default the naming strategy will use the file's name, for example Category.js will become Category, however you can implement whatever strategy is suitable by your use case.
-
loaderFunction: loaderFunction is the function used to involve require(..) calls you must return a require(module..) with your specific constructors for the module.
loadModulesSync: ({ baseDirectory = null, moduleHolder = null, doNotInclude = [], namingStrategy = defaultNamingStrategy },loaderFunction,loadDone)
Load modules in a asynchronous way, this method receives three params
-
options: Options to perform the module loading;
- baseDirectory(String): Base directory where all the loading will begin require(..) as a property according to the naming strategy
- doNotInclude(Array): An array of files or paths to be excluded
- namingStrategy(Function): A function used to name each property in moduleHolder object, the namingStrategy is a function which receives the loaded module full path, and the module itself. Inside this function you will have the freedom to determine which name the module will have over the holder, by default the naming strategy will use the file's name, for example Category.js will become Category, however you can implement whatever strategy is suitable by your use case.
-
loaderFunction: loaderFunction is the function used to involve require(..) calls you must return a require(module..) with your specific constructors for the module.
-
loadDone: Callback function which marks the loading process as done, it will receive as a parameter the object holder, in this case the holder is provided by the function itself.
Use cases
You can ask yourself, why would I need a f**ng module loader? here are some useful use cases
Sequelize's ORM boilerplate index
The main create this module was to reduce the boilerplate used to load sequelize's ORM models into a single db property, the example is as follows:
"use strict" module { const moduleLoader = ; const Sequelize = ; const sequelize = ...; const path = ; let db = {}; moduleLoader; Object; dbsequelize = sequelize; dbSequelize = Sequelize; return db;}
Express's middleware grouping
When developing express's applications is almost an imperative requirement to develop middlewares, those fancy functions which executes before, or after your http call. In some cases express applications may have a lot of middleware to be used so may come in handy to have every middleware available.
const moduleLoader = ;const path = ; let middleware = {}; moduleLoader; moduleexports = middleware;//in your further script you can use the middleware by just requiring the directory's indexconst middleware = ; console;//this object will hold all your middleware
Everything wich requires to collect a group of modules into a "holder"
If you found yourself in the need to use an object which collects modules from a directory, then mod-loader may be suitable for your needs, it does not require any additional node modules and uses native node's fs module
Tests
npm test