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));
}; };
@@ -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,12 +242,12 @@ 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
if (instance.hasListener('loadModule') && instance.emit('loadModule', opts) === false) { if (instance.hasListener('loadModule') && instance.emit('loadModule', opts) === false) {
@@ -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);
if (stats.isFile && path.extname(fullPath) === '.js') {
let opts = clone(options); let opts = clone(options);
if (stats.isFile && path.extname(fullPath) === '.js') {
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;
}
};
};