Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #591, support .js package ID resolution

  • Loading branch information...
commit 33f0570aff4d88a8acfe1dd1f0dcdd3ac07d83de 1 parent 3b0d8a8
@jrburke authored
View
36 almond.js
@@ -1,5 +1,5 @@
/**
- * @license almond 0.2.7 Copyright (c) 2011-2012, The Dojo Foundation All Rights Reserved.
+ * @license almond 0.2.8 Copyright (c) 2011-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
*/
@@ -31,10 +31,11 @@ var requirejs, require, define;
* @returns {String} normalized name
*/
function normalize(name, baseName) {
- var nameParts, nameSegment, mapValue, foundMap,
+ var nameParts, nameSegment, mapValue, foundMap, lastIndex,
foundI, foundStarMap, starI, i, j, part,
baseParts = baseName && baseName.split("/"),
map = config.map,
+ jsSuffixRegExp = /\.js$/,
starMap = (map && map['*']) || {};
//Adjust any relative paths.
@@ -49,8 +50,16 @@ var requirejs, require, define;
//"one/two/three.js", but we want the directory, "one/two" for
//this normalization.
baseParts = baseParts.slice(0, baseParts.length - 1);
+ name = name.split('/');
+ lastIndex = name.length - 1;
- name = baseParts.concat(name.split("/"));
+ // Node .js allowance:
+ if (config.pkgs && hasProp(config.pkgs, baseParts[0]) &&
+ jsSuffixRegExp.test(name[lastIndex])) {
+ name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
+ }
+
+ name = baseParts.concat(name);
//start trimDots
for (i = 0; i < name.length; i += 1) {
@@ -319,6 +328,7 @@ var requirejs, require, define;
};
requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
+ var i, pkgs;
if (typeof deps === "string") {
if (handlers[deps]) {
//callback in this case is really relName
@@ -332,6 +342,20 @@ var requirejs, require, define;
} else if (!deps.splice) {
//deps is a config object, not an array.
config = deps;
+ if (config.deps) {
+ req(config.deps, config.callback);
+ }
+ pkgs = config.packages;
+ if (config.packages) {
+ config.pkgs = {};
+ for (i = 0; i < pkgs.length; i++) {
+ config.pkgs[pkgs[i].name || pkgs[i]] = true;
+ }
+ }
+ if (!callback) {
+ return;
+ }
+
if (callback.splice) {
//callback is an array, which means it is a dependency list.
//Adjust args if there are dependencies
@@ -376,11 +400,7 @@ var requirejs, require, define;
* the config return value is used.
*/
req.config = function (cfg) {
- config = cfg;
- if (config.deps) {
- req(config.deps, config.callback);
- }
- return req;
+ return req(cfg);
};
/**
View
2  bower.json
@@ -1,6 +1,6 @@
{
"name": "almond",
- "version": "0.2.7",
+ "version": "0.2.8",
"main": "almond.js",
"ignore": [
"tests",
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "almond",
"description": "A minimal AMD API implementation for use in optimized browser builds.",
- "version": "0.2.7",
+ "version": "0.2.8",
"homepage": "http://github.com/jrburke/almond",
"author": "James Burke <jrburke@gmail.com> (http://github.com/jrburke)",
"repository": {
View
1  tests/all.js
@@ -28,3 +28,4 @@ doh.registerUrl("firstDefine", "../firstDefine/firstDefine.html");
doh.registerUrl("topRelativeRequire", "../topRelativeRequire/topRelativeRequire.html");
doh.registerUrl("configDeps", "../configDeps/configDeps.html");
doh.registerUrl("defineNoCallback", "../defineNoCallback/defineNoCallback.html");
+doh.registerUrl("packagesNode", "../packagesNode/packagesNode.html");
View
45 tests/packagesNode/main-built.js
@@ -0,0 +1,45 @@
+
+define('foo/lib/bar',{
+ name: 'bar'
+});
+
+define('foo/lib/baz',['./bar'], function (bar) {
+ return {
+ name: 'baz',
+ bar: bar
+ };
+});
+
+define('foo/lib/index',['./bar.js', './baz'], function (bar, baz) {
+ return {
+ name: 'foo',
+ bar: bar,
+ baz: baz
+ };
+});
+
+define('foo', ['foo/lib/index'], function (main) { return main; });
+
+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();
+});
+
+define("packagesNode-tests", function(){});
View
17 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="../doh/runner.js"></script>
+ <script type="text/javascript" src="../doh/_browserRunner.js"></script>
+ <script type="text/javascript" src="../../almond.js"></script>
+ <script type="text/javascript" src="main-built.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>
Please sign in to comment.
Something went wrong with that request. Please try again.