Skip to content

Commit c000625

Browse files
committed
Updated to RequireJS 1.0.5
1 parent 2deb132 commit c000625

File tree

1 file changed

+141
-32
lines changed

1 file changed

+141
-32
lines changed

external/requirejs/require.js

Lines changed: 141 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
/** vim: et:ts=4:sw=4:sts=4
2-
* @license RequireJS 1.0.3 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
2+
* @license RequireJS 1.0.5 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
33
* Available via the MIT or new BSD license.
44
* see: http://github.com/jrburke/requirejs for details
55
*/
66
/*jslint strict: false, plusplus: false, sub: true */
7-
/*global window: false, navigator: false, document: false, importScripts: false,
8-
jQuery: false, clearInterval: false, setInterval: false, self: false,
9-
setTimeout: false, opera: false */
7+
/*global window, navigator, document, importScripts, jQuery, setTimeout, opera */
108

119
var requirejs, require, define;
1210
(function () {
1311
//Change this version number for each release.
14-
var version = "1.0.3",
12+
var version = "1.0.5",
1513
commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
1614
cjsRequireRegExp = /require\(\s*["']([^'"\s]+)["']\s*\)/g,
1715
currDirRegExp = /^\.\//,
@@ -37,8 +35,13 @@ var requirejs, require, define;
3735
interactiveScript = null,
3836
checkLoadedDepth = 0,
3937
useInteractive = false,
38+
reservedDependencies = {
39+
require: true,
40+
module: true,
41+
exports: true
42+
},
4043
req, cfg = {}, currentlyAddingScript, s, head, baseElement, scripts, script,
41-
src, subPath, mainScript, dataMain, i, ctx, jQueryCheck, checkLoadedTimeoutId;
44+
src, subPath, mainScript, dataMain, globalI, ctx, jQueryCheck, checkLoadedTimeoutId;
4245

4346
function isFunction(it) {
4447
return ostring.call(it) === "[object Function]";
@@ -323,7 +326,15 @@ var requirejs, require, define;
323326
url = urlMap[normalizedName];
324327
if (!url) {
325328
//Calculate url for the module, if it has a name.
326-
url = context.nameToUrl(normalizedName, null, parentModuleMap);
329+
//Use name here since nameToUrl also calls normalize,
330+
//and for relative names that are outside the baseUrl
331+
//this causes havoc. Was thinking of just removing
332+
//parentModuleMap to avoid extra normalization, but
333+
//normalize() still does a dot removal because of
334+
//issue #142, so just pass in name here and redo
335+
//the normalization. Paths outside baseUrl are just
336+
//messy to support.
337+
url = context.nameToUrl(name, null, parentModuleMap);
327338

328339
//Store the URL mapping for later.
329340
urlMap[normalizedName] = url;
@@ -383,8 +394,8 @@ var requirejs, require, define;
383394
* per module because of the implication of path mappings that may
384395
* need to be relative to the module name.
385396
*/
386-
function makeRequire(relModuleMap, enableBuildCallback) {
387-
var modRequire = makeContextModuleFunc(context.require, relModuleMap, enableBuildCallback);
397+
function makeRequire(relModuleMap, enableBuildCallback, altRequire) {
398+
var modRequire = makeContextModuleFunc(altRequire || context.require, relModuleMap, enableBuildCallback);
388399

389400
mixin(modRequire, {
390401
nameToUrl: makeContextModuleFunc(context.nameToUrl, relModuleMap),
@@ -599,7 +610,22 @@ var requirejs, require, define;
599610
//Use parentName here since the plugin's name is not reliable,
600611
//could be some weird string with no path that actually wants to
601612
//reference the parentName's path.
602-
plugin.load(name, makeRequire(map.parentMap, true), load, config);
613+
plugin.load(name, makeRequire(map.parentMap, true, function (deps, cb) {
614+
var moduleDeps = [],
615+
i, dep, depMap;
616+
//Convert deps to full names and hold on to them
617+
//for reference later, when figuring out if they
618+
//are blocked by a circular dependency.
619+
for (i = 0; (dep = deps[i]); i++) {
620+
depMap = makeModuleMap(dep, map.parentMap);
621+
deps[i] = depMap.fullName;
622+
if (!depMap.prefix) {
623+
moduleDeps.push(deps[i]);
624+
}
625+
}
626+
depManager.moduleDeps = (depManager.moduleDeps || []).concat(moduleDeps);
627+
return context.require(deps, cb);
628+
}), load, config);
603629
}
604630
}
605631

@@ -628,7 +654,7 @@ var requirejs, require, define;
628654
prefix = map.prefix,
629655
plugin = prefix ? plugins[prefix] ||
630656
(plugins[prefix] = defined[prefix]) : null,
631-
manager, created, pluginManager;
657+
manager, created, pluginManager, prefixMap;
632658

633659
if (fullName) {
634660
manager = managerCallbacks[fullName];
@@ -666,7 +692,18 @@ var requirejs, require, define;
666692
//If there is a plugin needed, but it is not loaded,
667693
//first load the plugin, then continue on.
668694
if (prefix && !plugin) {
669-
pluginManager = getManager(makeModuleMap(prefix), true);
695+
prefixMap = makeModuleMap(prefix);
696+
697+
//Clear out defined and urlFetched if the plugin was previously
698+
//loaded/defined, but not as full module (as in a build
699+
//situation). However, only do this work if the plugin is in
700+
//defined but does not have a module export value.
701+
if (prefix in defined && !defined[prefix]) {
702+
delete defined[prefix];
703+
delete urlFetched[prefixMap.url];
704+
}
705+
706+
pluginManager = getManager(prefixMap, true);
670707
pluginManager.add(function (plugin) {
671708
//Create a new manager for the normalized
672709
//resource ID and have it call this manager when
@@ -855,15 +892,62 @@ var requirejs, require, define;
855892
}
856893
};
857894

858-
function forceExec(manager, traced) {
859-
if (manager.isDone) {
860-
return undefined;
895+
function findCycle(manager, traced) {
896+
var fullName = manager.map.fullName,
897+
depArray = manager.depArray,
898+
fullyLoaded = true,
899+
i, depName, depManager, result;
900+
901+
if (manager.isDone || !fullName || !loaded[fullName]) {
902+
return result;
903+
}
904+
905+
//Found the cycle.
906+
if (traced[fullName]) {
907+
return manager;
908+
}
909+
910+
traced[fullName] = true;
911+
912+
//Trace through the dependencies.
913+
if (depArray) {
914+
for (i = 0; i < depArray.length; i++) {
915+
//Some array members may be null, like if a trailing comma
916+
//IE, so do the explicit [i] access and check if it has a value.
917+
depName = depArray[i];
918+
if (!loaded[depName] && !reservedDependencies[depName]) {
919+
fullyLoaded = false;
920+
break;
921+
}
922+
depManager = waiting[depName];
923+
if (depManager && !depManager.isDone && loaded[depName]) {
924+
result = findCycle(depManager, traced);
925+
if (result) {
926+
break;
927+
}
928+
}
929+
}
930+
if (!fullyLoaded) {
931+
//Discard the cycle that was found, since it cannot
932+
//be forced yet. Also clear this module from traced.
933+
result = undefined;
934+
delete traced[fullName];
935+
}
861936
}
862937

938+
return result;
939+
}
940+
941+
function forceExec(manager, traced) {
863942
var fullName = manager.map.fullName,
864943
depArray = manager.depArray,
865944
i, depName, depManager, prefix, prefixManager, value;
866945

946+
947+
if (manager.isDone || !fullName || !loaded[fullName]) {
948+
return undefined;
949+
}
950+
867951
if (fullName) {
868952
if (traced[fullName]) {
869953
return defined[fullName];
@@ -894,7 +978,7 @@ var requirejs, require, define;
894978
}
895979
}
896980

897-
return fullName ? defined[fullName] : undefined;
981+
return defined[fullName];
898982
}
899983

900984
/**
@@ -907,8 +991,9 @@ var requirejs, require, define;
907991
var waitInterval = config.waitSeconds * 1000,
908992
//It is possible to disable the wait interval by using waitSeconds of 0.
909993
expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
910-
noLoads = "", hasLoadedProp = false, stillLoading = false, prop,
911-
err, manager;
994+
noLoads = "", hasLoadedProp = false, stillLoading = false,
995+
cycleDeps = [],
996+
i, prop, err, manager, cycleManager, moduleDeps;
912997

913998
//If there are items still in the paused queue processing wait.
914999
//This is particularly important in the sync case where each paused
@@ -938,7 +1023,20 @@ var requirejs, require, define;
9381023
noLoads += prop + " ";
9391024
} else {
9401025
stillLoading = true;
941-
break;
1026+
if (prop.indexOf('!') === -1) {
1027+
//No reason to keep looking for unfinished
1028+
//loading. If the only stillLoading is a
1029+
//plugin resource though, keep going,
1030+
//because it may be that a plugin resource
1031+
//is waiting on a non-plugin cycle.
1032+
cycleDeps = [];
1033+
break;
1034+
} else {
1035+
moduleDeps = managerCallbacks[prop] && managerCallbacks[prop].moduleDeps;
1036+
if (moduleDeps) {
1037+
cycleDeps.push.apply(cycleDeps, moduleDeps);
1038+
}
1039+
}
9421040
}
9431041
}
9441042
}
@@ -957,7 +1055,23 @@ var requirejs, require, define;
9571055
err.requireModules = noLoads;
9581056
return req.onError(err);
9591057
}
960-
if (stillLoading || context.scriptCount) {
1058+
1059+
//If still loading but a plugin is waiting on a regular module cycle
1060+
//break the cycle.
1061+
if (stillLoading && cycleDeps.length) {
1062+
for (i = 0; (manager = waiting[cycleDeps[i]]); i++) {
1063+
if ((cycleManager = findCycle(manager, {}))) {
1064+
forceExec(cycleManager, {});
1065+
break;
1066+
}
1067+
}
1068+
1069+
}
1070+
1071+
//If still waiting on loads, and the waiting load is something
1072+
//other than a plugin resource, or there are still outstanding
1073+
//scripts, then just try back later.
1074+
if (!expired && (stillLoading || context.scriptCount)) {
9611075
//Something is still waiting to load. Wait for it, but only
9621076
//if a timeout is not already in effect.
9631077
if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {
@@ -1015,6 +1129,9 @@ var requirejs, require, define;
10151129
resume = function () {
10161130
var manager, map, url, i, p, args, fullName;
10171131

1132+
//Any defined modules in the global queue, intake them now.
1133+
context.takeGlobalQueue();
1134+
10181135
resumeDepth += 1;
10191136

10201137
if (context.scriptCount <= 0) {
@@ -1178,8 +1295,7 @@ var requirejs, require, define;
11781295
context.requireWait = false;
11791296
//But first, call resume to register any defined modules that may
11801297
//be in a data-main built file before the priority config
1181-
//call. Also grab any waiting define calls for this context.
1182-
context.takeGlobalQueue();
1298+
//call.
11831299
resume();
11841300

11851301
context.require(cfg.priority);
@@ -1256,10 +1372,6 @@ var requirejs, require, define;
12561372
//then resume the dependency processing.
12571373
if (!context.requireWait) {
12581374
while (!context.scriptCount && context.paused.length) {
1259-
//For built layers, there can be some defined
1260-
//modules waiting for intake into the context,
1261-
//in particular module plugins. Take them.
1262-
context.takeGlobalQueue();
12631375
resume();
12641376
}
12651377
}
@@ -1737,7 +1849,8 @@ var requirejs, require, define;
17371849
node = context && context.config && context.config.xhtml ?
17381850
document.createElementNS("http://www.w3.org/1999/xhtml", "html:script") :
17391851
document.createElement("script");
1740-
node.type = type || "text/javascript";
1852+
node.type = type || (context && context.config.scriptType) ||
1853+
"text/javascript";
17411854
node.charset = "utf-8";
17421855
//Use async so Gecko does not block on executing the script if something
17431856
//like a long-polling comet tag is being run first. Gecko likes
@@ -1822,7 +1935,7 @@ var requirejs, require, define;
18221935
//Figure out baseUrl. Get it from the script tag with require.js in it.
18231936
scripts = document.getElementsByTagName("script");
18241937

1825-
for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) {
1938+
for (globalI = scripts.length - 1; globalI > -1 && (script = scripts[globalI]); globalI--) {
18261939
//Set the "head" where we can append children by
18271940
//using the script's parent.
18281941
if (!head) {
@@ -1930,10 +2043,6 @@ var requirejs, require, define;
19302043
setTimeout(function () {
19312044
ctx.requireWait = false;
19322045

1933-
//Any modules included with the require.js file will be in the
1934-
//global queue, assign them to this context.
1935-
ctx.takeGlobalQueue();
1936-
19372046
if (!ctx.scriptCount) {
19382047
ctx.resume();
19392048
}

0 commit comments

Comments
 (0)