forked from raadad/node-din
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdin.js
61 lines (46 loc) · 3 KB
/
din.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module.exports = function(config) {
var Din = function() {
var self = this,
fs = require('fs'),
prefixDir = config.baseDir ? config.baseDir : process.cwd();
if (!process._dinSingletons) process._dinSingletons = {};
self.config = config;
self.resolve = function(path, parentModulePaths) {
var parentModulePathsCopy = parentModulePaths.map(function(item) { return item; }),
basePath = (parentModulePaths && parentModulePaths.length) ? parentModulePathsCopy.pop() : prefixDir;
if (fs.existsSync(basePath + '/' + path)) return basePath + '/' + path;
if (fs.existsSync(basePath + '/' + path + '.js')) return basePath + '/' + path + '.js';
if (fs.existsSync(basePath + '/' + path + '.es6')) return basePath + '/' + path + '.es6';
if (fs.existsSync(basePath + '/node_modules/' + path )) return basePath + '/node_modules/' + path;
if (parentModulePaths.length) return self.resolve(path, parentModulePathsCopy);
return path;
};
self.load = function load(alias, parentModulePaths) {
if (!parentModulePaths) parentModulePaths = [];
if (typeof alias !== "string") return alias; //support for non lookup dependencies
if (alias.indexOf('s:') !== -1) return alias.split(':')[1]; // support for string based dependencies
if (alias === '_din') return self;
var moduleDescriptor = config.graph[alias]; // Load module from graph
if(moduleDescriptor && moduleDescriptor.lookup && config.graph[moduleDescriptor.lookup])
return self.load(moduleDescriptor.lookup, parentModulePaths); // recursivly lookup dependencies
if (process._dinSingletons[alias]) return process._dinSingletons[alias]; // support for singletons
if (!moduleDescriptor) moduleDescriptor = {}; // handle case where module is not defined in graph
if (moduleDescriptor.lookup) moduleDescriptor._lookup = moduleDescriptor.lookup; // lookup is used for two things, this is a work around to not make breaking API change
if (!moduleDescriptor._lookup) moduleDescriptor._lookup = alias; // as above
var moduleLocation = self.resolve(moduleDescriptor._lookup, parentModulePaths), //find module
loadedModule = require(moduleLocation); // load module
//recursivly load dependencies for module
if (moduleDescriptor.deps) {
parentModulePaths.push(moduleLocation);
var dependencies = moduleDescriptor.deps.map(function(item) {
return self.load(item, parentModulePaths);
});
loadedModule = loadedModule.apply(null, dependencies); // inject dependencies
if (moduleDescriptor.singleton) process._dinSingletons[alias] = loadedModule; // add to singleton store
}
return loadedModule;
};
return self;
};
return new Din();
};