Permalink
Browse files

Fixes #22 and #20, always go unordered, and support module config.

  • Loading branch information...
1 parent 65f7929 commit c9657f26d25a956683aee0a69d60052bd7fc7a89 @jrburke jrburke committed May 5, 2012
Showing with 99 additions and 20 deletions.
  1. +1 −0 .gitignore
  2. +33 −20 almond.js
  3. +3 −0 buildTest.sh
  4. +1 −0 tests/all.js
  5. +43 −0 tests/moduleConfig/moduleConfig.html
  6. +18 −0 tests/moduleConfig/moduleConfig.js
View
1 .gitignore
@@ -1,5 +1,6 @@
.DS_Store
tests/simple-built.js
+tests/moduleConfig/moduleConfig-built.js
tests/plugins/plugins-built.js
tests/plugins/coffee-built.js
tests/plugins/text-built.js
View
53 almond.js
@@ -3,17 +3,30 @@
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
*/
-/*jslint strict: false, plusplus: false */
+//Going sloppy to avoid 'use strict' string cost, but strict practices should
@jrburke
jrburke Jul 8, 2012

It is extra bytes to deliver the 'use strict' string in the source, and this is trying to be a small library. So, just a way to reduce file size by a little bit.

@zedr
zedr Jun 17, 2013

If strict mode isn't explicitly invoked, the interpreter might pass on using strict mode specific optimizations, that are not guaranteed (being an implementation detail) but might be available and may benefit performance.

Is it worth to miss this opportunity just to save 13 bytes?

@jrburke
jrburke Jun 17, 2013

There have been other costs with concatenating code with use strict with code that did not expect use strict. While those effects can be localized by placing it inside the function wrapper, I prefer to just let the consumer of this library decide if they want "use strict" parsing and can add it for instance, by using the r.js optimizer wrap options to specify it.

If you feel strongly about it though, you can fork the code to add it and then add a link to the fork on this page:
https://github.com/jrburke/requirejs/wiki/AMD-API-Shims

