Comparing version 0.0.21 to 0.0.22
'use strict'; | ||
Error.stackTraceLimit = 15; | ||
var _ = require('lodash'); | ||
@@ -12,3 +14,9 @@ var path = require('path'); | ||
function computeArgs(optName, replace, module) { | ||
var deps = annotate(module); | ||
var deps = module; | ||
if (_.isFunction(deps)) { | ||
deps = annotate(module); | ||
} | ||
else if (!_.isArray(module)) { | ||
deps = [ module ]; | ||
} | ||
@@ -45,7 +53,13 @@ deps = _.map(deps, function (dep) { | ||
if (!_.isFunction(module)) { | ||
throw new Error('[r42] Module argument should be a valid function.'); | ||
if (_.isFunction(module)) { | ||
return module.apply(global, computeArgs(optName, replace, module)); | ||
} | ||
return module.apply(global, computeArgs(optName, replace, module)) | ||
var res = computeArgs(optName, replace, module); | ||
if (!_.isArray(module)) { | ||
res = res[0]; | ||
} | ||
return res; | ||
} | ||
@@ -59,2 +73,6 @@ | ||
if (!_.isFunction(module)) { | ||
throw new Error('[r42] Define\'s module should be provided as a function.'); | ||
} | ||
mc.addModule(name, inject(name, replace, module)); | ||
@@ -64,3 +82,3 @@ } | ||
module.exports = { | ||
inject: _.createCallback(_.partial(inject, null), null, 2), | ||
inject: _.partial(inject, null), | ||
config: function (config) { | ||
@@ -67,0 +85,0 @@ mc = new ModuleContext(config, define); |
@@ -66,4 +66,3 @@ 'use strict'; | ||
catch(e) { | ||
console.error('Cannot load module ' + name + ' (required by module ' + callee + ')'); | ||
console.error(e.stack); | ||
console.error('Cannot load module ' + name + ' (required by module ' + (callee || '<root>') + ').'); | ||
throw e; | ||
@@ -83,3 +82,3 @@ } | ||
throw new Error('[r42] ModuleContext: define must be called only once ' + | ||
'per module but was called twice in ' + name); | ||
'per module but was called twice in ' + name + '.'); | ||
} | ||
@@ -94,3 +93,3 @@ defineCalled = true; | ||
if (!defineCalled) { | ||
throw new Error('[r42] ModuleContext: define not called by module ' + name); | ||
throw new Error('[r42] ModuleContext: define not called by module ' + name + '.'); | ||
} | ||
@@ -97,0 +96,0 @@ |
{ | ||
"name": "r42", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"description": "Dependency injection done right.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -98,2 +98,20 @@ ## r42 | ||
##### Modules in the same folder | ||
Sometimes, it is a pain to refer to a module in the same folder as you are right now. r42 allows for a fine way to do so. | ||
Using `$` to prefix your variable's name will automatically cause r42 to replace it by your current module's "path". It also works to prefix files in the `replacer` object. | ||
In a module `module/toto.js`: | ||
```js | ||
define({ | ||
superSub: '$super/sub', | ||
}, function (superSub, $sideFn, /*! $sub/sideValue */ sideVal) { | ||
// superSub refers to 'module/super/sub' | ||
// $sideFn refers to module/sideFn | ||
// sideVal refers to $sub/sideValue | ||
}); | ||
``` | ||
##### Special case : index.js in a subfolder | ||
@@ -121,20 +139,2 @@ | ||
##### Modules in the same folder | ||
Sometimes, it is a pain to refer to a module in the same folder as you are right now. r42 allows for a fine way to do so. | ||
Using `$` to prefix your variable's name will automatically cause r42 to replace it by your current module's "path". It also works to prefix files in the `replacer` object. | ||
In a module `module/toto.js`: | ||
```js | ||
define({ | ||
superSub: '$super/sub', | ||
}, function (superSub, $sideFn, /*! $sub/sideValue */ sideVal) { | ||
// superSub refers to 'module/super/sub' | ||
// $sideFn refers to module/sideFn | ||
// sideVal refers to $sub/sideValue | ||
}); | ||
``` | ||
##### Circular dependencies | ||
@@ -178,2 +178,25 @@ | ||
##### Loading modules dynamically | ||
You can also use r42.inject to load modules dynamically. In this case, provide a module name or module list and optionally a `replacer` as usual. Here is what it could look like: | ||
In module `dynamic.js` | ||
```js | ||
define(function (r42) { | ||
// This will load lodash in variable _. | ||
var _ = r42.inject('lodash'); | ||
// Same thing for the variable alias but using the replacer argument. | ||
var alias = r42.inject({ alias: 'lodash' }, 'alias'); | ||
// An example with multiple load at once | ||
// Here modules[0] will resolve to require('util') | ||
// and modules[1] to require('net') | ||
var modules = r42.inject(['util', 'net']); | ||
// You can also use the replacer argument with an array: | ||
// this example will load the same modules as the previous one | ||
modules = r42.inject({a: 'util', b: 'net'}, ['a', 'b']); | ||
}); | ||
``` | ||
##### Printing dependencies | ||
@@ -180,0 +203,0 @@ |
13762
233
235