[Pkg-javascript-commits] [dojo] 05/07: Fixes relative paths when using dojo/node, fixes #16414 (cherry picked from commit ddb069635e9577560db37f42025be93e1ea19f09)

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


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

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

commit 5477c6baefa87acb945233a4c765ebb26d4d37aa
Author: Kitson Kelly <dojo at kitsonkelly.com>
Date:   Thu May 30 07:33:51 2013 +0100

    Fixes relative paths when using dojo/node, fixes #16414
    (cherry picked from commit ddb069635e9577560db37f42025be93e1ea19f09)
---
 node.js                           | 27 ++++++++++++++++++++++-----
 tests/node.js                     | 10 +++++-----
 tests/resources/noderequireamd.js |  2 +-
 3 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/node.js b/node.js
index f33d8ae..f804ba9 100644
--- a/node.js
+++ b/node.js
@@ -3,6 +3,13 @@ define(["./has"], function(has){
 		throw new Error("node plugin failed to load because environment is not Node.js");
 	}
 
+	var pathUtil;
+	if(require.nodeRequire){
+		pathUtil = require.nodeRequire("path");
+	}else{
+		throw new Error("node plugin failed to load because it cannot find the original Node.js require");
+	}
+
 	return {
 		// summary:
 		//		This AMD plugin module allows native Node.js modules to be loaded by AMD modules using the Dojo
@@ -30,18 +37,28 @@ define(["./has"], function(has){
 				// temporarily moved into another variable, it will prevent modules from detecting AMD in this fashion.
 				define = undefined;
 
-				try {
+				try{
 					result = require(id);
-				} finally {
+				}finally{
 					define = oldDefine;
 				}
 				return result;
 			})(id, require.nodeRequire));
 		},
 
-		normalize: function (/**string*/ id) {
-			if (id.charAt(0) === '.') {
-				id = require.baseUrl + id;
+		normalize: function (/**string*/ id, /*Function*/ normalize){
+			// summary:
+			//		Produces a normalized id to be used by node.  Relative ids are resolved relative to the requesting
+			//		module's location in the file system and will return an id with path separators appropriate for the
+			//		local file system.
+
+			if(id.charAt(0) === "."){
+				// dirname of the reference module - normalized to match the local file system
+				var referenceModuleDirname = require.toUrl(normalize(".")).replace("/", pathUtil.sep),
+					segments = id.split("/");
+				segments.unshift(referenceModuleDirname);
+				// this will produce an absolute path normalized to the semantics of the underlying file system.
+				id = pathUtil.join.apply(pathUtil, segments);
 			}
 
 			return id;
diff --git a/tests/node.js b/tests/node.js
index 585e5c1..733a970 100644
--- a/tests/node.js
+++ b/tests/node.js
@@ -1,4 +1,4 @@
-define([ "doh/main" ], function(doh){
+define([ "require", "doh/main" ], function(require, doh){
 	doh.register("tests.node", [
 		function testRequireBuiltIn(t){
 			var td = new doh.Deferred();
@@ -19,7 +19,7 @@ define([ "doh/main" ], function(doh){
 
 		function testRequireSimpleModule(t){
 			var td = new doh.Deferred();
-			require(["dojo/node!./tests/resources/nodemodule"], td.getTestCallback(function(nodemodule){
+			require(["dojo/node!./resources/nodemodule"], td.getTestCallback(function(nodemodule){
 				t.t("test" in nodemodule, "module loaded");
 				t.is(nodemodule.test, "value", "object has expected value");
 			}));
@@ -28,7 +28,7 @@ define([ "doh/main" ], function(doh){
 
 		function testRequireRequire(t){
 			var td = new doh.Deferred();
-			require(["dojo/node!./tests/resources/noderequire"], td.getTestCallback(function(noderequire){
+			require(["dojo/node!./resources/noderequire"], td.getTestCallback(function(noderequire){
 				t.t("test" in noderequire, "module loaded");
 				t.is(noderequire.test, "value", "object has expected value");
 			}));
@@ -37,7 +37,7 @@ define([ "doh/main" ], function(doh){
 
 		function testRequirePackageJson(t){
 			var td = new doh.Deferred();
-			require(["dojo/node!./tests/resources/nodemod"], td.getTestCallback(function(nodemod){
+			require(["dojo/node!./resources/nodemod"], td.getTestCallback(function(nodemod){
 				t.t("test" in nodemod, "module loaded");
 				t.is(nodemod.test, "value", "object has expected value");
 			}));
@@ -46,7 +46,7 @@ define([ "doh/main" ], function(doh){
 
 		function testRequireCommonJSAMD(t){
 			var td = new doh.Deferred();
-			require(["dojo/node!./tests/resources/noderequireamd"], td.getTestCallback(function(noderequireamd){
+			require(["dojo/node!./resources/noderequireamd"], td.getTestCallback(function(noderequireamd){
 				t.t(noderequireamd.nodeamd.test === "foo", "module loaded");
 			}));
 			return td;
diff --git a/tests/resources/noderequireamd.js b/tests/resources/noderequireamd.js
index c276a25..d7366f8 100644
--- a/tests/resources/noderequireamd.js
+++ b/tests/resources/noderequireamd.js
@@ -4,4 +4,4 @@
 
 var noderequireamd = module.exports = exports;
 
-noderequireamd.nodeamd = require("./nodeamd.js");
+noderequireamd.nodeamd = require("./nodeamd");

-- 
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