Skip to content

Commit

Permalink
Upgraded to latest AMD proposal, fixed file->fs reference
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Oct 25, 2010
1 parent 0f6287d commit 7373562
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@ and automatically
analyzes modules references and downloads any dependencies on first access prior to analyzes modules references and downloads any dependencies on first access prior to
executing modules. Remotely downloaded modules are retained so they only need executing modules. Remotely downloaded modules are retained so they only need
to be downloaded once. Nodules supports standard CommonJS modules, 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).

This comment has been minimized.

Copy link
@dvv

dvv Oct 25, 2010

Is it compatible to RequireJS?

This comment has been minimized.

Copy link
@kriszyp

kriszyp Oct 25, 2010

Author Owner

Yes



To see Nodules in action right away, go into the "example" directory, and you can start To see Nodules in action right away, go into the "example" directory, and you can start
the example "package" with: the example "package" with:
Expand Down
79 changes: 71 additions & 8 deletions lib/nodules.js
Expand Up @@ -24,6 +24,8 @@ var modules = {},
monitored = [], monitored = [],
overlays = {}, overlays = {},
callbacks = [], callbacks = [],
currentModule,
currentRequire,
useSetInterval = false, useSetInterval = false,
monitorModules = true, monitorModules = true,
packages = {}, packages = {},
Expand Down Expand Up @@ -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; packages[""] = exports;
exports.mappings = []; exports.mappings = [];
exports.mappings.defaultPath = ""; exports.mappings.defaultPath = "";
Expand Down Expand Up @@ -248,11 +300,11 @@ function makeRequire(currentId){
if(factories[uri]){ if(factories[uri]){
try{ try{
var exports = moduleExports[uri] = {}, var exports = moduleExports[uri] = {},
module = modules[uri] = modules[uri] || new Module(uri), module = currentModule = modules[uri] = modules[uri] || new Module(uri),
currentFile = cachePath(uri), currentFile = cachePath(uri),
factory = factories[uri], factory = factories[uri],
originalExports = module.exports = exports, originalExports = module.exports = exports,
nextRequire = makeRequire(uri); nextRequire = currentRequire = makeRequire(uri);
module.dependents[currentId] = true; module.dependents[currentId] = true;
exports = factory.call(exports, nextRequire, exports, module, exports = factory.call(exports, nextRequire, exports, module,
currentFile, currentFile.replace(/\/[^\/]*$/,'')) currentFile, currentFile.replace(/\/[^\/]*$/,''))
Expand All @@ -271,6 +323,8 @@ function makeRequire(currentId){
var successful = true; var successful = true;
} }
finally{ finally{
currentRequire = null;
currentModule = null;
if(!successful){ if(!successful){
delete moduleExports[uri]; delete moduleExports[uri];
} }
Expand Down Expand Up @@ -306,7 +360,8 @@ function makeRequire(currentId){
factories[context + i + ".js"] = moduleDef.factory || moduleDef; factories[context + i + ".js"] = moduleDef.factory || moduleDef;
} }
}; };
require.def = function(id, dependencies, factory){ require.def = define;
/* require.def = function(id, dependencies, factory){
if(dependencies){ if(dependencies){
require.ensure(dependencies); require.ensure(dependencies);
}else{ }else{
Expand All @@ -322,7 +377,7 @@ function makeRequire(currentId){
} }
}) : []); }) : []);
}; };
}; };*/
require.paths = paths; require.paths = paths;
require.reloadable = reloadable; require.reloadable = reloadable;
require.resource = function(uri){ require.resource = function(uri){
Expand Down Expand Up @@ -544,6 +599,15 @@ exports.load = function(uri, require){
require.ensure(moduleId); 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){ if(packageData.compiler){
require.ensure(packageData.compiler.module); require.ensure(packageData.compiler.module);
} }
Expand All @@ -552,11 +616,10 @@ exports.load = function(uri, require){
}); });
}); });
}; };

function createFactory(uri, source){ function createFactory(uri, source){
try{ try{
factories[uri] = compile("function(require, exports, module, __filename, __dirname, Worker, SharedWorker){" + source + "\n;return exports;}", uri); 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)){ if(indexOfRequireDef > -1 && ((indexOfExport = source.indexOf("exports.")) == -1 || indexOfExport > indexOfRequireDef)){
// looks like it is an Aynchronous module definition module // looks like it is an Aynchronous module definition module
factories[uri]({def: function(id, dependencies, factory){ factories[uri]({def: function(id, dependencies, factory){
Expand Down Expand Up @@ -594,7 +657,7 @@ function createFactory(uri, source){
}) : arguments); }) : arguments);
}; };
}}); }});
} }*/
}catch(e){ }catch(e){
factories[uri] = function(){ factories[uri] = function(){
throw new Error(e.stack + " in " + uri); throw new Error(e.stack + " in " + uri);
Expand Down Expand Up @@ -791,7 +854,7 @@ function readModuleFile(path, uri){
if(path.match(/\.js$/) && typeof process != "undefined"){ if(path.match(/\.js$/) && typeof process != "undefined"){
path = path.replace(/\.js$/,".node"); path = path.replace(/\.js$/,".node");
try{ try{
file.read(path); fs.read(path);
return 'process.dlopen("' + path + '", exports);'; return 'process.dlopen("' + path + '", exports);';
} }
catch(nodeE){ catch(nodeE){
Expand Down

0 comments on commit 7373562

Please sign in to comment.