Skip to content

Commit

Permalink
Merge pull request #1 from jrburke/requirejsupdate
Browse files Browse the repository at this point in the history
update almond to 0.0.3 and r.js to 1.0.2
  • Loading branch information
secretrobotron committed Dec 15, 2011
2 parents a7d219a + 6b86e52 commit 5fda114
Show file tree
Hide file tree
Showing 2 changed files with 1,543 additions and 1,096 deletions.
301 changes: 167 additions & 134 deletions lib/build/almond.js
@@ -1,65 +1,25 @@
/**
* almond 0.0.0 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* almond 0.0.3 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
*/
/*jslint strict: false, plusplus: false */
/*global */
/*global setTimeout: false */

var requirejs, require, define;
(function () {
(function (undef) {

var defined = {},
aps = Array.prototype.slice,
ostring = Object.prototype.toString,
req;
waiting = {},
aps = [].slice,
main, req;

function isFunction(it) {
return ostring.call(it) === "[object Function]";
}

function isArray(it) {
return ostring.call(it) === "[object Array]";
}

if (typeof define === "function" && define.amd) {
if (typeof define === "function") {
//If a define is already in play via another AMD loader,
//do not overwrite.
return;
}

/**
* Trims the . and .. from an array of path segments.
* It will keep a leading path segment if a .. will become
* the first path segment, to help with module name lookups,
* which act like paths, but can be remapped. But the end result,
* all paths that use this function should look normalized.
* NOTE: this method MODIFIES the input array.
* @param {Array} ary the array of path segments.
*/
function trimDots(ary) {
var i, part;
for (i = 0; (part = ary[i]); i++) {
if (part === ".") {
ary.splice(i, 1);
i -= 1;
} else if (part === "..") {
if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
//End of the line. Keep at least one non-dot
//path segment at the front so it can be mapped
//correctly to disk. Otherwise, there is likely
//no path mapping for a path starting with '..'.
//This can still fail, but catches the most reasonable
//uses of ..
break;
} else if (i > 0) {
ary.splice(i - 1, 2);
i -= 2;
}
}
}
}

/**
* Given a relative module name, like ./something, normalize it to
* a real name that can be mapped to a path.
Expand All @@ -84,67 +44,144 @@ var requirejs, require, define;
baseName = baseName.slice(0, baseName.length - 1);

name = baseName.concat(name.split("/"));
trimDots(name);

//start trimDots
var i, part;
for (i = 0; (part = name[i]); i++) {
if (part === ".") {
name.splice(i, 1);
i -= 1;
} else if (part === "..") {
if (i === 1 && (name[2] === '..' || name[0] === '..')) {
//End of the line. Keep at least one non-dot
//path segment at the front so it can be mapped
//correctly to disk. Otherwise, there is likely
//no path mapping for a path starting with '..'.
//This can still fail, but catches the most reasonable
//uses of ..
break;
} else if (i > 0) {
name.splice(i - 1, 2);
i -= 2;
}
}
}
//end trimDots

name = name.join("/");
}
}
return name;
}

/**
* Helper function that creates a setExports function for a "module"
* CommonJS dependency. Do this here to avoid creating a closure that
* is part of a loop.
*/
function makeSetExports(moduleObj) {
return function (exports) {
moduleObj.exports = exports;
};
}

function makeRequire(relName) {
function makeRequire(relName, forceSync) {
return function () {
//A version of a require function that passes a moduleName
//value for items that may need to
//look up paths relative to the moduleName
var args = aps.call(arguments, 0);
args.push(relName);
return req.apply(null, args);
return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
};
}

function makeNormalize(relName) {
return function (name) {
return normalize(name, relName);
};
}

function makeLoad(depName) {
return function (value) {
defined[depName] = value;
};
}

function callDep(name) {
if (waiting.hasOwnProperty(name)) {
var args = waiting[name];
delete waiting[name];
main.apply(undef, args);
}
return defined[name];
}

/**
* Makes a name map, normalizing the name, and using a plugin
* for normalization if necessary. Grabs a ref to plugin
* too, as an optimization.
*/
function makeMap(name, relName) {
var prefix, plugin,
index = name.indexOf('!');

if (index !== -1) {
prefix = normalize(name.slice(0, index), relName);
name = name.slice(index + 1);
plugin = callDep(prefix);

//Normalize according
if (plugin && plugin.normalize) {
name = plugin.normalize(name, makeNormalize(relName));
} else {
name = normalize(name, relName);
}
} else {
name = normalize(name, relName);
}

//Using ridiculous property names for space reasons
return {
f: prefix ? prefix + '!' + name : name, //fullName
n: name,
p: plugin
};
}

function main(name, deps, callback, relName) {
main = function (name, deps, callback, relName) {
var args = [],
usingExports = false,
cjsModule, depName, i, ret;
usingExports,
cjsModule, depName, i, ret, map;

//Use name if no relName
if (!relName) {
relName = name;
}

//Call the callback to define the module, if necessary.
if (isFunction(callback)) {
if (typeof callback === 'function') {

//Default to require, exports, module if no deps if
//the factory arg has any arguments specified.
if (!deps.length && callback.length) {
deps = ['require', 'exports', 'module'];
}

//Pull out the defined dependencies and pass the ordered
//values to the callback.
if (deps) {
for (i = 0; i < deps.length; i++) {
depName = normalize(deps[i], (name || relName));

//Fast path CommonJS standard dependencies.
if (depName === "require") {
args[i] = makeRequire(name);
} else if (depName === "exports") {
//CommonJS module spec 1.1
args[i] = defined[name] = {};
usingExports = true;
} else if (depName === "module") {
//CommonJS module spec 1.1
cjsModule = args[i] = {
id: name,
uri: '',
exports: defined[name]
};
cjsModule.setExports = makeSetExports(cjsModule);
} else {
args[i] = defined[depName];
}
for (i = 0; i < deps.length; i++) {
map = makeMap(deps[i], relName);
depName = map.f;

//Fast path CommonJS standard dependencies.
if (depName === "require") {
args[i] = makeRequire(name);
} else if (depName === "exports") {
//CommonJS module spec 1.1
args[i] = defined[name] = {};
usingExports = true;
} else if (depName === "module") {
//CommonJS module spec 1.1
cjsModule = args[i] = {
id: name,
uri: '',
exports: defined[name]
};
} else if (defined.hasOwnProperty(depName) || waiting.hasOwnProperty(depName)) {
args[i] = callDep(depName);
} else if (map.p) {
map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
args[i] = defined[depName];
} else {
throw name + ' missing ' + depName;
}
}

Expand All @@ -154,12 +191,9 @@ var requirejs, require, define;
//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 !== undefined) {
ret = defined[name] = cjsModule.exports;
} else if (ret === undefined && usingExports) {
//exports already set the defined value.
ret = defined[name];
} else {
if (cjsModule && cjsModule.exports !== undef) {
defined[name] = cjsModule.exports;
} else if (!usingExports) {
//Use the return value from the function.
defined[name] = ret;
}
Expand All @@ -169,76 +203,75 @@ var requirejs, require, define;
//worry about defining if have a module name.
defined[name] = callback;
}
}
};

requirejs = req = function (deps, callback, relName) {
var moduleName, fullName, config;
requirejs = req = function (deps, callback, relName, forceSync) {
if (typeof deps === "string") {

//Determine if have config object in the call.
//Drop the config stuff on the ground.
if (!isArray(deps) && typeof deps !== "string") {
// deps is a config object
config = deps;
if (isArray(callback)) {
// Adjust args if there are dependencies
//Just return the module wanted. In this scenario, the
//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);
} else if (!deps.splice) {
//deps is a config object, not an array.
//Drop the config stuff on the ground.
if (callback.splice) {
//callback is an array, which means it is a dependency list.
//Adjust args if there are dependencies
deps = callback;
callback = arguments[2];
} else {
deps = [];
}
}

if (typeof deps === "string") {

//Just return the module wanted. In this scenario, the
//second arg (if passed) is just the relModuleMap.
moduleName = deps;
relName = callback;

//Normalize module name, if it contains . or ..
fullName = normalize(moduleName, relName);

if (!(fullName in defined)) {
throw new Error("Module name '" +
fullName +
"' has not been loaded.");
}
return defined[fullName];
}

//Simulate async callback;
setTimeout(function () {
main(null, deps, callback, relName);
}, 15);
if (forceSync) {
main(undef, deps, callback, relName);
} else {
setTimeout(function () {
main(undef, deps, callback, relName);
}, 15);
}

return req;
};

/**
* Support require.config() to make it easier to cooperate with other
* AMD loaders on globally agreed names.
* Just drops the config on the floor, but returns req in case
* the config return value is used.
*/
req.config = function (config) {
return req(config);
req.config = function () {
return req;
};

/**
* Export require as a global, but only if it does not already exist.
*/
if (typeof require === "undefined") {
if (!require) {
require = req;
}

define = function (name, deps, callback) {

//This module may not have dependencies
if (!isArray(deps)) {
if (!deps.splice) {
//deps is not an array, so probably means
//an object literal or factory function for
//the value. Adjust args.
callback = deps;
deps = [];
}

main(name, deps, callback);
if (define.unordered) {
waiting[name] = [name, deps, callback];
} else {
main(name, deps, callback);
}
};

define.amd = {};
define.amd = {
jQuery: true
};
}());

0 comments on commit 5fda114

Please sign in to comment.