Simple dependency injection and library loader.
Simple dependency injection and library loader for node.
Rely allows you to depend on named libraries which can be overriden in the configuration. The default options allow rely to fall back to node's
require() so that everything uses the default implementations unless overriden.
var rely = require'rely'options;var some_library = rely'some_library'; // get an implementationrely'other_library' implementation; // set an implementation
require() rely once in your main script, then use it to load all other dependencies. This way, all other modules share a common container (dependency configuration). Rely can load modules that have not been designed to use rely, but obviously the dependencies for those modules cannot be overriden.
Rely also supports automatic dependency injection for modules designed to support this. Consider the following contrived example:
// depend on lodash, glob and pathreturnreturn lodashmapglobsyncpatternreturn pathbasenamef;;;;// mark the module as supporting rely auto-loadingmoduleexports$rely = true;
If this module is loaded with rely, the dependencies will be automatically satisfied according to the argument names. The module can also be loaded without rely, by specifying the arguments manually.
If the module will be minified (thus changing the argument names), or you would like to use different names for the dependencies, the dependencies can be specified explicitly as follows:
moduleexports$rely = 'lodash' 'glob' 'path';
The module wrapping function will only be called the first time the module is loaded by rely. It will then be cached for subsequent accesses.
Depdencies can be specified one at a time by calling
rely() with two arguments:
// set implementation to be provided for 'name'rely'name' implementation;
implementation is a string value, this maps a path or npm module name to be loaded the first time the dependency is relied on. E.g.:
// set a path to give to node's require() when awesomelib is loadedrely'awesomelib' './lib/awesomelib.js';// module is only loaded on first usevar awesome = rely'awesomelib';
You can also specify all dependencies at the same time by calling
relysetup'foo': foo'bar': bar;
Multiple calls to
setup() will add to the dependencies, not replace them. A key of
'*' will cause all files matching a glob to be loaded. The modules will be mapped to their basename, excluding the '.js' extension.
relysetup'*': './lib/*.js'// for foo.js and bar.js, will result in dependencies called 'foo' and 'bar'.;
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
See the CHANGELOG
Copyright (c) 2013 Gordon Mackenzie-Leigh
Licensed under the MIT license.