Permalink
Browse files

Upgraded to latest AMD proposal, fixed file->fs reference

  • Loading branch information...
1 parent 0f6287d commit 7373562ec9d9acd4529d093447a70b81270f105b @kriszyp committed Oct 25, 2010
Showing with 72 additions and 9 deletions.
  1. +1 −1 README.md
  2. +71 −8 lib/nodules.js
View
@@ -5,7 +5,7 @@ and automatically
analyzes modules references and downloads any dependencies on first access prior to
executing modules. Remotely downloaded modules are retained so they only need
to be downloaded once. Nodules supports standard CommonJS modules,
-and CommonJS module transport format via require, require.ensure, [require.def](http://wiki.commonjs.org/wiki/Modules/Transport/C), and [require.define](http://wiki.commonjs.org/wiki/Modules/Transport/D).
+and CommonJS module transport format via require, require.ensure, [define](http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition), and [require.define](http://wiki.commonjs.org/wiki/Modules/Transport/D).
@dvv

dvv Oct 25, 2010

Is it compatible to RequireJS?

To see Nodules in action right away, go into the "example" directory, and you can start
the example "package" with:
View
@@ -24,6 +24,8 @@ var modules = {},
monitored = [],
overlays = {},
callbacks = [],
+ currentModule,
+ currentRequire,
useSetInterval = false,
monitorModules = true,
packages = {},
@@ -127,6 +129,56 @@ function EnginePackage(engine){
};
}
+define = function (id, injects, factory) {
+ if (currentModule == null) {
+ throw new Error("define() may only be called during module factory instantiation");
+ }
+ var module = currentModule;
+ var require = currentRequire;
+ if (!factory) {
+ // two or less arguments
+ factory = injects;
+ if (factory) {
+ // two args
+ if (typeof id === "string") {
+ if (id !== module.id) {
+ throw new Error("Can not assign module to a different id than the current file");
+ }
+ // default injects
+ injects = ["require", "exports", "module"];
+ }
+ else{
+ // anonymous, deps included
+ injects = id;
+ }
+ }
+ else {
+ // only one arg, just the factory
+ factory = id;
+ injects = ["require", "exports", "module"];
+ }
+ }
+ if (typeof factory !== "function"){
+ // we can just provide a plain object
+ return module.exports = factory;
+ }
+ var returned = factory.apply(module.exports, injects.map(function (injection) {
+ switch (injection) {
+ // check for CommonJS injection variables
+ case "require": return require;
+ case "exports": return module.exports;
+ case "module": return module;
+ default:
+ // a module dependency
+ return require(injection);
+ }
+ }));
+ if(returned){
+ // since AMD encapsulates a function/callback, it can allow the factory to return the exports.
+ module.exports = returned;
+ }
+};
+
packages[""] = exports;
exports.mappings = [];
exports.mappings.defaultPath = "";
@@ -248,11 +300,11 @@ function makeRequire(currentId){
if(factories[uri]){
try{
var exports = moduleExports[uri] = {},
- module = modules[uri] = modules[uri] || new Module(uri),
+ module = currentModule = modules[uri] = modules[uri] || new Module(uri),
currentFile = cachePath(uri),
factory = factories[uri],
originalExports = module.exports = exports,
- nextRequire = makeRequire(uri);
+ nextRequire = currentRequire = makeRequire(uri);
module.dependents[currentId] = true;
exports = factory.call(exports, nextRequire, exports, module,
currentFile, currentFile.replace(/\/[^\/]*$/,''))
@@ -271,6 +323,8 @@ function makeRequire(currentId){
var successful = true;
}
finally{
+ currentRequire = null;
+ currentModule = null;
if(!successful){
delete moduleExports[uri];
}
@@ -306,7 +360,8 @@ function makeRequire(currentId){
factories[context + i + ".js"] = moduleDef.factory || moduleDef;
}
};
- require.def = function(id, dependencies, factory){
+ require.def = define;
+/* require.def = function(id, dependencies, factory){
if(dependencies){
require.ensure(dependencies);
}else{
@@ -322,7 +377,7 @@ function makeRequire(currentId){
}
}) : []);
};
- };
+ };*/
require.paths = paths;
require.reloadable = reloadable;
require.resource = function(uri){
@@ -544,6 +599,15 @@ exports.load = function(uri, require){
require.ensure(moduleId);
}
});
+ source.replace(/define\s*\(\s*(\[(?:['"][^'"]*['"],?)+\])\s*\)/, function(t, deps){
+ deps = JSON.parse(deps);
+ if(require){
+ deps.forEach(function(moduleId){
+ require.ensure(moduleId);
+ });
+ }
+ });
+
if(packageData.compiler){
require.ensure(packageData.compiler.module);
}
@@ -552,11 +616,10 @@ exports.load = function(uri, require){
});
});
};
-
function createFactory(uri, source){
try{
factories[uri] = compile("function(require, exports, module, __filename, __dirname, Worker, SharedWorker){" + source + "\n;return exports;}", uri);
- var indexOfExport, indexOfRequireDef = source.indexOf("require.def");
+/* var indexOfExport, indexOfRequireDef = source.indexOf("define");
if(indexOfRequireDef > -1 && ((indexOfExport = source.indexOf("exports.")) == -1 || indexOfExport > indexOfRequireDef)){
// looks like it is an Aynchronous module definition module
factories[uri]({def: function(id, dependencies, factory){
@@ -594,7 +657,7 @@ function createFactory(uri, source){
}) : arguments);
};
}});
- }
+ }*/
}catch(e){
factories[uri] = function(){
throw new Error(e.stack + " in " + uri);
@@ -791,7 +854,7 @@ function readModuleFile(path, uri){
if(path.match(/\.js$/) && typeof process != "undefined"){
path = path.replace(/\.js$/,".node");
try{
- file.read(path);
+ fs.read(path);
return 'process.dlopen("' + path + '", exports);';
}
catch(nodeE){

0 comments on commit 7373562

Please sign in to comment.