cloader (NodeJS)
More user-friendly component loading to make the larger code-bases easier to manage and use.
What does it do
It calls require() whenever you need a package or script...
... and does some things before it does that.
Install
npm install cloader
Quick Start
// cloaderSetup.js in your project root
require('cloader')('load', __dirname+'/');
load.addLibrary('lib/');
load.addPackage('auth_api', 'packages/auth_api/index.js');
// app.js
require('cloaderSetup.js');
// everywhere
var User = load.c('models/User');
var authApi = load.p('auth_api');
Elaborate Setup
It's recommended to instantiate and configure the loader in a separate file located in your project root. This way you can simply require the file in every application that needs your loader (mocha's --require being a great example of the second application)
require('cloader')(name, root);
name The global name of your loader instance. In this document we use "load" but you can set it to anything you like. If you use this library in a package you plan on distributing you should use a non-generic name or only use it for the purpose of unit-testing.
root The loader resolves your scripts by using absolute paths. This should be the root directory of your project. There's no sanitation right now so make sure your directory ends with a trailing dash.
// Example using the script location as root, recommended.
require('cloader')('load', __dirname+'/');
load.addLibrary(path);
path The path to your library. Remember that you already added a root directory, so adding library paths should be done from that 'root' perspective.
// Example
load.addLibrary('lib/');
// This retrieves rootDirectory/lib/models/User.js
var User = load.c('models/User');
You can add multiple libraries. When trying to load a script, the loader will iterate over all libraries until it finds the script.
load.addPackage(name, path);
name The name you will be using when you want to retrieve the package.
path The directory path from the root to the package index. Note that you can also use '../../' to use a component located in an entirely different folder.
// Example virtual package
load.addPackage('auth_api', 'lib/auth_api/index');
// Now retrieve it
var authApi = load.p('auth_api');
Basically this introduces behavior seen in typical npm packages but then with your own 'virtual' packages.
Hypothetical Intermezzo
Say you want to move your user model into a separate package so you can share them between applications...
// Add this library to your loader
load.addLibrary('node_modules/SharedModels/lib/');
// It won't break the rest of your application as the loading works the same way
var User = load.c('models/User');
Remember that it is still strictly directory based right now, so keep them consistent if you want to move around your libraries. Virtual Directories are on the roadmap, those will take away some of that pain.
Usage
Already covered in the setup.
var User = load.c('models/User');
var VirtualPackage = load.p('MyVirtualPackage');
These also works but you probably won't be using them anyway...
var mongoose = load.p('mongoose');
var crypto = load.p('crypto');
... unless you turn your virtual package into a real package, in which case this will prevent your application from falling apart.
Roadmap to the future
Small stuff
- Sanitize and validate the directories including warnings
- In-memory caching + map-reduce algorithm for already loaded scripts.
- Experiment with more heavy abstraction (virtual directories etc...)
- Debug functionality
Plugins
require() and NPM have their limitations and impracticalities. They are also not likely to be changed.
This feature could help solve some of those (perceived) limitations, especially if it's as simple as using it like this...
// Write this line in your application
load.loadPlugins()
- This will iterate over all packages inside node_modules
- It will try to open a cloader.json file
- If available, will parse the file
- ... and automatically add the provided libraries and components
Now take this hypothetical example inside your application...
var MongoClient = load.c('mongodb/MongoClient');
This is of course a very simple use-case.
License
GPLv3