Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#86 relative requires support (PROTOTYPE) #94

Merged
merged 3 commits into from Apr 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/inject.coffee
Expand Up @@ -78,6 +78,7 @@ requireRegex = /(?:^|[^\w\$_.\(])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\
defineStaticRequireRegex = /^[\r\n\s]*define\(\s*("\S+",|'\S+',|\s*)\s*\[([^\]]*)\],\s*(function\s*\(|{).+/ defineStaticRequireRegex = /^[\r\n\s]*define\(\s*("\S+",|'\S+',|\s*)\s*\[([^\]]*)\],\s*(function\s*\(|{).+/
requireGreedyCapture = /require.*/ requireGreedyCapture = /require.*/
commentRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg commentRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg
relativePathRegex = /^(.\/|..\/).*/


### ###
CommonJS wrappers for a header and footer CommonJS wrappers for a header and footer
Expand Down Expand Up @@ -748,7 +749,15 @@ downloadTree = (tree, callback) ->
moduleId = tree.getValue() moduleId = tree.getValue()


# apply the ruleset for this module if we haven't yet # 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the use of tree.getParent() that is a very clean way to get the calling module's ID.

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 # the callback every module has when it has been loaded
onDownloadComplete = (moduleId, file) -> onDownloadComplete = (moduleId, file) ->
Expand Down Expand Up @@ -867,7 +876,7 @@ analyzeFile = (moduleId, tree) ->
db.module.setRequires(moduleId, safeRequires) db.module.setRequires(moduleId, safeRequires)
db.module.setCircular(moduleId, hasCircular) db.module.setCircular(moduleId, hasCircular)


applyRules = (moduleId, save) -> applyRules = (moduleId, save, relativePath) ->
### ###
## applyRules(moduleId) ## ## applyRules(moduleId) ##
_internal_ normalize the path based on the module collection or any functions _internal_ normalize the path based on the module collection or any functions
Expand All @@ -891,9 +900,13 @@ applyRules = (moduleId, save) ->
if typeof(userConfig.moduleRoot) is "undefined" then throw new Error("Module Root must be defined") 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 "string" then workingPath = "#{userConfig.moduleRoot}#{workingPath}"
else if typeof(userConfig.moduleRoot) is "function" 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 !fileSuffix.test(workingPath) then workingPath = "#{workingPath}.js"


if save is undefined if save is true
db.module.setPath(moduleId, workingPath) db.module.setPath(moduleId, workingPath)
db.module.setPointcuts(moduleId, pointcuts) db.module.setPointcuts(moduleId, pointcuts)
db.module.setRulesApplied(moduleId, true) db.module.setRulesApplied(moduleId, true)
Expand Down Expand Up @@ -1030,6 +1043,11 @@ createModule = (id, uri, exports) ->
return module["exports"] return module["exports"]
return module return module


basedir = (path) ->
if path.lastIndexOf("/") isnt -1
path = path.substring(0, path.lastIndexOf("/") + 1)
return path

### ###
Main Payloads: require, require.ensure, etc Main Payloads: require, require.ensure, etc
### ###
Expand Down
6 changes: 4 additions & 2 deletions tests/amd/amd.js
Expand Up @@ -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.setModuleRoot("http://localhost:4000/tests/amd/includes/spec/anon");
require.addRule("array", {path:"impl/array"}); require.addRule("array", {path:"impl/array"});
require(["require", "array"], function(require, array) { require(["require", "array"], function(require, array) {
equal("impl/array", array.name); equal("impl/array", array.name);
equal("util", array.utilName); equal("util", array.utilNameUl);
equal("impl/util", array.utilNameCl);
equal("../util", array.utilNameUUl);
start(); start();
}); });
}); });
Expand Down
6 changes: 4 additions & 2 deletions tests/amd/includes/spec/anon/impl/array.js
@@ -1,6 +1,8 @@
define(['./util'], function (util) { define(['../util', './util', '../../util'], function (util_ul, util_cl, util_uul) {
return { return {
name: 'impl/array', name: 'impl/array',
utilName: util.name utilNameUl: util_ul.name,
utilNameCl: util_cl.name,
utilNameUUl: util_uul.name
}; };
}); });
3 changes: 3 additions & 0 deletions tests/amd/includes/spec/util.js
@@ -0,0 +1,3 @@
define({
name: '../util'
});
1 change: 1 addition & 0 deletions tests/modules-1.1.1/includes/spec/divide-minus_one.js
@@ -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 changes: 6 additions & 1 deletion tests/modules-1.1.1/includes/spec/identifiers/terms.js
Expand Up @@ -7,7 +7,12 @@ var bar = require('../relative-two');
equal(bar.divide(10), 5, "relative path '..' test pass"); equal(bar.divide(10), 5, "relative path '..' test pass");


var boo = require("./relative-one"); 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 { try {
var qux = require("invalid$module"); var qux = require("invalid$module");
Expand Down
2 changes: 1 addition & 1 deletion tests/modules-1.1.1/modules-1.1.1.js
Expand Up @@ -34,7 +34,7 @@ asyncTest("Compliance", 13, function() {
require.run("compliance"); 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.setModuleRoot("http://localhost:4000/tests/modules-1.1.1/includes/spec/identifiers");
require.run("terms"); require.run("terms");
}); });
Expand Down