Skip to content

Commit

Permalink
Fixes requirejs/r.js#591: resolve modules IDs in a package that end i…
Browse files Browse the repository at this point in the history
…n .js without the .js
  • Loading branch information
jrburke committed Jan 7, 2014
1 parent 21b3125 commit 450a93d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
15 changes: 13 additions & 2 deletions require.js
Expand Up @@ -266,7 +266,7 @@ var requirejs, require, define;
* @returns {String} normalized name
*/
function normalize(name, baseName, applyMap) {
var pkgMain, mapValue, nameParts, i, j, nameSegment,
var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex,
foundMap, foundI, foundStarMap, starI,
baseParts = baseName && baseName.split('/'),
normalizedBaseParts = baseParts,
Expand All @@ -285,8 +285,19 @@ var requirejs, require, define;
//'one/two/three.js', but we want the directory, 'one/two' for
//this normalization.
normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
name = name.split('/');
lastIndex = name.length - 1;

// If inside a package and name ends in .js, strip
// it out, because of node. Have to do this here, and
// not in nameToUrl because node allows either .js or
// non .js to map to same file.
if (getOwn(config.pkgs, normalizedBaseParts[0]) &&
req.jsExtRegExp.test(name[lastIndex])) {
name[lastIndex] = name[lastIndex].replace(req.jsExtRegExp, '');
}

name = normalizedBaseParts.concat(name.split('/'));
name = normalizedBaseParts.concat(name);
trimDots(name);
name = name.join('/');
} else if (name.indexOf('./') === 0) {
Expand Down
3 changes: 3 additions & 0 deletions tests/packagesNode/node_modules/foo/lib/bar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/packagesNode/node_modules/foo/lib/baz.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/packagesNode/node_modules/foo/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/packagesNode/packagesNode-tests.js
@@ -0,0 +1,21 @@
require({
packages: [{
name: 'foo',
location: 'node_modules/foo',
main: 'lib/index'
}]
}, ['foo'], function (foo) {

doh.register(
'packagesNode',
[
function packagesNode(t){
t.is('foo', foo.name);
t.is('bar', foo.bar.name);
t.is('baz', foo.baz.name);
t.is('bar', foo.baz.bar.name);
}
]
);
doh.run();
});
17 changes: 17 additions & 0 deletions tests/packagesNode/packagesNode.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>require.js: Packages Node-Style Test</title>
<script type="text/javascript" src="../../require.js"></script>
<script type="text/javascript" src="../doh/runner.js"></script>
<script type="text/javascript" src="../doh/_browserRunner.js"></script>
<script type="text/javascript" src="packagesNode-tests.js"></script>
</head>
<body>
<h1>require.js: Packages Node-Style Test</h1>
<p>Test package that uses ".js" node-style module ID references inside
a package. More info:
<a href="https://github.com/jrburke/r.js/pull/591">r.js 591</a></p>
<p>Check console for messages</p>
</body>
</html>

0 comments on commit 450a93d

Please sign in to comment.