#86 relative requires support (PROTOTYPE) #94

Merged
merged 3 commits into from Apr 4, 2012
View
@@ -78,6 +78,7 @@ requireRegex = /(?:^|[^\w\$_.\(])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\
defineStaticRequireRegex = /^[\r\n\s]*define\(\s*("\S+",|'\S+',|\s*)\s*\[([^\]]*)\],\s*(function\s*\(|{).+/
requireGreedyCapture = /require.*/
commentRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg
+relativePathRegex = /^(.\/|..\/).*/
###
CommonJS wrappers for a header and footer
@@ -748,7 +749,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 relativePathRegex.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 +876,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 +900,13 @@ 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 = basedir(relativePath) + 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)
@@ -1030,6 +1043,11 @@ createModule = (id, uri, exports) ->
return module["exports"]
return module
+basedir = (path) ->
+ if path.lastIndexOf("/") isnt -1
+ path = path.substring(0, path.lastIndexOf("/") + 1)
+ return path
+
###
Main Payloads: require, require.ensure, etc
###
View
@@ -56,12 +56,14 @@ asyncTest("Anon - circular", 6, function() {
});
});
-asyncTest("Anon - relativeModuleId", 2, function() {
+asyncTest("Anon - relativeModuleId", 4, 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);
+ equal("../util", array.utilNameUUl);
start();
});
});
@@ -1,6 +1,8 @@
-define(['./util'], function (util) {
+define(['../util', './util', '../../util'], function (util_ul, util_cl, util_uul) {
return {
name: 'impl/array',
- utilName: util.name
+ utilNameUl: util_ul.name,
+ utilNameCl: util_cl.name,
+ utilNameUUl: util_uul.name
};
});
@@ -0,0 +1,3 @@
+define({
+ name: '../util'
+});
@@ -0,0 +1 @@
+exports.divide = function(val) { return val/2 - 1; };
@@ -0,0 +1 @@
+exports.divide = function(val) { return val/2 + 1; };
@@ -0,0 +1,3 @@
+exports.divide = require("../divide").divide;
+exports.divide_plus_one = require("./divide-plus_one").divide;
+exports.divide_minus_one = require("../../divide-minus_one").divide;
@@ -7,7 +7,12 @@ 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");
+equal(boo.divide_minus_one(10), 4, "relative path '.' test pass");
try {
var qux = require("invalid$module");
@@ -34,7 +34,7 @@ asyncTest("Compliance", 13, function() {
require.run("compliance");
});
-asyncTest("Compliance - Module Identifiers", 5, function() {
+asyncTest("Compliance - Module Identifiers", 8, function() {
require.setModuleRoot("http://localhost:4000/tests/modules-1.1.1/includes/spec/identifiers");
require.run("terms");
});