Permalink
Browse files

initial support for amd format modules

  • Loading branch information...
1 parent fbb42b5 commit 488a0a89b551d78e4eb4285553766517d131dc87 @joewalker joewalker committed Mar 22, 2012
Showing with 28 additions and 3 deletions.
  1. +28 −3 lib/dryice/mini_require.js
View
@@ -21,6 +21,9 @@ function define(moduleName, deps, payload) {
if (arguments.length == 2) {
payload = deps;
}
+ else {
+ payload.deps = deps;
+ }
if (define.debugDependencies) {
console.log("define: " + moduleName + " -> " + payload.toString()
@@ -48,6 +51,11 @@ define.debugDependencies = false;
* Self executing function in which Domain is defined, and attached to define
*/
(function() {
+ var Syntax = {
+ COMMON_JS: 'commonjs',
+ AMD: 'amd'
+ };
+
/**
* We invoke require() in the context of a Domain so we can have multiple
* sets of modules running separate from each other.
@@ -58,6 +66,7 @@ define.debugDependencies = false;
*/
function Domain() {
this.modules = {};
+ this.syntax = Syntax.COMMON_JS;
if (define.debugDependencies) {
this.depth = "";
@@ -77,8 +86,15 @@ define.debugDependencies = false;
* @param callback Function to call when the dependencies are resolved
* @return The module required or undefined for array/callback method
*/
- Domain.prototype.require = function(deps, callback) {
+ Domain.prototype.require = function(config, deps, callback) {
+ if (arguments.length === 2) {
+ callback = deps;
+ deps = config;
+ config = undefined;
+ }
+
if (Array.isArray(deps)) {
+ this.syntax = Syntax.AMD;
var params = deps.map(function(dep) {
return this.lookup(dep);
}, this);
@@ -123,9 +139,18 @@ define.debugDependencies = false;
this.depth += ".";
}
- var exports = {};
+ var exports;
try {
- module(this.require.bind(this), exports, { id: moduleName, uri: "" });
+ if (this.syntax === Syntax.COMMON_JS) {
+ exports = {};
+ module(this.require.bind(this), exports, { id: moduleName, uri: "" });
+ }
+ else {
+ var modules = module.deps.map(function(dep) {
+ return this.lookup(dep);
+ }.bind(this));
+ exports = module.apply(null, modules);
+ }
}
catch (ex) {
console.error("Error using module: " + moduleName, ex);

0 comments on commit 488a0a8

Please sign in to comment.