Permalink
Browse files

Fixes #101, resolve relative plugin resource IDs correctly

  • Loading branch information...
1 parent 8cf5b34 commit c90b217cf1fbe587df6d5bf65ca26dba6e9493e3 @jrburke jrburke committed Sep 1, 2016
Showing with 71 additions and 9 deletions.
  1. +17 −9 almond.js
  2. +1 −0 tests/all.js
  3. +16 −0 tests/plugins/relative/pluginRelative.html
  4. +37 −0 tests/plugins/relative/pluginRelative.js
View
@@ -195,32 +195,39 @@ var requirejs, require, define;
return [prefix, name];
}
+ //Creates a parts array for a relName where first part is plugin ID,
+ //second part is resource ID. Assumes relName has already been normalized.
+ function makeRelParts(relName) {
+ return relName ? splitPrefix(relName) : [];
+ }
+
/**
* Makes a name map, normalizing the name, and using a plugin
* for normalization if necessary. Grabs a ref to plugin
* too, as an optimization.
*/
- makeMap = function (name, relName) {
+ makeMap = function (name, relParts) {
var plugin,
parts = splitPrefix(name),
- prefix = parts[0];
+ prefix = parts[0],
+ relResourceName = relParts[1];
name = parts[1];
if (prefix) {
- prefix = normalize(prefix, relName);
+ prefix = normalize(prefix, relResourceName);
plugin = callDep(prefix);
}
//Normalize according
if (prefix) {
if (plugin && plugin.normalize) {
- name = plugin.normalize(name, makeNormalize(relName));
+ name = plugin.normalize(name, makeNormalize(relResourceName));
} else {
- name = normalize(name, relName);
+ name = normalize(name, relResourceName);
}
} else {
- name = normalize(name, relName);
+ name = normalize(name, relResourceName);
parts = splitPrefix(name);
prefix = parts[0];
name = parts[1];
@@ -267,13 +274,14 @@ var requirejs, require, define;
};
main = function (name, deps, callback, relName) {
- var cjsModule, depName, ret, map, i,
+ var cjsModule, depName, ret, map, i, relParts,
args = [],
callbackType = typeof callback,
usingExports;
//Use name if no relName
relName = relName || name;
+ relParts = makeRelParts(relName);
//Call the callback to define the module, if necessary.
if (callbackType === 'undefined' || callbackType === 'function') {
@@ -282,7 +290,7 @@ var requirejs, require, define;
//Default to [require, exports, module] if no deps
deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
for (i = 0; i < deps.length; i += 1) {
- map = makeMap(deps[i], relName);
+ map = makeMap(deps[i], relParts);
depName = map.f;
//Fast path CommonJS standard dependencies.
@@ -338,7 +346,7 @@ var requirejs, require, define;
//deps arg is the module name, and second arg (if passed)
//is just the relName.
//Normalize module name, if it contains . or ..
- return callDep(makeMap(deps, callback).f);
+ return callDep(makeMap(deps, makeRelParts(callback)).f);
} else if (!deps.splice) {
//deps is a config object, not an array.
config = deps;
View
@@ -8,6 +8,7 @@ doh.registerUrl("mapConfigStarAdapter", "../mapConfig/mapConfigStarAdapter.html"
doh.registerUrl("mapConfigSpecificity", "../mapConfig/mapConfigSpecificity.html");
doh.registerUrl("mapConfigPlugin", "../mapConfig/mapConfigPlugin.html");
doh.registerUrl("plugins", "../plugins/plugins.html");
+doh.registerUrl("pluginRelative", "../plugins/relative/pluginRelative.html");
doh.registerUrl("pluginsMapSameName", "../plugins/pluginMapSameName/pluginMapSameName.html");
doh.registerUrl("orderPlugin", "../plugins/order/order.html");
doh.registerUrl("text", "../plugins/text.html");
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>almond: Plugin Relative Module ID Test</title>
+ <script type="text/javascript" src="../../doh/runner.js"></script>
+ <script type="text/javascript" src="../../doh/_browserRunner.js"></script>
+ <script src="../../../almond.js"></script>
+ <script type="text/javascript" src="pluginRelative.js"></script>
+</head>
+<body>
+ <h1>almond: Plugin Relative Module ID Test</h1>
+ <p>Test if dependency that is a plugin ID with a relative resource name resolves correctly:
+ <a href="https://github.com/requirejs/requirejs/issues/101">101</a></p>
+ <p>Check console for messages</p>
+</body>
+</html>
@@ -0,0 +1,37 @@
+
+define('plugin', {
+ load: function (id, require, load, config) {
+ throw new Error("Dynamic load not allowed: " + id);
+ }
+});
+
+define('plugin!src/b', [], function () {
+ return {
+ name: 'b'
+ };
+});
+
+
+define('plugin!src/a', ['plugin!./b'], function (b) {
+ return {
+ name: 'a',
+ b: b
+ };
+});
+
+require(['plugin!src/a'], function (a) {
+
+ doh.register(
+ 'pluginRelative',
+ [
+ function pluginRelative(t){
+ t.is('a', a.name);
+ t.is('b', a.b.name);
+ }
+ ]
+ );
+ doh.run();
+
+});
+
+define("pluginRelative-tests", function(){});

0 comments on commit c90b217

Please sign in to comment.