Basename namesapce option for modules

This commit is contained in:
2015-08-02 18:56:58 +02:00
parent 3cd2541f7f
commit 44b46d8f5f
3 changed files with 40 additions and 19 deletions

View File

@@ -42,10 +42,11 @@ The modular constructor accept several settings :
path : [ 'app/module.js', 'app/module2.js', 'app/subfolder' ] path : [ 'app/module.js', 'app/module2.js', 'app/subfolder' ]
override : true, /* modules override already loaded modules in case of collision override : true, /* modules override already loaded modules in case of collision
default value : false */ 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 basePath : 'path/path2', /* the base path used to deduce the namespace of modules
default value : path value (when path is a directory) */ 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 */ Only when path is one file. Doesn't work with namespace setting */
liveReload : true, /* enable or disable liveReload on a file/folder basis liveReload : true, /* enable or disable liveReload on a file/folder basis
default value : false */ default value : false */

View File

@@ -86,6 +86,9 @@ function Modular (options) {
//Stop the application //Stop the application
this.stop = function () { this.stop = function () {
//TODO Stop watchers if there's any
return instance.emit('stop', get.bind(undefined, undefined)); return instance.emit('stop', get.bind(undefined, undefined));
}; };
@@ -114,18 +117,18 @@ function Modular (options) {
if (instance.hasListener('log')) { if (instance.hasListener('log')) {
instance.emit('log', get.bind(undefined, undefined), message); instance.emit('log', get.bind(undefined, undefined), message);
} }
else { else {
console.log('['+level+']', message); console.log('['+level+']', message);
if (level === 'error') { if (level === 'error') {
if (message instanceof Error) { if (message instanceof Error) {
throw message; throw message;
} }
else if (message) { else if (message) {
throw new Error(message); throw new Error(message);
} }
else { else {
throw new Error(); throw new Error();
} }
} }
} }
@@ -136,13 +139,13 @@ function Modular (options) {
var mod; var mod;
//Search for the module in caller namespace first //Search for the module in caller namespace first
if (caller && modules.has (caller.namespace && caller.namespace.length > 0 if (caller && modules.has (caller.namespace && caller.namespace.length > 0
? caller.namespace + '.' + name ? caller.namespace + '.' + name
: name)) { : name)) {
mod = modules.get ( mod = modules.get (
caller.namespace && caller.namespace.length > 0 caller.namespace && caller.namespace.length > 0
? caller.namespace + '.' + name ? caller.namespace + '.' + name
: name); : name);
} }
else if (modules.has(name)) { else if (modules.has(name)) {
mod = modules.get(name); mod = modules.get(name);
@@ -193,7 +196,7 @@ function Modular (options) {
} }
} }
//Register a new module //Register a new module
//file : path of the module //file : path of the module
//name (optional) : override the full name of the module (namespace and name) //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 //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 //Every folder of the module path is a part of the namespace
opts.namespace = path.relative((opts.basePath || settings.basePath) , path.dirname(opts.fullPath)) opts.namespace = path.relative((opts.basePath || settings.basePath) , path.dirname(opts.fullPath))
.replace(/\./g,'') .replace(/\./g,'')
.replace(/\//g,'.') .replace(/\//g,'.')
.replace(/^\.*/g,''); .replace(/^\.*/g,'');
}
if (opts.name === undefined) {
//Name is the basename of the module //Name is the basename of the module
opts.name = path.basename(opts.fullPath, autoload ? '.autoload.js' : path.extname(opts.fullPath)); 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) { if (opts.name.indexOf('.') >= 0) {
opts.name = opts.name.replace(/\./g,'_'); opts.name = opts.name.replace(/\./g,'_');
} }
}
//Adding the base namespace //Adding the base namespace
if (opts.baseNamespace !== undefined) { if (opts.baseNamespace !== undefined) {
opts.namespace = opts.baseNamespace.replace('/\./g','_') + '.' + opts.namespace; opts.namespace = opts.baseNamespace.replace('/\./g','_') + '.' + opts.namespace;
}
} }
//Calling the module loading hook //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 fullName = opts.namespace && opts.namespace.length > 0 ? opts.namespace + '.' + opts.name : opts.name;
var alreadyLoaded = modules.has(fullName); var alreadyLoaded = modules.has(fullName);
if (!alreadyLoaded || opts.override) { if (!alreadyLoaded || opts.override) {
@@ -335,13 +340,19 @@ function Modular (options) {
var fullPath = path.join(filePath, file); var fullPath = path.join(filePath, file);
var stats = fs.statSync(fullPath); var stats = fs.statSync(fullPath);
let opts = clone(options);
if (stats.isFile && path.extname(fullPath) === '.js') { if (stats.isFile && path.extname(fullPath) === '.js') {
let opts = clone(options);
opts.path = fullPath; opts.path = fullPath;
register(opts); register(opts);
} }
else if (stats.isDirectory()) { else if (stats.isDirectory()) {
if (opts.namespace !== undefined) {
opts.namespace = opts.namespace + '.' + file;
}
if (opts.name) {
opts.name = undefined;
}
loadFolder(fullPath, options); loadFolder(fullPath, options);
} }
} }

9
test/livereload/base.js Normal file
View File

@@ -0,0 +1,9 @@
'use strict';
module.exports = function() {
return {
run : function () {
return 1;
}
};
};