+//be followed.
+/*jslint sloppy: true */
/*global setTimeout: false */
var requirejs, require, define;
(function (undef) {
-
var defined = {},
waiting = {},
+ config = {},
aps = [].slice,
main, req;
+ function each(ary, func) {
+ if (ary) {
+ var i;
+ for (i = 0; i < ary.length; i += 1) {
+ if (func(ary[i], i, ary)) {
+ break;
+ }
+ }
+ }
+ }
+
if (typeof define === "function") {
//If a define is already in play via another AMD loader,
//do not overwrite.
@@ -46,8 +59,7 @@ var requirejs, require, define;
name = baseName.concat(name.split("/"));
//start trimDots
- var i, part;
- for (i = 0; (part = name[i]); i++) {
+ each(name, function (part, i) {
if (part === ".") {
name.splice(i, 1);
i -= 1;
@@ -59,13 +71,13 @@ var requirejs, require, define;
//no path mapping for a path starting with '..'.
//This can still fail, but catches the most reasonable
//uses of ..
- break;
+ return true;
} else if (i > 0) {
name.splice(i - 1, 2);
i -= 2;
}
}
- }
+ });
//end trimDots
name = name.join("/");
@@ -139,7 +151,7 @@ var requirejs, require, define;
main = function (name, deps, callback, relName) {
var args = [],
usingExports,
- cjsModule, depName, i, ret, map;
+ cjsModule, depName, ret, map;
//Use name if no relName
if (!relName) {
@@ -157,8 +169,8 @@ var requirejs, require, define;
//Pull out the defined dependencies and pass the ordered
//values to the callback.
- for (i = 0; i < deps.length; i++) {
- map = makeMap(deps[i], relName);
+ each(deps, function (dep, i) {
+ map = makeMap(dep, relName);
depName = map.f;
//Fast path CommonJS standard dependencies.
@@ -173,7 +185,10 @@ var requirejs, require, define;
cjsModule = args[i] = {
id: name,
uri: '',
- exports: defined[name]
+ exports: defined[name],
+ config: function () {
+ return (config && config.config && config.config[name]) || {};
+ }
};
} else if (defined.hasOwnProperty(depName) || waiting.hasOwnProperty(depName)) {
args[i] = callDep(depName);
@@ -183,17 +198,18 @@ var requirejs, require, define;
} else {
throw name + ' missing ' + depName;
}
- }
+ });
ret = callback.apply(defined[name], args);
if (name) {
//If setting exports via "module" is in play,
//favor that over return value and exports. After that,
//favor a non-undefined return value over exports use.
- if (cjsModule && cjsModule.exports !== undef) {
+ if (cjsModule && cjsModule.exports !== undef &&
+ cjsModule.exports !== defined[name]) {
defined[name] = cjsModule.exports;
- } else if (!usingExports) {
+ } else if (ret !== undef) {
//Use the return value from the function.
defined[name] = ret;
}
@@ -220,7 +236,7 @@ var requirejs, require, define;
//callback is an array, which means it is a dependency list.
//Adjust args if there are dependencies
deps = callback;
- callback = arguments[2];
+ callback = relName;
} else {
deps = [];
}
@@ -242,7 +258,8 @@ var requirejs, require, define;
* Just drops the config on the floor, but returns req in case
* the config return value is used.
*/
- req.config = function () {
+ req.config = function (cfg) {
+ config = cfg;
return req;
};
@@ -264,11 +281,7 @@ var requirejs, require, define;
deps = [];
}
- if (define.unordered) {
- waiting[name] = [name, deps, callback];
- } else {
- main(name, deps, callback);
- }
+ waiting[name] = [name, deps, callback];
};
define.amd = {
View
3 buildTest.sh
@@ -2,6 +2,9 @@
cat almond.js > tests/simple-built.js
cat tests/simple.js >> tests/simple-built.js
+cat almond.js > tests/moduleConfig/moduleConfig-built.js
+cat tests/moduleConfig/moduleConfig.js >> tests/moduleConfig/moduleConfig-built.js
+
cat almond.js > tests/plugins/plugins-built.js
cat tests/plugins/plugins.js >> tests/plugins/plugins-built.js
View
1 tests/all.js
@@ -1,5 +1,6 @@
doh.registerUrl("simple", "../simple.html");
+doh.registerUrl("moduleConfig", "../moduleConfig/moduleConfig.html");
doh.registerUrl("plugins", "../plugins/plugins.html");
doh.registerUrl("orderPlugin", "../plugins/order/order.html");
doh.registerUrl("text", "../plugins/text.html");
View
43 tests/moduleConfig/moduleConfig.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>almond: moduleConfig 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="moduleConfig-built.js"></script>
+ <script type="text/javascript">
+ var master = new doh.Deferred();
+
+ requirejs.config({
+ config: {
+ 'foo': {
+ locale: 'en-us'
+ },
+ 'bar': {
+ color: 'blue'
+ }
+ }
+ });
+
+ require(['foo', 'bar', 'baz'], function(foo, bar, baz) {
+ doh.register('moduleConfig',
+ [{
+ name: 'moduleConfig',
+ timeout: 2000,
+ runTest: function() {
+ doh.is('en-us', foo.locale);
+ doh.is('blue', bar.color);
+ doh.is(undefined, baz.doesNotExist);
+ }
+ }]
+ );
+ doh.run();
+
+ });
+ </script>
+</head>
+<body>
+ <h1>almond: moduleConfig Test</h1>
+ <p>Check console for messages</p>
+</body>
+</html>
View
18 tests/moduleConfig/moduleConfig.js
@@ -0,0 +1,18 @@
+
+define('foo', function (require, exports, module) {
+ return {
+ locale: module.config().locale
+ };
+});
+
+define('bar', ['module'], function (module) {
+ return {
+ color: module.config().color
+ };
+});
+
+define('baz', ['module'], function (module) {
+ return {
+ doesNotExist: module.config().doesNotExist
+ };
+});

0 comments on commit c9657f2

Please sign in to comment.