Skip to content

Commit

Permalink
Upgraded to follow latest AMD proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Oct 25, 2010
1 parent 9680b70 commit 87fc3d6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ with any client side module loader that supports the CommonJS module transport
format. This has been primarily tested with RequireJS and Yabble:

[http://github.com/jrburke/requirejs](http://github.com/jrburke/requirejs)
[http://github.com/kriszyp/yabble](http://github.com/kriszyp/yabble) (currently the kriszyp fork is needed because of some implementation bugs in require.def handling in Yabble)
[http://github.com/kriszyp/yabble](http://github.com/kriszyp/yabble) (currently the kriszyp fork is needed because of some implementation bugs in define handling in Yabble)

To use transporter, include the appliance in your JSGI stack:

Expand Down
17 changes: 7 additions & 10 deletions lib/jsgi/transporter.js
@@ -1,7 +1,5 @@
//TODO: optionally write to target file

require.def||(require.def=function(factory){module.exports=factory(require, exports, module);});
require.def(function(require, exports, module){
exports = module.exports ? Transporter : exports;
exports.Transporter = Transporter;
function Transporter(options, app) {
Expand Down Expand Up @@ -85,7 +83,7 @@ function Transporter(options, app) {
}
errors.forEach(write);
if(requestedModules.length > 1){
write('require.def("' + requestedModules.join(",") + '", [], function(){});');
write('define("' + requestedModules.join(",") + '", [], function(){});');
}
if(autoRun && requestedModules.length){
write('require.ensure&&require.ensure(["' + requestedModules.join(",") + '"], function(require){' +
Expand Down Expand Up @@ -217,10 +215,10 @@ exports.CommonJS = function(moduleName, fileContents, loadModule, modules, write
var depNames = {};
var baseModule = moduleName.substring(0, moduleName.lastIndexOf("/") + 1);
var needsStaticAnalysis = true;
var firstRequireDef = fileContents.indexOf("require.def(");
var firstRequireDef = fileContents.indexOf("define(");
if(firstRequireDef > -1){
// in AMD form
fileContents = fileContents.replace(/require\.def\(\s*(?:['"]([^'"]*)['"]\s*,)?\s*(?:(\[[^\]]*\]\s*),)?/,function(t, id, depsJson){
fileContents = fileContents.replace(/define\(\s*(?:['"]([^'"]*)['"]\s*,)?\s*(?:(\[[^\]]*\]\s*),)?/,function(t, id, depsJson){
if(depsJson){
JSON.parse(depsJson).forEach(onDependency);
}else if(!id){
Expand All @@ -234,7 +232,7 @@ exports.CommonJS = function(moduleName, fileContents, loadModule, modules, write
if(!depsJson && !id){// && deps.length){
deps = ["require", "exports", "module"].concat(deps);
}
return 'require.def("' + moduleName + '",' + (deps.length ? '["' + deps.join('", "') + '"],' : '');
return 'define("' + moduleName + '",' + (deps.length ? '["' + deps.join('", "') + '"],' : '');
});
}else{
staticAnalysis();
Expand Down Expand Up @@ -279,7 +277,7 @@ exports.CommonJS = function(moduleName, fileContents, loadModule, modules, write
}
if(write){
if(firstRequireDef == -1){
write('require.def("');
write('define("');
write(moduleName);
write('", ["require", "exports", "module"');
write((deps.length ? ', "' + deps.join('", "') + '"' : '') + '], ');
Expand Down Expand Up @@ -333,7 +331,7 @@ exports.DojoRequireJS = function(moduleName, fileContents, loadModule, modules,
}
});
if(write){
write('require.def("');
write('define("');
write(moduleName);
write('", [');
write((deps.length ? '"' + deps.join('", "') + '"' : '') + '], ');
Expand All @@ -342,5 +340,4 @@ exports.DojoRequireJS = function(moduleName, fileContents, loadModule, modules,
write('\n});\n');
}
};
return exports;
});
module.exports = exports;
17 changes: 9 additions & 8 deletions lib/receiver.js
Expand Up @@ -48,17 +48,18 @@
}
return modules[id];
}
define = function(id, deps, factory){
if(typeof deps == "function"){
factories[id] = deps;
}else{
(factories[id] = factory).deps = deps;
}
};

require = {
def: function(id, deps, factory){
if(typeof deps == "function"){
factories[id] = deps;
}else{
(factories[id] = factory).deps = deps;
}
},
ensure: function(modules, callback){
for(var i = 0; i < modules.length; i++){
req(modules[i]);
modules = req(modules[i]);
}
callback(req);
}
Expand Down

0 comments on commit 87fc3d6

Please sign in to comment.