[Pkg-javascript-commits] [dojo] 65/88: dojo/node can now load CommonJS modules dependent on modules that try to detect AMD, refs #16414

David Prévot taffit at moszumanska.debian.org
Thu Aug 21 17:39:39 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to annotated tag 1.8.5
in repository dojo.

commit 7e6dd429b40f2b52b0c7d1561bbc2f1874628c96
Author: Kitson Kelly <dojo at kitsonkelly.com>
Date:   Sat Feb 16 10:34:53 2013 +0000

    dojo/node can now load CommonJS modules dependent on modules that try to detect AMD, refs #16414
    
    git-svn-id: http://svn.dojotoolkit.org/src/branches/1.8/dojo@30604 560b804f-0ae3-0310-86f3-f6aa0a117693
---
 node.js                           | 25 ++++++++++++++++++++++++-
 tests/node.js                     |  8 ++++++++
 tests/resources/nodeamd.js        | 26 ++++++++++++++++++++++++++
 tests/resources/noderequireamd.js |  7 +++++++
 4 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/node.js b/node.js
index 99dbf0f..5fabbb9 100644
--- a/node.js
+++ b/node.js
@@ -21,7 +21,30 @@ define(["dojo/has"], function(has){
 				throw new Error("Cannot find native require function");
 			}
 
-			load(require.nodeRequire(id));
+			load((function(id, require){
+				var oldDefine = define,
+					result;
+
+				// Some modules may attempt to detect an AMD loader via define and define.amd.  This can cause issues
+				// when other CommonJS modules attempt to load them via the standard node require().  If define is
+				// temporarily moved into another variable, it will prevent modules from detecting AMD in this fashion.
+				define = undefined;
+
+				try {
+					result = require(id);
+				} finally {
+					define = oldDefine;
+				}
+				return result;
+			})(id, require.nodeRequire));
+		},
+
+		normalize: function (/**string*/ id) {
+			if (id.charAt(0) === '.') {
+				id = require.baseUrl + id;
+			}
+
+			return id;
 		}
 	};
 });
\ No newline at end of file
diff --git a/tests/node.js b/tests/node.js
index 46cb043..585e5c1 100644
--- a/tests/node.js
+++ b/tests/node.js
@@ -42,6 +42,14 @@ define([ "doh/main" ], function(doh){
 				t.is(nodemod.test, "value", "object has expected value");
 			}));
 			return td;
+		},
+
+		function testRequireCommonJSAMD(t){
+			var td = new doh.Deferred();
+			require(["dojo/node!./tests/resources/noderequireamd"], td.getTestCallback(function(noderequireamd){
+				t.t(noderequireamd.nodeamd.test === "foo", "module loaded");
+			}));
+			return td;
 		}
 	]);
 });
\ No newline at end of file
diff --git a/tests/resources/nodeamd.js b/tests/resources/nodeamd.js
new file mode 100644
index 0000000..da21a49
--- /dev/null
+++ b/tests/resources/nodeamd.js
@@ -0,0 +1,26 @@
+/*
+ * Test module for dojo/node plugin that tries to detect AMD
+ */
+
+(function(undefined){
+
+	var nodeamd = {};
+
+	nodeamd.test = 'foo';
+
+	// "Improper" detection of AMD in a combined CommonJS/AMD modules, where the module thinks it is being loaded
+	// by an AMD loader, when in fact it could be being loaded by a CommonJS module loader.  The dojo/node plugin
+	// needs to "hide" define from these types of modules.
+	if (typeof define === "function" && define.amd) {
+		define("nodeamd", [], function () {
+			return nodeamd;
+		});
+	}
+	else if (typeof module !== 'undefined' && module.exports) {
+		module.exports = nodeamd;
+	}
+	else if (typeof ender === 'undefined') {
+		this['nodeamd'] = nodeamd;
+	}
+
+}).call(this);
\ No newline at end of file
diff --git a/tests/resources/noderequireamd.js b/tests/resources/noderequireamd.js
new file mode 100644
index 0000000..c276a25
--- /dev/null
+++ b/tests/resources/noderequireamd.js
@@ -0,0 +1,7 @@
+/*
+ * Test module for dojo/node plugin that relies upon a 'dual' AMD/CommonJS module
+ */
+
+var noderequireamd = module.exports = exports;
+
+noderequireamd.nodeamd = require("./nodeamd.js");

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/dojo.git



More information about the Pkg-javascript-commits mailing list