Skip to content

Commit

Permalink
Fix issue where normalizeName worked incorrectly if the baseName was …
Browse files Browse the repository at this point in the history
…a package name. Changed it to accept the context and check if baseName is a package and normalize accordingly.
  • Loading branch information
jrburke committed Oct 3, 2010
1 parent e89c6c1 commit 416f24a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 24 deletions.
4 changes: 2 additions & 2 deletions build/jslib/optimize.js
Expand Up @@ -225,8 +225,8 @@ var optimize;

//Take the last match, the one closest to current text! string.
defName = defMatch[1];
normalizedName = require.normalizeName(modName, defName);

normalizedName = require.normalizeName(modName, defName, require.s.contexts._);
}

if (strip !== "strip") {
Expand Down
44 changes: 28 additions & 16 deletions require.js
Expand Up @@ -282,7 +282,7 @@ var require;
var context, newContext, loaded, pluginPrefix,
canSetContext, prop, newLength, outDeps, mods, paths, index, i,
deferMods, deferModArgs, lastModArg, waitingName, packages,
packagePaths, pkgPath;
packagePaths;

contextName = contextName ? contextName : (config && config.context ? config.context : s.ctxName);
context = s.contexts[contextName];
Expand Down Expand Up @@ -451,7 +451,7 @@ var require;
outDeps = deps;
deps = [];
for (i = 0; i < outDeps.length; i++) {
deps[i] = req.splitPrefix(outDeps[i], name);
deps[i] = req.splitPrefix(outDeps[i], name, context);
}
}

Expand Down Expand Up @@ -837,10 +837,12 @@ var require;
}
contextName = contextName || s.ctxName;

var ret, context = s.contexts[contextName];

//Normalize module name, if it contains . or ..
moduleName = req.normalizeName(moduleName, relModuleName);
moduleName = req.normalizeName(moduleName, relModuleName, context);

var ret = s.contexts[contextName].defined[moduleName];
ret = context.defined[moduleName];
if (ret === undefined) {
req.onError(new Error("require: module name '" +
moduleName +
Expand Down Expand Up @@ -894,9 +896,10 @@ var require;
* @param {String} name the relative name
* @param {String} baseName a real name that the name arg is relative
* to.
* @param {Object} context
* @returns {String} normalized name
*/
req.normalizeName = function (name, baseName) {
req.normalizeName = function (name, baseName, context) {
//Adjust any relative paths.
var part;
if (name.charAt(0) === ".") {
Expand All @@ -905,13 +908,20 @@ var require;
name +
", no relative module name available."));
}
//Convert baseName to array, and lop off the last part,
//so that . matches that "directory" and not name of the baseName's
//module. For instance, baseName of "one/two/three", maps to
//"one/two/three.js", but we want the directory, "one/two" for
//this normalization.
baseName = baseName.split("/");
baseName = baseName.slice(0, baseName.length - 1);

if (context.config.packages[baseName]) {
//If the baseName is a package name, then just treat it as one
//name to concat the name with.
baseName = [baseName];
} else {
//Convert baseName to array, and lop off the last part,
//so that . matches that "directory" and not name of the baseName's
//module. For instance, baseName of "one/two/three", maps to
//"one/two/three.js", but we want the directory, "one/two" for
//this normalization.
baseName = baseName.split("/");
baseName = baseName.slice(0, baseName.length - 1);
}

name = baseName.concat(name.split("/"));
for (i = 0; (part = name[i]); i++) {
Expand All @@ -936,20 +946,21 @@ var require;
* @param {String} name the module name
* @param {String} [baseName] base name that name is
* relative to.
* @param {Object} context
*
* @returns {Object} with properties, 'prefix' (which
* may be null), 'name' and 'fullName', which is a combination
* of the prefix (if it exists) and the name.
*/
req.splitPrefix = function (name, baseName) {
req.splitPrefix = function (name, baseName, context) {
var index = name.indexOf("!"), prefix = null;
if (index !== -1) {
prefix = name.substring(0, index);
name = name.substring(index + 1, name.length);
}

//Account for relative paths if there is a base name.
name = req.normalizeName(name, baseName);
name = req.normalizeName(name, baseName, context);

return {
prefix: prefix,
Expand All @@ -963,10 +974,11 @@ var require;
*/
req.nameToUrl = function (moduleName, ext, contextName, relModuleName) {
var paths, packages, pkg, pkgPath, syms, i, parentModule, url,
config = s.contexts[contextName].config;
context = s.contexts[contextName],
config = context.config;

//Normalize module name if have a base relative module name to work from.
moduleName = req.normalizeName(moduleName, relModuleName);
moduleName = req.normalizeName(moduleName, relModuleName, context);

//If a colon is in the URL, it indicates a protocol is used and it is just
//an URL to a file, or if it starts with a slash or ends with .js, it is just a plain file.
Expand Down
3 changes: 3 additions & 0 deletions tests/packages/baz/lib/helper.js
@@ -0,0 +1,3 @@
require.def({
name: "baz/helper"
});
5 changes: 3 additions & 2 deletions tests/packages/baz/lib/index.js
@@ -1,7 +1,8 @@
require.def(['bar', 'foo'], function (bar, foo) {
require.def(['bar', 'foo', './helper'], function (bar, foo, helper) {
return {
name: 'baz',
barDepVersion: bar.version,
fooName: foo.name
fooName: foo.name,
helperName: helper.name
};
});
2 changes: 1 addition & 1 deletion tests/packages/dojox/window/window.js
@@ -1,4 +1,4 @@
require.def(['./window/pane'], function (pane) {
require.def(['./pane'], function (pane) {
return {
name: 'dojox/window',
paneName: pane.name
Expand Down
2 changes: 2 additions & 0 deletions tests/packages/packages-tests.js
Expand Up @@ -62,11 +62,13 @@ function(require, alpha, replace, beta, util, bar, baz,
t.is("baz", baz.name);
t.is("0.4", baz.barDepVersion);
t.is("foo", baz.fooName);
t.is("baz/helper", baz.helperName);
t.is("foo", foo.name);
t.is("alpha", foo.alphaName);
t.is("foo/second", second.name);
t.is((require.isBrowser ? "./foo/lib/../data.html" : "./packages/foo/lib/../data.html"), require.nameToUrl('foo/../data', '.html'));
t.is('dojox/chair', chair.name);
t.is('dojox/chair/legs', chair.legsName);
t.is('dojox/table', table.name);
t.is('dojox/chair', table.chairName);
t.is('dojox/table/legs', legs.name);
Expand Down
3 changes: 3 additions & 0 deletions tests/packages/pkgs/dojox/chair/lib/legs.js
@@ -0,0 +1,3 @@
require.def({
name: 'dojox/chair/legs'
});
7 changes: 5 additions & 2 deletions tests/packages/pkgs/dojox/chair/lib/main.js
@@ -1,3 +1,6 @@
require.def({
name: 'dojox/chair'
require.def(['./legs'], function (legs) {
return {
name: 'dojox/chair',
legsName: legs.name
}
});
2 changes: 1 addition & 1 deletion tests/workers.js
@@ -1,7 +1,7 @@
importScripts('../require.js');

require({
baseUrl: "./",
baseUrl: "./"
},
["require", "simple", "anon/blue", "func", "anon/green"],
function(require, simple, blue, func, green) {
Expand Down

0 comments on commit 416f24a

Please sign in to comment.