Basename namesapce option for modules
This commit is contained in:
@@ -42,10 +42,11 @@ The modular constructor accept several settings :
|
||||
path : [ 'app/module.js', 'app/module2.js', 'app/subfolder' ]
|
||||
override : true, /* modules override already loaded modules in case of collision
|
||||
default value : false */
|
||||
namespace : 'namespace', /* namespace is appended before the deduced namespace */
|
||||
namespace : 'namespace', /* specify the namespace of the module */
|
||||
baseNamespace : 'base', /* base namespace appended to module namespace */
|
||||
basePath : 'path/path2', /* the base path used to deduce the namespace of modules
|
||||
default value : path value (when path is a directory) */
|
||||
fullName : 'namespace.name',/* specify the full name of the module
|
||||
name : 'name', /* specify the name of the module
|
||||
Only when path is one file. Doesn't work with namespace setting */
|
||||
liveReload : true, /* enable or disable liveReload on a file/folder basis
|
||||
default value : false */
|
||||
|
||||
@@ -86,6 +86,9 @@ function Modular (options) {
|
||||
|
||||
//Stop the application
|
||||
this.stop = function () {
|
||||
//TODO Stop watchers if there's any
|
||||
|
||||
|
||||
return instance.emit('stop', get.bind(undefined, undefined));
|
||||
};
|
||||
|
||||
@@ -114,18 +117,18 @@ function Modular (options) {
|
||||
if (instance.hasListener('log')) {
|
||||
instance.emit('log', get.bind(undefined, undefined), message);
|
||||
}
|
||||
else {
|
||||
else {
|
||||
console.log('['+level+']', message);
|
||||
|
||||
if (level === 'error') {
|
||||
if (message instanceof Error) {
|
||||
throw message;
|
||||
throw message;
|
||||
}
|
||||
else if (message) {
|
||||
throw new Error(message);
|
||||
throw new Error(message);
|
||||
}
|
||||
else {
|
||||
throw new Error();
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,13 +139,13 @@ function Modular (options) {
|
||||
var mod;
|
||||
|
||||
//Search for the module in caller namespace first
|
||||
if (caller && modules.has (caller.namespace && caller.namespace.length > 0
|
||||
? caller.namespace + '.' + name
|
||||
if (caller && modules.has (caller.namespace && caller.namespace.length > 0
|
||||
? caller.namespace + '.' + name
|
||||
: name)) {
|
||||
mod = modules.get (
|
||||
caller.namespace && caller.namespace.length > 0
|
||||
? caller.namespace + '.' + name
|
||||
: name);
|
||||
caller.namespace && caller.namespace.length > 0
|
||||
? caller.namespace + '.' + name
|
||||
: name);
|
||||
}
|
||||
else if (modules.has(name)) {
|
||||
mod = modules.get(name);
|
||||
@@ -193,7 +196,7 @@ function Modular (options) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Register a new module
|
||||
//file : path of the module
|
||||
//name (optional) : override the full name of the module (namespace and name)
|
||||
@@ -223,13 +226,15 @@ function Modular (options) {
|
||||
}
|
||||
|
||||
//Name parameter override module namespace and module name
|
||||
if (opts.namespace === undefined && opts.name === undefined) {
|
||||
if (opts.namespace === undefined) {
|
||||
//Every folder of the module path is a part of the namespace
|
||||
opts.namespace = path.relative((opts.basePath || settings.basePath) , path.dirname(opts.fullPath))
|
||||
.replace(/\./g,'')
|
||||
.replace(/\//g,'.')
|
||||
.replace(/^\.*/g,'');
|
||||
}
|
||||
|
||||
if (opts.name === undefined) {
|
||||
//Name is the basename of the module
|
||||
opts.name = path.basename(opts.fullPath, autoload ? '.autoload.js' : path.extname(opts.fullPath));
|
||||
|
||||
@@ -237,11 +242,11 @@ function Modular (options) {
|
||||
if (opts.name.indexOf('.') >= 0) {
|
||||
opts.name = opts.name.replace(/\./g,'_');
|
||||
}
|
||||
}
|
||||
|
||||
//Adding the base namespace
|
||||
if (opts.baseNamespace !== undefined) {
|
||||
opts.namespace = opts.baseNamespace.replace('/\./g','_') + '.' + opts.namespace;
|
||||
}
|
||||
//Adding the base namespace
|
||||
if (opts.baseNamespace !== undefined) {
|
||||
opts.namespace = opts.baseNamespace.replace('/\./g','_') + '.' + opts.namespace;
|
||||
}
|
||||
|
||||
//Calling the module loading hook
|
||||
@@ -250,7 +255,7 @@ function Modular (options) {
|
||||
}
|
||||
|
||||
var fullName = opts.namespace && opts.namespace.length > 0 ? opts.namespace + '.' + opts.name : opts.name;
|
||||
|
||||
|
||||
var alreadyLoaded = modules.has(fullName);
|
||||
|
||||
if (!alreadyLoaded || opts.override) {
|
||||
@@ -335,13 +340,19 @@ function Modular (options) {
|
||||
var fullPath = path.join(filePath, file);
|
||||
var stats = fs.statSync(fullPath);
|
||||
|
||||
let opts = clone(options);
|
||||
if (stats.isFile && path.extname(fullPath) === '.js') {
|
||||
let opts = clone(options);
|
||||
opts.path = fullPath;
|
||||
|
||||
register(opts);
|
||||
}
|
||||
else if (stats.isDirectory()) {
|
||||
if (opts.namespace !== undefined) {
|
||||
opts.namespace = opts.namespace + '.' + file;
|
||||
}
|
||||
if (opts.name) {
|
||||
opts.name = undefined;
|
||||
}
|
||||
loadFolder(fullPath, options);
|
||||
}
|
||||
}
|
||||
|
||||
9
test/livereload/base.js
Normal file
9
test/livereload/base.js
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function() {
|
||||
return {
|
||||
run : function () {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user