Permalink
Browse files

Add support for transport/C and D modules

  • Loading branch information...
1 parent 26917b1 commit 7c0453f852a38303911937ddf420ffeee5cb6dc2 @kriszyp committed Aug 24, 2010
Showing with 41 additions and 11 deletions.
  1. +41 −11 lib/nodules.js
View
52 lib/nodules.js
@@ -186,16 +186,18 @@ function resolveUri(currentId, uri, mappings){
return uri;
}
}
-function getPackage(uri){
+function getPackageUri(uri){
if(uri.substring(0,4) == "jar:"){
// if it is an archive, the root should be the package URI
- uri = uri.substring(0, uri.lastIndexOf('!') + 2);
+ return uri.substring(0, uri.lastIndexOf('!') + 2);
}
else{
// else try to base it on the path
- uri = uri.substring(0, uri.lastIndexOf('/lib/') + 1);
+ return uri.substring(0, uri.lastIndexOf('/lib/') + 1);
}
- return packages[uri] || {mappings: packages[""].mappings};
+}
+function getPackage(uri){
+ return packages[getPackageUri(uri)] || {mappings: packages[""].mappings};
}
function makeWorker(Constructor, currentId){
return Constructor && function(script, name){
@@ -213,14 +215,22 @@ function makeRequire(currentId){
return moduleExports[uri];
}
if(factories[uri]){
- var exports = moduleExports[uri] = {};
try{
- var module = modules[uri] = modules[uri] || new Module(uri);
+ var exports = moduleExports[uri] = {},
+ module = modules[uri] = modules[uri] || new Module(uri),
+ currentFile = cachePath(uri),
+ factory = factories[uri],
+ nextRequire = makeRequire(uri);
module.dependents[currentId] = true;
- var currentFile = cachePath(uri);
- exports = factories[uri].call(exports, makeRequire(uri), exports, module,
+ exports = factory.call(exports, nextRequire, exports, module,
currentFile, currentFile.replace(/\/[^\/]*$/,''))
|| exports;
+ if(factory != factories[uri]){
+ // if a module was wrapped with the transport/D than the factory will get replaced
+ exports = factories[uri].call(exports, nextRequire, exports, module,
+ currentFile, currentFile.replace(/\/[^\/]*$/,''))
+ || exports;
+ }
var successful = true;
}
finally{
@@ -247,12 +257,32 @@ function makeRequire(currentId){
};
require.main = main;
require.define = function(moduleSet, dependencies){
- require.ensure(dependencies);
+ if(dependencies){
+ require.ensure(dependencies);
+ }
+ var context = getPackageUri(currentId) + "lib/";
for(var i in moduleSet){
- // TODO: Verify that id is an acceptably defined by the requested URL (shouldn't allow cross-domain definitions)
- factories[i] = moduleSet[i];
+ var moduleDef = moduleSet[i];
+ factories[context + i + ".js"] = moduleDef.factory || moduleDef;
}
};
+ require.def = function(id, dependencies, factory){
+ if(dependencies){
+ require.ensure(dependencies);
+ }else{
+ factory = dependencies;
+ }
+ factories[getPackageUri(currentId) + "lib/" + id + ".js"] = function(require, exports, module){
+ return factory.apply(exports, dependencies ? dependencies.map(function(id){
+ switch(id){
+ case "require": return require;
+ case "exports" : return exports;
+ case "module" : return module;
+ default: return require(id);
+ }
+ }) : []);
+ };
+ };
require.paths = paths;
require.reloadable = reloadable;
require.resource = function(uri){

0 comments on commit 7c0453f

Please sign in to comment.