diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc2b62f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +*.pid +node_modules +pids diff --git a/index.js b/index.js new file mode 100644 index 0000000..842992d --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = ('at', require('./lib/yabble')) diff --git a/lib/yabble.js b/lib/yabble.js index 5d1751d..96b1d81 100644 --- a/lib/yabble.js +++ b/lib/yabble.js @@ -38,6 +38,7 @@ _timeoutLength = 20000, _mainProgram; + var head = document.getElementsByTagName('head')[0]; @@ -130,7 +131,10 @@ // Takes a module's ID and resolves a URI according to the module root path var resolveModuleUri = function(moduleId) { - if (moduleId.charAt(0) != '.') { + if (/^http(s?):\/\//.test(moduleId)) { + return moduleId+'.js'; + } + else if (moduleId.charAt(0) != '.') { return _moduleRoot+moduleId+'.js'; } else { @@ -183,17 +187,17 @@ // relative require()s var createRequireFunc = function(refPath) { var require = function(relModuleId) { - var moduleId = resolveModuleId(relModuleId, refPath), - module = getModule(moduleId); + var moduleId = resolveModuleId(relModuleId, refPath); + var module = getModule(moduleId); if (!module) { throw "Module not loaded"; } - else if (module.error) { + /*else if (module.error) { throw "Error loading module"; - } + }*/ - if (!module.exports) { + if (!module.exports && !module.error) { module.exports = {}; var moduleDir = moduleId.substring(0, moduleId.lastIndexOf('/')+1), injects = module.injects, @@ -207,7 +211,7 @@ args.push(module.exports); } else if (injects[i] == 'module') { - args.push(module.module); + args.push(module); } } @@ -398,6 +402,8 @@ fireCallbacks(); } } + + // head.removeChild(scriptEl); }; if (useStandard) { @@ -453,9 +459,17 @@ // Set the uri which forms the conceptual module namespace root Yabble.setModuleRoot = function(path) { if (!(/^http(s?):\/\//.test(path))) { - var href = window.location.href; - href = href.substr(0, href.lastIndexOf('/')+1); - path = combinePaths(path, href); + var href = location.protocol+'//'+location.host; + if (path.charAt(0) !== '/') { + href += location.pathname; + path = combinePaths( + path, + href + ); + } + else { + path = href+path; + } } if (path.length && path.charAt(path.length-1) != '/') { @@ -558,9 +572,11 @@ }; Yabble.reset(); + + Yabble._modules = _modules; // Export to the require global window.require = Yabble; })(function(code) { return (window.eval || eval)(code, null); -}); \ No newline at end of file +}); diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..c0092a4 --- /dev/null +++ b/test/server.js @@ -0,0 +1,17 @@ +/* +This is a small node.js server, so that you can test Yabble on your OS without using the file:/// protocol. +You need to +- install node.js (nodejs.org) +- install node package manager (npmjs.org) +- Type: npm install connect +- Type: node server.js ROOTDIR & +- Go to http://localhost:3000/index.html +*/ + +var connect = require('connect') + +dir = process.argv[2] + +connect( + connect.static(dir) +).listen(3000)