Permalink
Browse files

#86 relative requires support

If require a module within another module, we solve './' or '../' as a
relative path based on parent module instead of ROOT only
  • Loading branch information...
1 parent 93b8841 commit f8ea1aea0576cd7a89e36ac0c728885e72dc250e @chaozhang chaozhang committed Apr 3, 2012
View
20 src/inject.coffee
@@ -748,7 +748,15 @@ downloadTree = (tree, callback) ->
moduleId = tree.getValue()
# apply the ruleset for this module if we haven't yet
- applyRules(moduleId) if db.module.getRulesApplied() is false
+ if db.module.getRulesApplied() is false
+ if /^(.\/|..\/).*/.test(moduleId)
+ # handle relative path
+ relativePath = userConfig.moduleRoot
+ if tree.getParent() and tree.getParent().getValue()
+ relativePath = db.module.getPath(tree.getParent().getValue())
+ applyRules(moduleId, true, relativePath)
+ else
+ applyRules(moduleId, true)
# the callback every module has when it has been loaded
onDownloadComplete = (moduleId, file) ->
@@ -867,7 +875,7 @@ analyzeFile = (moduleId, tree) ->
db.module.setRequires(moduleId, safeRequires)
db.module.setCircular(moduleId, hasCircular)
-applyRules = (moduleId, save) ->
+applyRules = (moduleId, save, relativePath) ->
###
## applyRules(moduleId) ##
_internal_ normalize the path based on the module collection or any functions
@@ -891,9 +899,15 @@ applyRules = (moduleId, save) ->
if typeof(userConfig.moduleRoot) is "undefined" then throw new Error("Module Root must be defined")
else if typeof(userConfig.moduleRoot) is "string" then workingPath = "#{userConfig.moduleRoot}#{workingPath}"
else if typeof(userConfig.moduleRoot) is "function" then workingPath = userConfig.moduleRoot(workingPath)
+
+ if typeof(relativePath) is "string"
+ workingPath = relativePath + moduleId
+ if relativePath.lastIndexOf("/") isnt -1
+ workingPath = relativePath.substring(0, relativePath.lastIndexOf("/") + 1) + moduleId
+
if !fileSuffix.test(workingPath) then workingPath = "#{workingPath}.js"
- if save is undefined
+ if save is true
db.module.setPath(moduleId, workingPath)
db.module.setPointcuts(moduleId, pointcuts)
db.module.setRulesApplied(moduleId, true)
View
5 tests/amd/amd.js
@@ -56,12 +56,13 @@ asyncTest("Anon - circular", 6, function() {
});
});
-asyncTest("Anon - relativeModuleId", 2, function() {
+asyncTest("Anon - relativeModuleId", 3, function() {
require.setModuleRoot("http://localhost:4000/tests/amd/includes/spec/anon");
require.addRule("array", {path:"impl/array"});
require(["require", "array"], function(require, array) {
equal("impl/array", array.name);
- equal("util", array.utilName);
+ equal("util", array.utilNameUl);
+ equal("impl/util", array.utilNameCl);
start();
});
});
View
5 tests/amd/includes/spec/anon/impl/array.js
@@ -1,6 +1,7 @@
-define(['./util'], function (util) {
+define(['../util', './util'], function (util_ul, util_cl) {
return {
name: 'impl/array',
- utilName: util.name
+ utilNameUl: util_ul.name,
+ utilNameCl: util_cl.name
};
});
View
1 tests/modules-1.1.1/includes/spec/identifiers/relative-three/divide-plus_one.js
@@ -0,0 +1 @@
+exports.divide = function(val) { return val/2 + 1; };
View
2 tests/modules-1.1.1/includes/spec/identifiers/relative-three/relative-three.js
@@ -0,0 +1,2 @@
+exports.divide = require("../divide").divide;
+exports.divide_plus_one = require("./divide-plus_one").divide;
View
6 tests/modules-1.1.1/includes/spec/identifiers/terms.js
@@ -7,7 +7,11 @@ var bar = require('../relative-two');
equal(bar.divide(10), 5, "relative path '..' test pass");
var boo = require("./relative-one");
-equal(boo.divide(4), 2, "relative path '.' test pass")
+equal(boo.divide(4), 2, "relative path '.' test pass");
+
+var boo = require("relative-three/relative-three");
+equal(boo.divide(4), 2, "relative path '.' test pass");
+equal(boo.divide_plus_one(10), 6, "relative path '.' test pass");
try {
var qux = require("invalid$module");
View
2 tests/modules-1.1.1/modules-1.1.1.js
@@ -34,7 +34,7 @@ asyncTest("Compliance", 13, function() {
require.run("compliance");
});
-asyncTest("Compliance - Module Identifiers", 5, function() {
+asyncTest("Compliance - Module Identifiers", 7, function() {
require.setModuleRoot("http://localhost:4000/tests/modules-1.1.1/includes/spec/identifiers");
require.run("terms");
});

0 comments on commit f8ea1ae

Please sign in to comment.