annotate node_modules/requirejs/require.js @ 69:333afcfd3f3a

added node_modules to project and fixed path to chronometer also added deps to installer script
author tzara <rc-web@kiben.net>
date Sat, 26 Oct 2013 14:12:50 +0100
parents
children 0ae87af84e2f
rev   line source
rc-web@69 1 /** vim: et:ts=4:sw=4:sts=4
rc-web@69 2 * @license RequireJS 2.1.9 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
rc-web@69 3 * Available via the MIT or new BSD license.
rc-web@69 4 * see: http://github.com/jrburke/requirejs for details
rc-web@69 5 */
rc-web@69 6 //Not using strict: uneven strict support in browsers, #392, and causes
rc-web@69 7 //problems with requirejs.exec()/transpiler plugins that may not be strict.
rc-web@69 8 /*jslint regexp: true, nomen: true, sloppy: true */
rc-web@69 9 /*global window, navigator, document, importScripts, setTimeout, opera */
rc-web@69 10
rc-web@69 11 var requirejs, require, define;
rc-web@69 12 (function (global) {
rc-web@69 13 var req, s, head, baseElement, dataMain, src,
rc-web@69 14 interactiveScript, currentlyAddingScript, mainScript, subPath,
rc-web@69 15 version = '2.1.9',
rc-web@69 16 commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
rc-web@69 17 cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
rc-web@69 18 jsSuffixRegExp = /\.js$/,
rc-web@69 19 currDirRegExp = /^\.\//,
rc-web@69 20 op = Object.prototype,
rc-web@69 21 ostring = op.toString,
rc-web@69 22 hasOwn = op.hasOwnProperty,
rc-web@69 23 ap = Array.prototype,
rc-web@69 24 apsp = ap.splice,
rc-web@69 25 isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document),
rc-web@69 26 isWebWorker = !isBrowser && typeof importScripts !== 'undefined',
rc-web@69 27 //PS3 indicates loaded and complete, but need to wait for complete
rc-web@69 28 //specifically. Sequence is 'loading', 'loaded', execution,
rc-web@69 29 // then 'complete'. The UA check is unfortunate, but not sure how
rc-web@69 30 //to feature test w/o causing perf issues.
rc-web@69 31 readyRegExp = isBrowser && navigator.platform === 'PLAYSTATION 3' ?
rc-web@69 32 /^complete$/ : /^(complete|loaded)$/,
rc-web@69 33 defContextName = '_',
rc-web@69 34 //Oh the tragedy, detecting opera. See the usage of isOpera for reason.
rc-web@69 35 isOpera = typeof opera !== 'undefined' && opera.toString() === '[object Opera]',
rc-web@69 36 contexts = {},
rc-web@69 37 cfg = {},
rc-web@69 38 globalDefQueue = [],
rc-web@69 39 useInteractive = false;
rc-web@69 40
rc-web@69 41 function isFunction(it) {
rc-web@69 42 return ostring.call(it) === '[object Function]';
rc-web@69 43 }
rc-web@69 44
rc-web@69 45 function isArray(it) {
rc-web@69 46 return ostring.call(it) === '[object Array]';
rc-web@69 47 }
rc-web@69 48
rc-web@69 49 /**
rc-web@69 50 * Helper function for iterating over an array. If the func returns
rc-web@69 51 * a true value, it will break out of the loop.
rc-web@69 52 */
rc-web@69 53 function each(ary, func) {
rc-web@69 54 if (ary) {
rc-web@69 55 var i;
rc-web@69 56 for (i = 0; i < ary.length; i += 1) {
rc-web@69 57 if (ary[i] && func(ary[i], i, ary)) {
rc-web@69 58 break;
rc-web@69 59 }
rc-web@69 60 }
rc-web@69 61 }
rc-web@69 62 }
rc-web@69 63
rc-web@69 64 /**
rc-web@69 65 * Helper function for iterating over an array backwards. If the func
rc-web@69 66 * returns a true value, it will break out of the loop.
rc-web@69 67 */
rc-web@69 68 function eachReverse(ary, func) {
rc-web@69 69 if (ary) {
rc-web@69 70 var i;
rc-web@69 71 for (i = ary.length - 1; i > -1; i -= 1) {
rc-web@69 72 if (ary[i] && func(ary[i], i, ary)) {
rc-web@69 73 break;
rc-web@69 74 }
rc-web@69 75 }
rc-web@69 76 }
rc-web@69 77 }
rc-web@69 78
rc-web@69 79 function hasProp(obj, prop) {
rc-web@69 80 return hasOwn.call(obj, prop);
rc-web@69 81 }
rc-web@69 82
rc-web@69 83 function getOwn(obj, prop) {
rc-web@69 84 return hasProp(obj, prop) && obj[prop];
rc-web@69 85 }
rc-web@69 86
rc-web@69 87 /**
rc-web@69 88 * Cycles over properties in an object and calls a function for each
rc-web@69 89 * property value. If the function returns a truthy value, then the
rc-web@69 90 * iteration is stopped.
rc-web@69 91 */
rc-web@69 92 function eachProp(obj, func) {
rc-web@69 93 var prop;
rc-web@69 94 for (prop in obj) {
rc-web@69 95 if (hasProp(obj, prop)) {
rc-web@69 96 if (func(obj[prop], prop)) {
rc-web@69 97 break;
rc-web@69 98 }
rc-web@69 99 }
rc-web@69 100 }
rc-web@69 101 }
rc-web@69 102
rc-web@69 103 /**
rc-web@69 104 * Simple function to mix in properties from source into target,
rc-web@69 105 * but only if target does not already have a property of the same name.
rc-web@69 106 */
rc-web@69 107 function mixin(target, source, force, deepStringMixin) {
rc-web@69 108 if (source) {
rc-web@69 109 eachProp(source, function (value, prop) {
rc-web@69 110 if (force || !hasProp(target, prop)) {
rc-web@69 111 if (deepStringMixin && typeof value !== 'string') {
rc-web@69 112 if (!target[prop]) {
rc-web@69 113 target[prop] = {};
rc-web@69 114 }
rc-web@69 115 mixin(target[prop], value, force, deepStringMixin);
rc-web@69 116 } else {
rc-web@69 117 target[prop] = value;
rc-web@69 118 }
rc-web@69 119 }
rc-web@69 120 });
rc-web@69 121 }
rc-web@69 122 return target;
rc-web@69 123 }
rc-web@69 124
rc-web@69 125 //Similar to Function.prototype.bind, but the 'this' object is specified
rc-web@69 126 //first, since it is easier to read/figure out what 'this' will be.
rc-web@69 127 function bind(obj, fn) {
rc-web@69 128 return function () {
rc-web@69 129 return fn.apply(obj, arguments);
rc-web@69 130 };
rc-web@69 131 }
rc-web@69 132
rc-web@69 133 function scripts() {
rc-web@69 134 return document.getElementsByTagName('script');
rc-web@69 135 }
rc-web@69 136
rc-web@69 137 function defaultOnError(err) {
rc-web@69 138 throw err;
rc-web@69 139 }
rc-web@69 140
rc-web@69 141 //Allow getting a global that expressed in
rc-web@69 142 //dot notation, like 'a.b.c'.
rc-web@69 143 function getGlobal(value) {
rc-web@69 144 if (!value) {
rc-web@69 145 return value;
rc-web@69 146 }
rc-web@69 147 var g = global;
rc-web@69 148 each(value.split('.'), function (part) {
rc-web@69 149 g = g[part];
rc-web@69 150 });
rc-web@69 151 return g;
rc-web@69 152 }
rc-web@69 153
rc-web@69 154 /**
rc-web@69 155 * Constructs an error with a pointer to an URL with more information.
rc-web@69 156 * @param {String} id the error ID that maps to an ID on a web page.
rc-web@69 157 * @param {String} message human readable error.
rc-web@69 158 * @param {Error} [err] the original error, if there is one.
rc-web@69 159 *
rc-web@69 160 * @returns {Error}
rc-web@69 161 */
rc-web@69 162 function makeError(id, msg, err, requireModules) {
rc-web@69 163 var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#' + id);
rc-web@69 164 e.requireType = id;
rc-web@69 165 e.requireModules = requireModules;
rc-web@69 166 if (err) {
rc-web@69 167 e.originalError = err;
rc-web@69 168 }
rc-web@69 169 return e;
rc-web@69 170 }
rc-web@69 171
rc-web@69 172 if (typeof define !== 'undefined') {
rc-web@69 173 //If a define is already in play via another AMD loader,
rc-web@69 174 //do not overwrite.
rc-web@69 175 return;
rc-web@69 176 }
rc-web@69 177
rc-web@69 178 if (typeof requirejs !== 'undefined') {
rc-web@69 179 if (isFunction(requirejs)) {
rc-web@69 180 //Do not overwrite and existing requirejs instance.
rc-web@69 181 return;
rc-web@69 182 }
rc-web@69 183 cfg = requirejs;
rc-web@69 184 requirejs = undefined;
rc-web@69 185 }
rc-web@69 186
rc-web@69 187 //Allow for a require config object
rc-web@69 188 if (typeof require !== 'undefined' && !isFunction(require)) {
rc-web@69 189 //assume it is a config object.
rc-web@69 190 cfg = require;
rc-web@69 191 require = undefined;
rc-web@69 192 }
rc-web@69 193
rc-web@69 194 function newContext(contextName) {
rc-web@69 195 var inCheckLoaded, Module, context, handlers,
rc-web@69 196 checkLoadedTimeoutId,
rc-web@69 197 config = {
rc-web@69 198 //Defaults. Do not set a default for map
rc-web@69 199 //config to speed up normalize(), which
rc-web@69 200 //will run faster if there is no default.
rc-web@69 201 waitSeconds: 7,
rc-web@69 202 baseUrl: './',
rc-web@69 203 paths: {},
rc-web@69 204 pkgs: {},
rc-web@69 205 shim: {},
rc-web@69 206 config: {}
rc-web@69 207 },
rc-web@69 208 registry = {},
rc-web@69 209 //registry of just enabled modules, to speed
rc-web@69 210 //cycle breaking code when lots of modules
rc-web@69 211 //are registered, but not activated.
rc-web@69 212 enabledRegistry = {},
rc-web@69 213 undefEvents = {},
rc-web@69 214 defQueue = [],
rc-web@69 215 defined = {},
rc-web@69 216 urlFetched = {},
rc-web@69 217 requireCounter = 1,
rc-web@69 218 unnormalizedCounter = 1;
rc-web@69 219
rc-web@69 220 /**
rc-web@69 221 * Trims the . and .. from an array of path segments.
rc-web@69 222 * It will keep a leading path segment if a .. will become
rc-web@69 223 * the first path segment, to help with module name lookups,
rc-web@69 224 * which act like paths, but can be remapped. But the end result,
rc-web@69 225 * all paths that use this function should look normalized.
rc-web@69 226 * NOTE: this method MODIFIES the input array.
rc-web@69 227 * @param {Array} ary the array of path segments.
rc-web@69 228 */
rc-web@69 229 function trimDots(ary) {
rc-web@69 230 var i, part;
rc-web@69 231 for (i = 0; ary[i]; i += 1) {
rc-web@69 232 part = ary[i];
rc-web@69 233 if (part === '.') {
rc-web@69 234 ary.splice(i, 1);
rc-web@69 235 i -= 1;
rc-web@69 236 } else if (part === '..') {
rc-web@69 237 if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
rc-web@69 238 //End of the line. Keep at least one non-dot
rc-web@69 239 //path segment at the front so it can be mapped
rc-web@69 240 //correctly to disk. Otherwise, there is likely
rc-web@69 241 //no path mapping for a path starting with '..'.
rc-web@69 242 //This can still fail, but catches the most reasonable
rc-web@69 243 //uses of ..
rc-web@69 244 break;
rc-web@69 245 } else if (i > 0) {
rc-web@69 246 ary.splice(i - 1, 2);
rc-web@69 247 i -= 2;
rc-web@69 248 }
rc-web@69 249 }
rc-web@69 250 }
rc-web@69 251 }
rc-web@69 252
rc-web@69 253 /**
rc-web@69 254 * Given a relative module name, like ./something, normalize it to
rc-web@69 255 * a real name that can be mapped to a path.
rc-web@69 256 * @param {String} name the relative name
rc-web@69 257 * @param {String} baseName a real name that the name arg is relative
rc-web@69 258 * to.
rc-web@69 259 * @param {Boolean} applyMap apply the map config to the value. Should
rc-web@69 260 * only be done if this normalization is for a dependency ID.
rc-web@69 261 * @returns {String} normalized name
rc-web@69 262 */
rc-web@69 263 function normalize(name, baseName, applyMap) {
rc-web@69 264 var pkgName, pkgConfig, mapValue, nameParts, i, j, nameSegment,
rc-web@69 265 foundMap, foundI, foundStarMap, starI,
rc-web@69 266 baseParts = baseName && baseName.split('/'),
rc-web@69 267 normalizedBaseParts = baseParts,
rc-web@69 268 map = config.map,
rc-web@69 269 starMap = map && map['*'];
rc-web@69 270
rc-web@69 271 //Adjust any relative paths.
rc-web@69 272 if (name && name.charAt(0) === '.') {
rc-web@69 273 //If have a base name, try to normalize against it,
rc-web@69 274 //otherwise, assume it is a top-level require that will
rc-web@69 275 //be relative to baseUrl in the end.
rc-web@69 276 if (baseName) {
rc-web@69 277 if (getOwn(config.pkgs, baseName)) {
rc-web@69 278 //If the baseName is a package name, then just treat it as one
rc-web@69 279 //name to concat the name with.
rc-web@69 280 normalizedBaseParts = baseParts = [baseName];
rc-web@69 281 } else {
rc-web@69 282 //Convert baseName to array, and lop off the last part,
rc-web@69 283 //so that . matches that 'directory' and not name of the baseName's
rc-web@69 284 //module. For instance, baseName of 'one/two/three', maps to
rc-web@69 285 //'one/two/three.js', but we want the directory, 'one/two' for
rc-web@69 286 //this normalization.
rc-web@69 287 normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
rc-web@69 288 }
rc-web@69 289
rc-web@69 290 name = normalizedBaseParts.concat(name.split('/'));
rc-web@69 291 trimDots(name);
rc-web@69 292
rc-web@69 293 //Some use of packages may use a . path to reference the
rc-web@69 294 //'main' module name, so normalize for that.
rc-web@69 295 pkgConfig = getOwn(config.pkgs, (pkgName = name[0]));
rc-web@69 296 name = name.join('/');
rc-web@69 297 if (pkgConfig && name === pkgName + '/' + pkgConfig.main) {
rc-web@69 298 name = pkgName;
rc-web@69 299 }
rc-web@69 300 } else if (name.indexOf('./') === 0) {
rc-web@69 301 // No baseName, so this is ID is resolved relative
rc-web@69 302 // to baseUrl, pull off the leading dot.
rc-web@69 303 name = name.substring(2);
rc-web@69 304 }
rc-web@69 305 }
rc-web@69 306
rc-web@69 307 //Apply map config if available.
rc-web@69 308 if (applyMap && map && (baseParts || starMap)) {
rc-web@69 309 nameParts = name.split('/');
rc-web@69 310
rc-web@69 311 for (i = nameParts.length; i > 0; i -= 1) {
rc-web@69 312 nameSegment = nameParts.slice(0, i).join('/');
rc-web@69 313
rc-web@69 314 if (baseParts) {
rc-web@69 315 //Find the longest baseName segment match in the config.
rc-web@69 316 //So, do joins on the biggest to smallest lengths of baseParts.
rc-web@69 317 for (j = baseParts.length; j > 0; j -= 1) {
rc-web@69 318 mapValue = getOwn(map, baseParts.slice(0, j).join('/'));
rc-web@69 319
rc-web@69 320 //baseName segment has config, find if it has one for
rc-web@69 321 //this name.
rc-web@69 322 if (mapValue) {
rc-web@69 323 mapValue = getOwn(mapValue, nameSegment);
rc-web@69 324 if (mapValue) {
rc-web@69 325 //Match, update name to the new value.
rc-web@69 326 foundMap = mapValue;
rc-web@69 327 foundI = i;
rc-web@69 328 break;
rc-web@69 329 }
rc-web@69 330 }
rc-web@69 331 }
rc-web@69 332 }
rc-web@69 333
rc-web@69 334 if (foundMap) {
rc-web@69 335 break;
rc-web@69 336 }
rc-web@69 337
rc-web@69 338 //Check for a star map match, but just hold on to it,
rc-web@69 339 //if there is a shorter segment match later in a matching
rc-web@69 340 //config, then favor over this star map.
rc-web@69 341 if (!foundStarMap && starMap && getOwn(starMap, nameSegment)) {
rc-web@69 342 foundStarMap = getOwn(starMap, nameSegment);
rc-web@69 343 starI = i;
rc-web@69 344 }
rc-web@69 345 }
rc-web@69 346
rc-web@69 347 if (!foundMap && foundStarMap) {
rc-web@69 348 foundMap = foundStarMap;
rc-web@69 349 foundI = starI;
rc-web@69 350 }
rc-web@69 351
rc-web@69 352 if (foundMap) {
rc-web@69 353 nameParts.splice(0, foundI, foundMap);
rc-web@69 354 name = nameParts.join('/');
rc-web@69 355 }
rc-web@69 356 }
rc-web@69 357
rc-web@69 358 return name;
rc-web@69 359 }
rc-web@69 360
rc-web@69 361 function removeScript(name) {
rc-web@69 362 if (isBrowser) {
rc-web@69 363 each(scripts(), function (scriptNode) {
rc-web@69 364 if (scriptNode.getAttribute('data-requiremodule') === name &&
rc-web@69 365 scriptNode.getAttribute('data-requirecontext') === context.contextName) {
rc-web@69 366 scriptNode.parentNode.removeChild(scriptNode);
rc-web@69 367 return true;
rc-web@69 368 }
rc-web@69 369 });
rc-web@69 370 }
rc-web@69 371 }
rc-web@69 372
rc-web@69 373 function hasPathFallback(id) {
rc-web@69 374 var pathConfig = getOwn(config.paths, id);
rc-web@69 375 if (pathConfig && isArray(pathConfig) && pathConfig.length > 1) {
rc-web@69 376 //Pop off the first array value, since it failed, and
rc-web@69 377 //retry
rc-web@69 378 pathConfig.shift();
rc-web@69 379 context.require.undef(id);
rc-web@69 380 context.require([id]);
rc-web@69 381 return true;
rc-web@69 382 }
rc-web@69 383 }
rc-web@69 384
rc-web@69 385 //Turns a plugin!resource to [plugin, resource]
rc-web@69 386 //with the plugin being undefined if the name
rc-web@69 387 //did not have a plugin prefix.
rc-web@69 388 function splitPrefix(name) {
rc-web@69 389 var prefix,
rc-web@69 390 index = name ? name.indexOf('!') : -1;
rc-web@69 391 if (index > -1) {
rc-web@69 392 prefix = name.substring(0, index);
rc-web@69 393 name = name.substring(index + 1, name.length);
rc-web@69 394 }
rc-web@69 395 return [prefix, name];
rc-web@69 396 }
rc-web@69 397
rc-web@69 398 /**
rc-web@69 399 * Creates a module mapping that includes plugin prefix, module
rc-web@69 400 * name, and path. If parentModuleMap is provided it will
rc-web@69 401 * also normalize the name via require.normalize()
rc-web@69 402 *
rc-web@69 403 * @param {String} name the module name
rc-web@69 404 * @param {String} [parentModuleMap] parent module map
rc-web@69 405 * for the module name, used to resolve relative names.
rc-web@69 406 * @param {Boolean} isNormalized: is the ID already normalized.
rc-web@69 407 * This is true if this call is done for a define() module ID.
rc-web@69 408 * @param {Boolean} applyMap: apply the map config to the ID.
rc-web@69 409 * Should only be true if this map is for a dependency.
rc-web@69 410 *
rc-web@69 411 * @returns {Object}
rc-web@69 412 */
rc-web@69 413 function makeModuleMap(name, parentModuleMap, isNormalized, applyMap) {
rc-web@69 414 var url, pluginModule, suffix, nameParts,
rc-web@69 415 prefix = null,
rc-web@69 416 parentName = parentModuleMap ? parentModuleMap.name : null,
rc-web@69 417 originalName = name,
rc-web@69 418 isDefine = true,
rc-web@69 419 normalizedName = '';
rc-web@69 420
rc-web@69 421 //If no name, then it means it is a require call, generate an
rc-web@69 422 //internal name.
rc-web@69 423 if (!name) {
rc-web@69 424 isDefine = false;
rc-web@69 425 name = '_@r' + (requireCounter += 1);
rc-web@69 426 }
rc-web@69 427
rc-web@69 428 nameParts = splitPrefix(name);
rc-web@69 429 prefix = nameParts[0];
rc-web@69 430 name = nameParts[1];
rc-web@69 431
rc-web@69 432 if (prefix) {
rc-web@69 433 prefix = normalize(prefix, parentName, applyMap);
rc-web@69 434 pluginModule = getOwn(defined, prefix);
rc-web@69 435 }
rc-web@69 436
rc-web@69 437 //Account for relative paths if there is a base name.
rc-web@69 438 if (name) {
rc-web@69 439 if (prefix) {
rc-web@69 440 if (pluginModule && pluginModule.normalize) {
rc-web@69 441 //Plugin is loaded, use its normalize method.
rc-web@69 442 normalizedName = pluginModule.normalize(name, function (name) {
rc-web@69 443 return normalize(name, parentName, applyMap);
rc-web@69 444 });
rc-web@69 445 } else {
rc-web@69 446 normalizedName = normalize(name, parentName, applyMap);
rc-web@69 447 }
rc-web@69 448 } else {
rc-web@69 449 //A regular module.
rc-web@69 450 normalizedName = normalize(name, parentName, applyMap);
rc-web@69 451
rc-web@69 452 //Normalized name may be a plugin ID due to map config
rc-web@69 453 //application in normalize. The map config values must
rc-web@69 454 //already be normalized, so do not need to redo that part.
rc-web@69 455 nameParts = splitPrefix(normalizedName);
rc-web@69 456 prefix = nameParts[0];
rc-web@69 457 normalizedName = nameParts[1];
rc-web@69 458 isNormalized = true;
rc-web@69 459
rc-web@69 460 url = context.nameToUrl(normalizedName);
rc-web@69 461 }
rc-web@69 462 }
rc-web@69 463
rc-web@69 464 //If the id is a plugin id that cannot be determined if it needs
rc-web@69 465 //normalization, stamp it with a unique ID so two matching relative
rc-web@69 466 //ids that may conflict can be separate.
rc-web@69 467 suffix = prefix && !pluginModule && !isNormalized ?
rc-web@69 468 '_unnormalized' + (unnormalizedCounter += 1) :
rc-web@69 469 '';
rc-web@69 470
rc-web@69 471 return {
rc-web@69 472 prefix: prefix,
rc-web@69 473 name: normalizedName,
rc-web@69 474 parentMap: parentModuleMap,
rc-web@69 475 unnormalized: !!suffix,
rc-web@69 476 url: url,
rc-web@69 477 originalName: originalName,
rc-web@69 478 isDefine: isDefine,
rc-web@69 479 id: (prefix ?
rc-web@69 480 prefix + '!' + normalizedName :
rc-web@69 481 normalizedName) + suffix
rc-web@69 482 };
rc-web@69 483 }
rc-web@69 484
rc-web@69 485 function getModule(depMap) {
rc-web@69 486 var id = depMap.id,
rc-web@69 487 mod = getOwn(registry, id);
rc-web@69 488
rc-web@69 489 if (!mod) {
rc-web@69 490 mod = registry[id] = new context.Module(depMap);
rc-web@69 491 }
rc-web@69 492
rc-web@69 493 return mod;
rc-web@69 494 }
rc-web@69 495
rc-web@69 496 function on(depMap, name, fn) {
rc-web@69 497 var id = depMap.id,
rc-web@69 498 mod = getOwn(registry, id);
rc-web@69 499
rc-web@69 500 if (hasProp(defined, id) &&
rc-web@69 501 (!mod || mod.defineEmitComplete)) {
rc-web@69 502 if (name === 'defined') {
rc-web@69 503 fn(defined[id]);
rc-web@69 504 }
rc-web@69 505 } else {
rc-web@69 506 mod = getModule(depMap);
rc-web@69 507 if (mod.error && name === 'error') {
rc-web@69 508 fn(mod.error);
rc-web@69 509 } else {
rc-web@69 510 mod.on(name, fn);
rc-web@69 511 }
rc-web@69 512 }
rc-web@69 513 }
rc-web@69 514
rc-web@69 515 function onError(err, errback) {
rc-web@69 516 var ids = err.requireModules,
rc-web@69 517 notified = false;
rc-web@69 518
rc-web@69 519 if (errback) {
rc-web@69 520 errback(err);
rc-web@69 521 } else {
rc-web@69 522 each(ids, function (id) {
rc-web@69 523 var mod = getOwn(registry, id);
rc-web@69 524 if (mod) {
rc-web@69 525 //Set error on module, so it skips timeout checks.
rc-web@69 526 mod.error = err;
rc-web@69 527 if (mod.events.error) {
rc-web@69 528 notified = true;
rc-web@69 529 mod.emit('error', err);
rc-web@69 530 }
rc-web@69 531 }
rc-web@69 532 });
rc-web@69 533
rc-web@69 534 if (!notified) {
rc-web@69 535 req.onError(err);
rc-web@69 536 }
rc-web@69 537 }
rc-web@69 538 }
rc-web@69 539
rc-web@69 540 /**
rc-web@69 541 * Internal method to transfer globalQueue items to this context's
rc-web@69 542 * defQueue.
rc-web@69 543 */
rc-web@69 544 function takeGlobalQueue() {
rc-web@69 545 //Push all the globalDefQueue items into the context's defQueue
rc-web@69 546 if (globalDefQueue.length) {
rc-web@69 547 //Array splice in the values since the context code has a
rc-web@69 548 //local var ref to defQueue, so cannot just reassign the one
rc-web@69 549 //on context.
rc-web@69 550 apsp.apply(defQueue,
rc-web@69 551 [defQueue.length - 1, 0].concat(globalDefQueue));
rc-web@69 552 globalDefQueue = [];
rc-web@69 553 }
rc-web@69 554 }
rc-web@69 555
rc-web@69 556 handlers = {
rc-web@69 557 'require': function (mod) {
rc-web@69 558 if (mod.require) {
rc-web@69 559 return mod.require;
rc-web@69 560 } else {
rc-web@69 561 return (mod.require = context.makeRequire(mod.map));
rc-web@69 562 }
rc-web@69 563 },
rc-web@69 564 'exports': function (mod) {
rc-web@69 565 mod.usingExports = true;
rc-web@69 566 if (mod.map.isDefine) {
rc-web@69 567 if (mod.exports) {
rc-web@69 568 return mod.exports;
rc-web@69 569 } else {
rc-web@69 570 return (mod.exports = defined[mod.map.id] = {});
rc-web@69 571 }
rc-web@69 572 }
rc-web@69 573 },
rc-web@69 574 'module': function (mod) {
rc-web@69 575 if (mod.module) {
rc-web@69 576 return mod.module;
rc-web@69 577 } else {
rc-web@69 578 return (mod.module = {
rc-web@69 579 id: mod.map.id,
rc-web@69 580 uri: mod.map.url,
rc-web@69 581 config: function () {
rc-web@69 582 var c,
rc-web@69 583 pkg = getOwn(config.pkgs, mod.map.id);
rc-web@69 584 // For packages, only support config targeted
rc-web@69 585 // at the main module.
rc-web@69 586 c = pkg ? getOwn(config.config, mod.map.id + '/' + pkg.main) :
rc-web@69 587 getOwn(config.config, mod.map.id);
rc-web@69 588 return c || {};
rc-web@69 589 },
rc-web@69 590 exports: defined[mod.map.id]
rc-web@69 591 });
rc-web@69 592 }
rc-web@69 593 }
rc-web@69 594 };
rc-web@69 595
rc-web@69 596 function cleanRegistry(id) {
rc-web@69 597 //Clean up machinery used for waiting modules.
rc-web@69 598 delete registry[id];
rc-web@69 599 delete enabledRegistry[id];
rc-web@69 600 }
rc-web@69 601
rc-web@69 602 function breakCycle(mod, traced, processed) {
rc-web@69 603 var id = mod.map.id;
rc-web@69 604
rc-web@69 605 if (mod.error) {
rc-web@69 606 mod.emit('error', mod.error);
rc-web@69 607 } else {
rc-web@69 608 traced[id] = true;
rc-web@69 609 each(mod.depMaps, function (depMap, i) {
rc-web@69 610 var depId = depMap.id,
rc-web@69 611 dep = getOwn(registry, depId);
rc-web@69 612
rc-web@69 613 //Only force things that have not completed
rc-web@69 614 //being defined, so still in the registry,
rc-web@69 615 //and only if it has not been matched up
rc-web@69 616 //in the module already.
rc-web@69 617 if (dep && !mod.depMatched[i] && !processed[depId]) {
rc-web@69 618 if (getOwn(traced, depId)) {
rc-web@69 619 mod.defineDep(i, defined[depId]);
rc-web@69 620 mod.check(); //pass false?
rc-web@69 621 } else {
rc-web@69 622 breakCycle(dep, traced, processed);
rc-web@69 623 }
rc-web@69 624 }
rc-web@69 625 });
rc-web@69 626 processed[id] = true;
rc-web@69 627 }
rc-web@69 628 }
rc-web@69 629
rc-web@69 630 function checkLoaded() {
rc-web@69 631 var map, modId, err, usingPathFallback,
rc-web@69 632 waitInterval = config.waitSeconds * 1000,
rc-web@69 633 //It is possible to disable the wait interval by using waitSeconds of 0.
rc-web@69 634 expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
rc-web@69 635 noLoads = [],
rc-web@69 636 reqCalls = [],
rc-web@69 637 stillLoading = false,
rc-web@69 638 needCycleCheck = true;
rc-web@69 639
rc-web@69 640 //Do not bother if this call was a result of a cycle break.
rc-web@69 641 if (inCheckLoaded) {
rc-web@69 642 return;
rc-web@69 643 }
rc-web@69 644
rc-web@69 645 inCheckLoaded = true;
rc-web@69 646
rc-web@69 647 //Figure out the state of all the modules.
rc-web@69 648 eachProp(enabledRegistry, function (mod) {
rc-web@69 649 map = mod.map;
rc-web@69 650 modId = map.id;
rc-web@69 651
rc-web@69 652 //Skip things that are not enabled or in error state.
rc-web@69 653 if (!mod.enabled) {
rc-web@69 654 return;
rc-web@69 655 }
rc-web@69 656
rc-web@69 657 if (!map.isDefine) {
rc-web@69 658 reqCalls.push(mod);
rc-web@69 659 }
rc-web@69 660
rc-web@69 661 if (!mod.error) {
rc-web@69 662 //If the module should be executed, and it has not
rc-web@69 663 //been inited and time is up, remember it.
rc-web@69 664 if (!mod.inited && expired) {
rc-web@69 665 if (hasPathFallback(modId)) {
rc-web@69 666 usingPathFallback = true;
rc-web@69 667 stillLoading = true;
rc-web@69 668 } else {
rc-web@69 669 noLoads.push(modId);
rc-web@69 670 removeScript(modId);
rc-web@69 671 }
rc-web@69 672 } else if (!mod.inited && mod.fetched && map.isDefine) {
rc-web@69 673 stillLoading = true;
rc-web@69 674 if (!map.prefix) {
rc-web@69 675 //No reason to keep looking for unfinished
rc-web@69 676 //loading. If the only stillLoading is a
rc-web@69 677 //plugin resource though, keep going,
rc-web@69 678 //because it may be that a plugin resource
rc-web@69 679 //is waiting on a non-plugin cycle.
rc-web@69 680 return (needCycleCheck = false);
rc-web@69 681 }
rc-web@69 682 }
rc-web@69 683 }
rc-web@69 684 });
rc-web@69 685
rc-web@69 686 if (expired && noLoads.length) {
rc-web@69 687 //If wait time expired, throw error of unloaded modules.
rc-web@69 688 err = makeError('timeout', 'Load timeout for modules: ' + noLoads, null, noLoads);
rc-web@69 689 err.contextName = context.contextName;
rc-web@69 690 return onError(err);
rc-web@69 691 }
rc-web@69 692
rc-web@69 693 //Not expired, check for a cycle.
rc-web@69 694 if (needCycleCheck) {
rc-web@69 695 each(reqCalls, function (mod) {
rc-web@69 696 breakCycle(mod, {}, {});
rc-web@69 697 });
rc-web@69 698 }
rc-web@69 699
rc-web@69 700 //If still waiting on loads, and the waiting load is something
rc-web@69 701 //other than a plugin resource, or there are still outstanding
rc-web@69 702 //scripts, then just try back later.
rc-web@69 703 if ((!expired || usingPathFallback) && stillLoading) {
rc-web@69 704 //Something is still waiting to load. Wait for it, but only
rc-web@69 705 //if a timeout is not already in effect.
rc-web@69 706 if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {
rc-web@69 707 checkLoadedTimeoutId = setTimeout(function () {
rc-web@69 708 checkLoadedTimeoutId = 0;
rc-web@69 709 checkLoaded();
rc-web@69 710 }, 50);
rc-web@69 711 }
rc-web@69 712 }
rc-web@69 713
rc-web@69 714 inCheckLoaded = false;
rc-web@69 715 }
rc-web@69 716
rc-web@69 717 Module = function (map) {
rc-web@69 718 this.events = getOwn(undefEvents, map.id) || {};
rc-web@69 719 this.map = map;
rc-web@69 720 this.shim = getOwn(config.shim, map.id);
rc-web@69 721 this.depExports = [];
rc-web@69 722 this.depMaps = [];
rc-web@69 723 this.depMatched = [];
rc-web@69 724 this.pluginMaps = {};
rc-web@69 725 this.depCount = 0;
rc-web@69 726
rc-web@69 727 /* this.exports this.factory
rc-web@69 728 this.depMaps = [],
rc-web@69 729 this.enabled, this.fetched
rc-web@69 730 */
rc-web@69 731 };
rc-web@69 732
rc-web@69 733 Module.prototype = {
rc-web@69 734 init: function (depMaps, factory, errback, options) {
rc-web@69 735 options = options || {};
rc-web@69 736
rc-web@69 737 //Do not do more inits if already done. Can happen if there
rc-web@69 738 //are multiple define calls for the same module. That is not
rc-web@69 739 //a normal, common case, but it is also not unexpected.
rc-web@69 740 if (this.inited) {
rc-web@69 741 return;
rc-web@69 742 }
rc-web@69 743
rc-web@69 744 this.factory = factory;
rc-web@69 745
rc-web@69 746 if (errback) {
rc-web@69 747 //Register for errors on this module.
rc-web@69 748 this.on('error', errback);
rc-web@69 749 } else if (this.events.error) {
rc-web@69 750 //If no errback already, but there are error listeners
rc-web@69 751 //on this module, set up an errback to pass to the deps.
rc-web@69 752 errback = bind(this, function (err) {
rc-web@69 753 this.emit('error', err);
rc-web@69 754 });
rc-web@69 755 }
rc-web@69 756
rc-web@69 757 //Do a copy of the dependency array, so that
rc-web@69 758 //source inputs are not modified. For example
rc-web@69 759 //"shim" deps are passed in here directly, and
rc-web@69 760 //doing a direct modification of the depMaps array
rc-web@69 761 //would affect that config.
rc-web@69 762 this.depMaps = depMaps && depMaps.slice(0);
rc-web@69 763
rc-web@69 764 this.errback = errback;
rc-web@69 765
rc-web@69 766 //Indicate this module has be initialized
rc-web@69 767 this.inited = true;
rc-web@69 768
rc-web@69 769 this.ignore = options.ignore;
rc-web@69 770
rc-web@69 771 //Could have option to init this module in enabled mode,
rc-web@69 772 //or could have been previously marked as enabled. However,
rc-web@69 773 //the dependencies are not known until init is called. So
rc-web@69 774 //if enabled previously, now trigger dependencies as enabled.
rc-web@69 775 if (options.enabled || this.enabled) {
rc-web@69 776 //Enable this module and dependencies.
rc-web@69 777 //Will call this.check()
rc-web@69 778 this.enable();
rc-web@69 779 } else {
rc-web@69 780 this.check();
rc-web@69 781 }
rc-web@69 782 },
rc-web@69 783
rc-web@69 784 defineDep: function (i, depExports) {
rc-web@69 785 //Because of cycles, defined callback for a given
rc-web@69 786 //export can be called more than once.
rc-web@69 787 if (!this.depMatched[i]) {
rc-web@69 788 this.depMatched[i] = true;
rc-web@69 789 this.depCount -= 1;
rc-web@69 790 this.depExports[i] = depExports;
rc-web@69 791 }
rc-web@69 792 },
rc-web@69 793
rc-web@69 794 fetch: function () {
rc-web@69 795 if (this.fetched) {
rc-web@69 796 return;
rc-web@69 797 }
rc-web@69 798 this.fetched = true;
rc-web@69 799
rc-web@69 800 context.startTime = (new Date()).getTime();
rc-web@69 801
rc-web@69 802 var map = this.map;
rc-web@69 803
rc-web@69 804 //If the manager is for a plugin managed resource,
rc-web@69 805 //ask the plugin to load it now.
rc-web@69 806 if (this.shim) {
rc-web@69 807 context.makeRequire(this.map, {
rc-web@69 808 enableBuildCallback: true
rc-web@69 809 })(this.shim.deps || [], bind(this, function () {
rc-web@69 810 return map.prefix ? this.callPlugin() : this.load();
rc-web@69 811 }));
rc-web@69 812 } else {
rc-web@69 813 //Regular dependency.
rc-web@69 814 return map.prefix ? this.callPlugin() : this.load();
rc-web@69 815 }
rc-web@69 816 },
rc-web@69 817
rc-web@69 818 load: function () {
rc-web@69 819 var url = this.map.url;
rc-web@69 820
rc-web@69 821 //Regular dependency.
rc-web@69 822 if (!urlFetched[url]) {
rc-web@69 823 urlFetched[url] = true;
rc-web@69 824 context.load(this.map.id, url);
rc-web@69 825 }
rc-web@69 826 },
rc-web@69 827
rc-web@69 828 /**
rc-web@69 829 * Checks if the module is ready to define itself, and if so,
rc-web@69 830 * define it.
rc-web@69 831 */
rc-web@69 832 check: function () {
rc-web@69 833 if (!this.enabled || this.enabling) {
rc-web@69 834 return;
rc-web@69 835 }
rc-web@69 836
rc-web@69 837 var err, cjsModule,
rc-web@69 838 id = this.map.id,
rc-web@69 839 depExports = this.depExports,
rc-web@69 840 exports = this.exports,
rc-web@69 841 factory = this.factory;
rc-web@69 842
rc-web@69 843 if (!this.inited) {
rc-web@69 844 this.fetch();
rc-web@69 845 } else if (this.error) {
rc-web@69 846 this.emit('error', this.error);
rc-web@69 847 } else if (!this.defining) {
rc-web@69 848 //The factory could trigger another require call
rc-web@69 849 //that would result in checking this module to
rc-web@69 850 //define itself again. If already in the process
rc-web@69 851 //of doing that, skip this work.
rc-web@69 852 this.defining = true;
rc-web@69 853
rc-web@69 854 if (this.depCount < 1 && !this.defined) {
rc-web@69 855 if (isFunction(factory)) {
rc-web@69 856 //If there is an error listener, favor passing
rc-web@69 857 //to that instead of throwing an error. However,
rc-web@69 858 //only do it for define()'d modules. require
rc-web@69 859 //errbacks should not be called for failures in
rc-web@69 860 //their callbacks (#699). However if a global
rc-web@69 861 //onError is set, use that.
rc-web@69 862 if ((this.events.error && this.map.isDefine) ||
rc-web@69 863 req.onError !== defaultOnError) {
rc-web@69 864 try {
rc-web@69 865 exports = context.execCb(id, factory, depExports, exports);
rc-web@69 866 } catch (e) {
rc-web@69 867 err = e;
rc-web@69 868 }
rc-web@69 869 } else {
rc-web@69 870 exports = context.execCb(id, factory, depExports, exports);
rc-web@69 871 }
rc-web@69 872
rc-web@69 873 if (this.map.isDefine) {
rc-web@69 874 //If setting exports via 'module' is in play,
rc-web@69 875 //favor that over return value and exports. After that,
rc-web@69 876 //favor a non-undefined return value over exports use.
rc-web@69 877 cjsModule = this.module;
rc-web@69 878 if (cjsModule &&
rc-web@69 879 cjsModule.exports !== undefined &&
rc-web@69 880 //Make sure it is not already the exports value
rc-web@69 881 cjsModule.exports !== this.exports) {
rc-web@69 882 exports = cjsModule.exports;
rc-web@69 883 } else if (exports === undefined && this.usingExports) {
rc-web@69 884 //exports already set the defined value.
rc-web@69 885 exports = this.exports;
rc-web@69 886 }
rc-web@69 887 }
rc-web@69 888
rc-web@69 889 if (err) {
rc-web@69 890 err.requireMap = this.map;
rc-web@69 891 err.requireModules = this.map.isDefine ? [this.map.id] : null;
rc-web@69 892 err.requireType = this.map.isDefine ? 'define' : 'require';
rc-web@69 893 return onError((this.error = err));
rc-web@69 894 }
rc-web@69 895
rc-web@69 896 } else {
rc-web@69 897 //Just a literal value
rc-web@69 898 exports = factory;
rc-web@69 899 }
rc-web@69 900
rc-web@69 901 this.exports = exports;
rc-web@69 902
rc-web@69 903 if (this.map.isDefine && !this.ignore) {
rc-web@69 904 defined[id] = exports;
rc-web@69 905
rc-web@69 906 if (req.onResourceLoad) {
rc-web@69 907 req.onResourceLoad(context, this.map, this.depMaps);
rc-web@69 908 }
rc-web@69 909 }
rc-web@69 910
rc-web@69 911 //Clean up
rc-web@69 912 cleanRegistry(id);
rc-web@69 913
rc-web@69 914 this.defined = true;
rc-web@69 915 }
rc-web@69 916
rc-web@69 917 //Finished the define stage. Allow calling check again
rc-web@69 918 //to allow define notifications below in the case of a
rc-web@69 919 //cycle.
rc-web@69 920 this.defining = false;
rc-web@69 921
rc-web@69 922 if (this.defined && !this.defineEmitted) {
rc-web@69 923 this.defineEmitted = true;
rc-web@69 924 this.emit('defined', this.exports);
rc-web@69 925 this.defineEmitComplete = true;
rc-web@69 926 }
rc-web@69 927
rc-web@69 928 }
rc-web@69 929 },
rc-web@69 930
rc-web@69 931 callPlugin: function () {
rc-web@69 932 var map = this.map,
rc-web@69 933 id = map.id,
rc-web@69 934 //Map already normalized the prefix.
rc-web@69 935 pluginMap = makeModuleMap(map.prefix);
rc-web@69 936
rc-web@69 937 //Mark this as a dependency for this plugin, so it
rc-web@69 938 //can be traced for cycles.
rc-web@69 939 this.depMaps.push(pluginMap);
rc-web@69 940
rc-web@69 941 on(pluginMap, 'defined', bind(this, function (plugin) {
rc-web@69 942 var load, normalizedMap, normalizedMod,
rc-web@69 943 name = this.map.name,
rc-web@69 944 parentName = this.map.parentMap ? this.map.parentMap.name : null,
rc-web@69 945 localRequire = context.makeRequire(map.parentMap, {
rc-web@69 946 enableBuildCallback: true
rc-web@69 947 });
rc-web@69 948
rc-web@69 949 //If current map is not normalized, wait for that
rc-web@69 950 //normalized name to load instead of continuing.
rc-web@69 951 if (this.map.unnormalized) {
rc-web@69 952 //Normalize the ID if the plugin allows it.
rc-web@69 953 if (plugin.normalize) {
rc-web@69 954 name = plugin.normalize(name, function (name) {
rc-web@69 955 return normalize(name, parentName, true);
rc-web@69 956 }) || '';
rc-web@69 957 }
rc-web@69 958
rc-web@69 959 //prefix and name should already be normalized, no need
rc-web@69 960 //for applying map config again either.
rc-web@69 961 normalizedMap = makeModuleMap(map.prefix + '!' + name,
rc-web@69 962 this.map.parentMap);
rc-web@69 963 on(normalizedMap,
rc-web@69 964 'defined', bind(this, function (value) {
rc-web@69 965 this.init([], function () { return value; }, null, {
rc-web@69 966 enabled: true,
rc-web@69 967 ignore: true
rc-web@69 968 });
rc-web@69 969 }));
rc-web@69 970
rc-web@69 971 normalizedMod = getOwn(registry, normalizedMap.id);
rc-web@69 972 if (normalizedMod) {
rc-web@69 973 //Mark this as a dependency for this plugin, so it
rc-web@69 974 //can be traced for cycles.
rc-web@69 975 this.depMaps.push(normalizedMap);
rc-web@69 976
rc-web@69 977 if (this.events.error) {
rc-web@69 978 normalizedMod.on('error', bind(this, function (err) {
rc-web@69 979 this.emit('error', err);
rc-web@69 980 }));
rc-web@69 981 }
rc-web@69 982 normalizedMod.enable();
rc-web@69 983 }
rc-web@69 984
rc-web@69 985 return;
rc-web@69 986 }
rc-web@69 987
rc-web@69 988 load = bind(this, function (value) {
rc-web@69 989 this.init([], function () { return value; }, null, {
rc-web@69 990 enabled: true
rc-web@69 991 });
rc-web@69 992 });
rc-web@69 993
rc-web@69 994 load.error = bind(this, function (err) {
rc-web@69 995 this.inited = true;
rc-web@69 996 this.error = err;
rc-web@69 997 err.requireModules = [id];
rc-web@69 998
rc-web@69 999 //Remove temp unnormalized modules for this module,
rc-web@69 1000 //since they will never be resolved otherwise now.
rc-web@69 1001 eachProp(registry, function (mod) {
rc-web@69 1002 if (mod.map.id.indexOf(id + '_unnormalized') === 0) {
rc-web@69 1003 cleanRegistry(mod.map.id);
rc-web@69 1004 }
rc-web@69 1005 });
rc-web@69 1006
rc-web@69 1007 onError(err);
rc-web@69 1008 });
rc-web@69 1009
rc-web@69 1010 //Allow plugins to load other code without having to know the
rc-web@69 1011 //context or how to 'complete' the load.
rc-web@69 1012 load.fromText = bind(this, function (text, textAlt) {
rc-web@69 1013 /*jslint evil: true */
rc-web@69 1014 var moduleName = map.name,
rc-web@69 1015 moduleMap = makeModuleMap(moduleName),
rc-web@69 1016 hasInteractive = useInteractive;
rc-web@69 1017
rc-web@69 1018 //As of 2.1.0, support just passing the text, to reinforce
rc-web@69 1019 //fromText only being called once per resource. Still
rc-web@69 1020 //support old style of passing moduleName but discard
rc-web@69 1021 //that moduleName in favor of the internal ref.
rc-web@69 1022 if (textAlt) {
rc-web@69 1023 text = textAlt;
rc-web@69 1024 }
rc-web@69 1025
rc-web@69 1026 //Turn off interactive script matching for IE for any define
rc-web@69 1027 //calls in the text, then turn it back on at the end.
rc-web@69 1028 if (hasInteractive) {
rc-web@69 1029 useInteractive = false;
rc-web@69 1030 }
rc-web@69 1031
rc-web@69 1032 //Prime the system by creating a module instance for
rc-web@69 1033 //it.
rc-web@69 1034 getModule(moduleMap);
rc-web@69 1035
rc-web@69 1036 //Transfer any config to this other module.
rc-web@69 1037 if (hasProp(config.config, id)) {
rc-web@69 1038 config.config[moduleName] = config.config[id];
rc-web@69 1039 }
rc-web@69 1040
rc-web@69 1041 try {
rc-web@69 1042 req.exec(text);
rc-web@69 1043 } catch (e) {
rc-web@69 1044 return onError(makeError('fromtexteval',
rc-web@69 1045 'fromText eval for ' + id +
rc-web@69 1046 ' failed: ' + e,
rc-web@69 1047 e,
rc-web@69 1048 [id]));
rc-web@69 1049 }
rc-web@69 1050
rc-web@69 1051 if (hasInteractive) {
rc-web@69 1052 useInteractive = true;
rc-web@69 1053 }
rc-web@69 1054
rc-web@69 1055 //Mark this as a dependency for the plugin
rc-web@69 1056 //resource
rc-web@69 1057 this.depMaps.push(moduleMap);
rc-web@69 1058
rc-web@69 1059 //Support anonymous modules.
rc-web@69 1060 context.completeLoad(moduleName);
rc-web@69 1061
rc-web@69 1062 //Bind the value of that module to the value for this
rc-web@69 1063 //resource ID.
rc-web@69 1064 localRequire([moduleName], load);
rc-web@69 1065 });
rc-web@69 1066
rc-web@69 1067 //Use parentName here since the plugin's name is not reliable,
rc-web@69 1068 //could be some weird string with no path that actually wants to
rc-web@69 1069 //reference the parentName's path.
rc-web@69 1070 plugin.load(map.name, localRequire, load, config);
rc-web@69 1071 }));
rc-web@69 1072
rc-web@69 1073 context.enable(pluginMap, this);
rc-web@69 1074 this.pluginMaps[pluginMap.id] = pluginMap;
rc-web@69 1075 },
rc-web@69 1076
rc-web@69 1077 enable: function () {
rc-web@69 1078 enabledRegistry[this.map.id] = this;
rc-web@69 1079 this.enabled = true;
rc-web@69 1080
rc-web@69 1081 //Set flag mentioning that the module is enabling,
rc-web@69 1082 //so that immediate calls to the defined callbacks
rc-web@69 1083 //for dependencies do not trigger inadvertent load
rc-web@69 1084 //with the depCount still being zero.
rc-web@69 1085 this.enabling = true;
rc-web@69 1086
rc-web@69 1087 //Enable each dependency
rc-web@69 1088 each(this.depMaps, bind(this, function (depMap, i) {
rc-web@69 1089 var id, mod, handler;
rc-web@69 1090
rc-web@69 1091 if (typeof depMap === 'string') {
rc-web@69 1092 //Dependency needs to be converted to a depMap
rc-web@69 1093 //and wired up to this module.
rc-web@69 1094 depMap = makeModuleMap(depMap,
rc-web@69 1095 (this.map.isDefine ? this.map : this.map.parentMap),
rc-web@69 1096 false,
rc-web@69 1097 !this.skipMap);
rc-web@69 1098 this.depMaps[i] = depMap;
rc-web@69 1099
rc-web@69 1100 handler = getOwn(handlers, depMap.id);
rc-web@69 1101
rc-web@69 1102 if (handler) {
rc-web@69 1103 this.depExports[i] = handler(this);
rc-web@69 1104 return;
rc-web@69 1105 }
rc-web@69 1106
rc-web@69 1107 this.depCount += 1;
rc-web@69 1108
rc-web@69 1109 on(depMap, 'defined', bind(this, function (depExports) {
rc-web@69 1110 this.defineDep(i, depExports);
rc-web@69 1111 this.check();
rc-web@69 1112 }));
rc-web@69 1113
rc-web@69 1114 if (this.errback) {
rc-web@69 1115 on(depMap, 'error', bind(this, this.errback));
rc-web@69 1116 }
rc-web@69 1117 }
rc-web@69 1118
rc-web@69 1119 id = depMap.id;
rc-web@69 1120 mod = registry[id];
rc-web@69 1121
rc-web@69 1122 //Skip special modules like 'require', 'exports', 'module'
rc-web@69 1123 //Also, don't call enable if it is already enabled,
rc-web@69 1124 //important in circular dependency cases.
rc-web@69 1125 if (!hasProp(handlers, id) && mod && !mod.enabled) {
rc-web@69 1126 context.enable(depMap, this);
rc-web@69 1127 }
rc-web@69 1128 }));
rc-web@69 1129
rc-web@69 1130 //Enable each plugin that is used in
rc-web@69 1131 //a dependency
rc-web@69 1132 eachProp(this.pluginMaps, bind(this, function (pluginMap) {
rc-web@69 1133 var mod = getOwn(registry, pluginMap.id);
rc-web@69 1134 if (mod && !mod.enabled) {
rc-web@69 1135 context.enable(pluginMap, this);
rc-web@69 1136 }
rc-web@69 1137 }));
rc-web@69 1138
rc-web@69 1139 this.enabling = false;
rc-web@69 1140
rc-web@69 1141 this.check();
rc-web@69 1142 },
rc-web@69 1143
rc-web@69 1144 on: function (name, cb) {
rc-web@69 1145 var cbs = this.events[name];
rc-web@69 1146 if (!cbs) {
rc-web@69 1147 cbs = this.events[name] = [];
rc-web@69 1148 }
rc-web@69 1149 cbs.push(cb);
rc-web@69 1150 },
rc-web@69 1151
rc-web@69 1152 emit: function (name, evt) {
rc-web@69 1153 each(this.events[name], function (cb) {
rc-web@69 1154 cb(evt);
rc-web@69 1155 });
rc-web@69 1156 if (name === 'error') {
rc-web@69 1157 //Now that the error handler was triggered, remove
rc-web@69 1158 //the listeners, since this broken Module instance
rc-web@69 1159 //can stay around for a while in the registry.
rc-web@69 1160 delete this.events[name];
rc-web@69 1161 }
rc-web@69 1162 }
rc-web@69 1163 };
rc-web@69 1164
rc-web@69 1165 function callGetModule(args) {
rc-web@69 1166 //Skip modules already defined.
rc-web@69 1167 if (!hasProp(defined, args[0])) {
rc-web@69 1168 getModule(makeModuleMap(args[0], null, true)).init(args[1], args[2]);
rc-web@69 1169 }
rc-web@69 1170 }
rc-web@69 1171
rc-web@69 1172 function removeListener(node, func, name, ieName) {
rc-web@69 1173 //Favor detachEvent because of IE9
rc-web@69 1174 //issue, see attachEvent/addEventListener comment elsewhere
rc-web@69 1175 //in this file.
rc-web@69 1176 if (node.detachEvent && !isOpera) {
rc-web@69 1177 //Probably IE. If not it will throw an error, which will be
rc-web@69 1178 //useful to know.
rc-web@69 1179 if (ieName) {
rc-web@69 1180 node.detachEvent(ieName, func);
rc-web@69 1181 }
rc-web@69 1182 } else {
rc-web@69 1183 node.removeEventListener(name, func, false);
rc-web@69 1184 }
rc-web@69 1185 }
rc-web@69 1186
rc-web@69 1187 /**
rc-web@69 1188 * Given an event from a script node, get the requirejs info from it,
rc-web@69 1189 * and then removes the event listeners on the node.
rc-web@69 1190 * @param {Event} evt
rc-web@69 1191 * @returns {Object}
rc-web@69 1192 */
rc-web@69 1193 function getScriptData(evt) {
rc-web@69 1194 //Using currentTarget instead of target for Firefox 2.0's sake. Not
rc-web@69 1195 //all old browsers will be supported, but this one was easy enough
rc-web@69 1196 //to support and still makes sense.
rc-web@69 1197 var node = evt.currentTarget || evt.srcElement;
rc-web@69 1198
rc-web@69 1199 //Remove the listeners once here.
rc-web@69 1200 removeListener(node, context.onScriptLoad, 'load', 'onreadystatechange');
rc-web@69 1201 removeListener(node, context.onScriptError, 'error');
rc-web@69 1202
rc-web@69 1203 return {
rc-web@69 1204 node: node,
rc-web@69 1205 id: node && node.getAttribute('data-requiremodule')
rc-web@69 1206 };
rc-web@69 1207 }
rc-web@69 1208
rc-web@69 1209 function intakeDefines() {
rc-web@69 1210 var args;
rc-web@69 1211
rc-web@69 1212 //Any defined modules in the global queue, intake them now.
rc-web@69 1213 takeGlobalQueue();
rc-web@69 1214
rc-web@69 1215 //Make sure any remaining defQueue items get properly processed.
rc-web@69 1216 while (defQueue.length) {
rc-web@69 1217 args = defQueue.shift();
rc-web@69 1218 if (args[0] === null) {
rc-web@69 1219 return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
rc-web@69 1220 } else {
rc-web@69 1221 //args are id, deps, factory. Should be normalized by the
rc-web@69 1222 //define() function.
rc-web@69 1223 callGetModule(args);
rc-web@69 1224 }
rc-web@69 1225 }
rc-web@69 1226 }
rc-web@69 1227
rc-web@69 1228 context = {
rc-web@69 1229 config: config,
rc-web@69 1230 contextName: contextName,
rc-web@69 1231 registry: registry,
rc-web@69 1232 defined: defined,
rc-web@69 1233 urlFetched: urlFetched,
rc-web@69 1234 defQueue: defQueue,
rc-web@69 1235 Module: Module,
rc-web@69 1236 makeModuleMap: makeModuleMap,
rc-web@69 1237 nextTick: req.nextTick,
rc-web@69 1238 onError: onError,
rc-web@69 1239
rc-web@69 1240 /**
rc-web@69 1241 * Set a configuration for the context.
rc-web@69 1242 * @param {Object} cfg config object to integrate.
rc-web@69 1243 */
rc-web@69 1244 configure: function (cfg) {
rc-web@69 1245 //Make sure the baseUrl ends in a slash.
rc-web@69 1246 if (cfg.baseUrl) {
rc-web@69 1247 if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== '/') {
rc-web@69 1248 cfg.baseUrl += '/';
rc-web@69 1249 }
rc-web@69 1250 }
rc-web@69 1251
rc-web@69 1252 //Save off the paths and packages since they require special processing,
rc-web@69 1253 //they are additive.
rc-web@69 1254 var pkgs = config.pkgs,
rc-web@69 1255 shim = config.shim,
rc-web@69 1256 objs = {
rc-web@69 1257 paths: true,
rc-web@69 1258 config: true,
rc-web@69 1259 map: true
rc-web@69 1260 };
rc-web@69 1261
rc-web@69 1262 eachProp(cfg, function (value, prop) {
rc-web@69 1263 if (objs[prop]) {
rc-web@69 1264 if (prop === 'map') {
rc-web@69 1265 if (!config.map) {
rc-web@69 1266 config.map = {};
rc-web@69 1267 }
rc-web@69 1268 mixin(config[prop], value, true, true);
rc-web@69 1269 } else {
rc-web@69 1270 mixin(config[prop], value, true);
rc-web@69 1271 }
rc-web@69 1272 } else {
rc-web@69 1273 config[prop] = value;
rc-web@69 1274 }
rc-web@69 1275 });
rc-web@69 1276
rc-web@69 1277 //Merge shim
rc-web@69 1278 if (cfg.shim) {
rc-web@69 1279 eachProp(cfg.shim, function (value, id) {
rc-web@69 1280 //Normalize the structure
rc-web@69 1281 if (isArray(value)) {
rc-web@69 1282 value = {
rc-web@69 1283 deps: value
rc-web@69 1284 };
rc-web@69 1285 }
rc-web@69 1286 if ((value.exports || value.init) && !value.exportsFn) {
rc-web@69 1287 value.exportsFn = context.makeShimExports(value);
rc-web@69 1288 }
rc-web@69 1289 shim[id] = value;
rc-web@69 1290 });
rc-web@69 1291 config.shim = shim;
rc-web@69 1292 }
rc-web@69 1293
rc-web@69 1294 //Adjust packages if necessary.
rc-web@69 1295 if (cfg.packages) {
rc-web@69 1296 each(cfg.packages, function (pkgObj) {
rc-web@69 1297 var location;
rc-web@69 1298
rc-web@69 1299 pkgObj = typeof pkgObj === 'string' ? { name: pkgObj } : pkgObj;
rc-web@69 1300 location = pkgObj.location;
rc-web@69 1301
rc-web@69 1302 //Create a brand new object on pkgs, since currentPackages can
rc-web@69 1303 //be passed in again, and config.pkgs is the internal transformed
rc-web@69 1304 //state for all package configs.
rc-web@69 1305 pkgs[pkgObj.name] = {
rc-web@69 1306 name: pkgObj.name,
rc-web@69 1307 location: location || pkgObj.name,
rc-web@69 1308 //Remove leading dot in main, so main paths are normalized,
rc-web@69 1309 //and remove any trailing .js, since different package
rc-web@69 1310 //envs have different conventions: some use a module name,
rc-web@69 1311 //some use a file name.
rc-web@69 1312 main: (pkgObj.main || 'main')
rc-web@69 1313 .replace(currDirRegExp, '')
rc-web@69 1314 .replace(jsSuffixRegExp, '')
rc-web@69 1315 };
rc-web@69 1316 });
rc-web@69 1317
rc-web@69 1318 //Done with modifications, assing packages back to context config
rc-web@69 1319 config.pkgs = pkgs;
rc-web@69 1320 }
rc-web@69 1321
rc-web@69 1322 //If there are any "waiting to execute" modules in the registry,
rc-web@69 1323 //update the maps for them, since their info, like URLs to load,
rc-web@69 1324 //may have changed.
rc-web@69 1325 eachProp(registry, function (mod, id) {
rc-web@69 1326 //If module already has init called, since it is too
rc-web@69 1327 //late to modify them, and ignore unnormalized ones
rc-web@69 1328 //since they are transient.
rc-web@69 1329 if (!mod.inited && !mod.map.unnormalized) {
rc-web@69 1330 mod.map = makeModuleMap(id);
rc-web@69 1331 }
rc-web@69 1332 });
rc-web@69 1333
rc-web@69 1334 //If a deps array or a config callback is specified, then call
rc-web@69 1335 //require with those args. This is useful when require is defined as a
rc-web@69 1336 //config object before require.js is loaded.
rc-web@69 1337 if (cfg.deps || cfg.callback) {
rc-web@69 1338 context.require(cfg.deps || [], cfg.callback);
rc-web@69 1339 }
rc-web@69 1340 },
rc-web@69 1341
rc-web@69 1342 makeShimExports: function (value) {
rc-web@69 1343 function fn() {
rc-web@69 1344 var ret;
rc-web@69 1345 if (value.init) {
rc-web@69 1346 ret = value.init.apply(global, arguments);
rc-web@69 1347 }
rc-web@69 1348 return ret || (value.exports && getGlobal(value.exports));
rc-web@69 1349 }
rc-web@69 1350 return fn;
rc-web@69 1351 },
rc-web@69 1352
rc-web@69 1353 makeRequire: function (relMap, options) {
rc-web@69 1354 options = options || {};
rc-web@69 1355
rc-web@69 1356 function localRequire(deps, callback, errback) {
rc-web@69 1357 var id, map, requireMod;
rc-web@69 1358
rc-web@69 1359 if (options.enableBuildCallback && callback && isFunction(callback)) {
rc-web@69 1360 callback.__requireJsBuild = true;
rc-web@69 1361 }
rc-web@69 1362
rc-web@69 1363 if (typeof deps === 'string') {
rc-web@69 1364 if (isFunction(callback)) {
rc-web@69 1365 //Invalid call
rc-web@69 1366 return onError(makeError('requireargs', 'Invalid require call'), errback);
rc-web@69 1367 }
rc-web@69 1368
rc-web@69 1369 //If require|exports|module are requested, get the
rc-web@69 1370 //value for them from the special handlers. Caveat:
rc-web@69 1371 //this only works while module is being defined.
rc-web@69 1372 if (relMap && hasProp(handlers, deps)) {
rc-web@69 1373 return handlers[deps](registry[relMap.id]);
rc-web@69 1374 }
rc-web@69 1375
rc-web@69 1376 //Synchronous access to one module. If require.get is
rc-web@69 1377 //available (as in the Node adapter), prefer that.
rc-web@69 1378 if (req.get) {
rc-web@69 1379 return req.get(context, deps, relMap, localRequire);
rc-web@69 1380 }
rc-web@69 1381
rc-web@69 1382 //Normalize module name, if it contains . or ..
rc-web@69 1383 map = makeModuleMap(deps, relMap, false, true);
rc-web@69 1384 id = map.id;
rc-web@69 1385
rc-web@69 1386 if (!hasProp(defined, id)) {
rc-web@69 1387 return onError(makeError('notloaded', 'Module name "' +
rc-web@69 1388 id +
rc-web@69 1389 '" has not been loaded yet for context: ' +
rc-web@69 1390 contextName +
rc-web@69 1391 (relMap ? '' : '. Use require([])')));
rc-web@69 1392 }
rc-web@69 1393 return defined[id];
rc-web@69 1394 }
rc-web@69 1395
rc-web@69 1396 //Grab defines waiting in the global queue.
rc-web@69 1397 intakeDefines();
rc-web@69 1398
rc-web@69 1399 //Mark all the dependencies as needing to be loaded.
rc-web@69 1400 context.nextTick(function () {
rc-web@69 1401 //Some defines could have been added since the
rc-web@69 1402 //require call, collect them.
rc-web@69 1403 intakeDefines();
rc-web@69 1404
rc-web@69 1405 requireMod = getModule(makeModuleMap(null, relMap));
rc-web@69 1406
rc-web@69 1407 //Store if map config should be applied to this require
rc-web@69 1408 //call for dependencies.
rc-web@69 1409 requireMod.skipMap = options.skipMap;
rc-web@69 1410
rc-web@69 1411 requireMod.init(deps, callback, errback, {
rc-web@69 1412 enabled: true
rc-web@69 1413 });
rc-web@69 1414
rc-web@69 1415 checkLoaded();
rc-web@69 1416 });
rc-web@69 1417
rc-web@69 1418 return localRequire;
rc-web@69 1419 }
rc-web@69 1420
rc-web@69 1421 mixin(localRequire, {
rc-web@69 1422 isBrowser: isBrowser,
rc-web@69 1423
rc-web@69 1424 /**
rc-web@69 1425 * Converts a module name + .extension into an URL path.
rc-web@69 1426 * *Requires* the use of a module name. It does not support using
rc-web@69 1427 * plain URLs like nameToUrl.
rc-web@69 1428 */
rc-web@69 1429 toUrl: function (moduleNamePlusExt) {
rc-web@69 1430 var ext,
rc-web@69 1431 index = moduleNamePlusExt.lastIndexOf('.'),
rc-web@69 1432 segment = moduleNamePlusExt.split('/')[0],
rc-web@69 1433 isRelative = segment === '.' || segment === '..';
rc-web@69 1434
rc-web@69 1435 //Have a file extension alias, and it is not the
rc-web@69 1436 //dots from a relative path.
rc-web@69 1437 if (index !== -1 && (!isRelative || index > 1)) {
rc-web@69 1438 ext = moduleNamePlusExt.substring(index, moduleNamePlusExt.length);
rc-web@69 1439 moduleNamePlusExt = moduleNamePlusExt.substring(0, index);
rc-web@69 1440 }
rc-web@69 1441
rc-web@69 1442 return context.nameToUrl(normalize(moduleNamePlusExt,
rc-web@69 1443 relMap && relMap.id, true), ext, true);
rc-web@69 1444 },
rc-web@69 1445
rc-web@69 1446 defined: function (id) {
rc-web@69 1447 return hasProp(defined, makeModuleMap(id, relMap, false, true).id);
rc-web@69 1448 },
rc-web@69 1449
rc-web@69 1450 specified: function (id) {
rc-web@69 1451 id = makeModuleMap(id, relMap, false, true).id;
rc-web@69 1452 return hasProp(defined, id) || hasProp(registry, id);
rc-web@69 1453 }
rc-web@69 1454 });
rc-web@69 1455
rc-web@69 1456 //Only allow undef on top level require calls
rc-web@69 1457 if (!relMap) {
rc-web@69 1458 localRequire.undef = function (id) {
rc-web@69 1459 //Bind any waiting define() calls to this context,
rc-web@69 1460 //fix for #408
rc-web@69 1461 takeGlobalQueue();
rc-web@69 1462
rc-web@69 1463 var map = makeModuleMap(id, relMap, true),
rc-web@69 1464 mod = getOwn(registry, id);
rc-web@69 1465
rc-web@69 1466 removeScript(id);
rc-web@69 1467
rc-web@69 1468 delete defined[id];
rc-web@69 1469 delete urlFetched[map.url];
rc-web@69 1470 delete undefEvents[id];
rc-web@69 1471
rc-web@69 1472 if (mod) {
rc-web@69 1473 //Hold on to listeners in case the
rc-web@69 1474 //module will be attempted to be reloaded
rc-web@69 1475 //using a different config.
rc-web@69 1476 if (mod.events.defined) {
rc-web@69 1477 undefEvents[id] = mod.events;
rc-web@69 1478 }
rc-web@69 1479
rc-web@69 1480 cleanRegistry(id);
rc-web@69 1481 }
rc-web@69 1482 };
rc-web@69 1483 }
rc-web@69 1484
rc-web@69 1485 return localRequire;
rc-web@69 1486 },
rc-web@69 1487
rc-web@69 1488 /**
rc-web@69 1489 * Called to enable a module if it is still in the registry
rc-web@69 1490 * awaiting enablement. A second arg, parent, the parent module,
rc-web@69 1491 * is passed in for context, when this method is overriden by
rc-web@69 1492 * the optimizer. Not shown here to keep code compact.
rc-web@69 1493 */
rc-web@69 1494 enable: function (depMap) {
rc-web@69 1495 var mod = getOwn(registry, depMap.id);
rc-web@69 1496 if (mod) {
rc-web@69 1497 getModule(depMap).enable();
rc-web@69 1498 }
rc-web@69 1499 },
rc-web@69 1500
rc-web@69 1501 /**
rc-web@69 1502 * Internal method used by environment adapters to complete a load event.
rc-web@69 1503 * A load event could be a script load or just a load pass from a synchronous
rc-web@69 1504 * load call.
rc-web@69 1505 * @param {String} moduleName the name of the module to potentially complete.
rc-web@69 1506 */
rc-web@69 1507 completeLoad: function (moduleName) {
rc-web@69 1508 var found, args, mod,
rc-web@69 1509 shim = getOwn(config.shim, moduleName) || {},
rc-web@69 1510 shExports = shim.exports;
rc-web@69 1511
rc-web@69 1512 takeGlobalQueue();
rc-web@69 1513
rc-web@69 1514 while (defQueue.length) {
rc-web@69 1515 args = defQueue.shift();
rc-web@69 1516 if (args[0] === null) {
rc-web@69 1517 args[0] = moduleName;
rc-web@69 1518 //If already found an anonymous module and bound it
rc-web@69 1519 //to this name, then this is some other anon module
rc-web@69 1520 //waiting for its completeLoad to fire.
rc-web@69 1521 if (found) {
rc-web@69 1522 break;
rc-web@69 1523 }
rc-web@69 1524 found = true;
rc-web@69 1525 } else if (args[0] === moduleName) {
rc-web@69 1526 //Found matching define call for this script!
rc-web@69 1527 found = true;
rc-web@69 1528 }
rc-web@69 1529
rc-web@69 1530 callGetModule(args);
rc-web@69 1531 }
rc-web@69 1532
rc-web@69 1533 //Do this after the cycle of callGetModule in case the result
rc-web@69 1534 //of those calls/init calls changes the registry.
rc-web@69 1535 mod = getOwn(registry, moduleName);
rc-web@69 1536
rc-web@69 1537 if (!found && !hasProp(defined, moduleName) && mod && !mod.inited) {
rc-web@69 1538 if (config.enforceDefine && (!shExports || !getGlobal(shExports))) {
rc-web@69 1539 if (hasPathFallback(moduleName)) {
rc-web@69 1540 return;
rc-web@69 1541 } else {
rc-web@69 1542 return onError(makeError('nodefine',
rc-web@69 1543 'No define call for ' + moduleName,
rc-web@69 1544 null,
rc-web@69 1545 [moduleName]));
rc-web@69 1546 }
rc-web@69 1547 } else {
rc-web@69 1548 //A script that does not call define(), so just simulate
rc-web@69 1549 //the call for it.
rc-web@69 1550 callGetModule([moduleName, (shim.deps || []), shim.exportsFn]);
rc-web@69 1551 }
rc-web@69 1552 }
rc-web@69 1553
rc-web@69 1554 checkLoaded();
rc-web@69 1555 },
rc-web@69 1556
rc-web@69 1557 /**
rc-web@69 1558 * Converts a module name to a file path. Supports cases where
rc-web@69 1559 * moduleName may actually be just an URL.
rc-web@69 1560 * Note that it **does not** call normalize on the moduleName,
rc-web@69 1561 * it is assumed to have already been normalized. This is an
rc-web@69 1562 * internal API, not a public one. Use toUrl for the public API.
rc-web@69 1563 */
rc-web@69 1564 nameToUrl: function (moduleName, ext, skipExt) {
rc-web@69 1565 var paths, pkgs, pkg, pkgPath, syms, i, parentModule, url,
rc-web@69 1566 parentPath;
rc-web@69 1567
rc-web@69 1568 //If a colon is in the URL, it indicates a protocol is used and it is just
rc-web@69 1569 //an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?)
rc-web@69 1570 //or ends with .js, then assume the user meant to use an url and not a module id.
rc-web@69 1571 //The slash is important for protocol-less URLs as well as full paths.
rc-web@69 1572 if (req.jsExtRegExp.test(moduleName)) {
rc-web@69 1573 //Just a plain path, not module name lookup, so just return it.
rc-web@69 1574 //Add extension if it is included. This is a bit wonky, only non-.js things pass
rc-web@69 1575 //an extension, this method probably needs to be reworked.
rc-web@69 1576 url = moduleName + (ext || '');
rc-web@69 1577 } else {
rc-web@69 1578 //A module that needs to be converted to a path.
rc-web@69 1579 paths = config.paths;
rc-web@69 1580 pkgs = config.pkgs;
rc-web@69 1581
rc-web@69 1582 syms = moduleName.split('/');
rc-web@69 1583 //For each module name segment, see if there is a path
rc-web@69 1584 //registered for it. Start with most specific name
rc-web@69 1585 //and work up from it.
rc-web@69 1586 for (i = syms.length; i > 0; i -= 1) {
rc-web@69 1587 parentModule = syms.slice(0, i).join('/');
rc-web@69 1588 pkg = getOwn(pkgs, parentModule);
rc-web@69 1589 parentPath = getOwn(paths, parentModule);
rc-web@69 1590 if (parentPath) {
rc-web@69 1591 //If an array, it means there are a few choices,
rc-web@69 1592 //Choose the one that is desired
rc-web@69 1593 if (isArray(parentPath)) {
rc-web@69 1594 parentPath = parentPath[0];
rc-web@69 1595 }
rc-web@69 1596 syms.splice(0, i, parentPath);
rc-web@69 1597 break;
rc-web@69 1598 } else if (pkg) {
rc-web@69 1599 //If module name is just the package name, then looking
rc-web@69 1600 //for the main module.
rc-web@69 1601 if (moduleName === pkg.name) {
rc-web@69 1602 pkgPath = pkg.location + '/' + pkg.main;
rc-web@69 1603 } else {
rc-web@69 1604 pkgPath = pkg.location;
rc-web@69 1605 }
rc-web@69 1606 syms.splice(0, i, pkgPath);
rc-web@69 1607 break;
rc-web@69 1608 }
rc-web@69 1609 }
rc-web@69 1610
rc-web@69 1611 //Join the path parts together, then figure out if baseUrl is needed.
rc-web@69 1612 url = syms.join('/');
rc-web@69 1613 url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js'));
rc-web@69 1614 url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
rc-web@69 1615 }
rc-web@69 1616
rc-web@69 1617 return config.urlArgs ? url +
rc-web@69 1618 ((url.indexOf('?') === -1 ? '?' : '&') +
rc-web@69 1619 config.urlArgs) : url;
rc-web@69 1620 },
rc-web@69 1621
rc-web@69 1622 //Delegates to req.load. Broken out as a separate function to
rc-web@69 1623 //allow overriding in the optimizer.
rc-web@69 1624 load: function (id, url) {
rc-web@69 1625 req.load(context, id, url);
rc-web@69 1626 },
rc-web@69 1627
rc-web@69 1628 /**
rc-web@69 1629 * Executes a module callback function. Broken out as a separate function
rc-web@69 1630 * solely to allow the build system to sequence the files in the built
rc-web@69 1631 * layer in the right sequence.
rc-web@69 1632 *
rc-web@69 1633 * @private
rc-web@69 1634 */
rc-web@69 1635 execCb: function (name, callback, args, exports) {
rc-web@69 1636 return callback.apply(exports, args);
rc-web@69 1637 },
rc-web@69 1638
rc-web@69 1639 /**
rc-web@69 1640 * callback for script loads, used to check status of loading.
rc-web@69 1641 *
rc-web@69 1642 * @param {Event} evt the event from the browser for the script
rc-web@69 1643 * that was loaded.
rc-web@69 1644 */
rc-web@69 1645 onScriptLoad: function (evt) {
rc-web@69 1646 //Using currentTarget instead of target for Firefox 2.0's sake. Not
rc-web@69 1647 //all old browsers will be supported, but this one was easy enough
rc-web@69 1648 //to support and still makes sense.
rc-web@69 1649 if (evt.type === 'load' ||
rc-web@69 1650 (readyRegExp.test((evt.currentTarget || evt.srcElement).readyState))) {
rc-web@69 1651 //Reset interactive script so a script node is not held onto for
rc-web@69 1652 //to long.
rc-web@69 1653 interactiveScript = null;
rc-web@69 1654
rc-web@69 1655 //Pull out the name of the module and the context.
rc-web@69 1656 var data = getScriptData(evt);
rc-web@69 1657 context.completeLoad(data.id);
rc-web@69 1658 }
rc-web@69 1659 },
rc-web@69 1660
rc-web@69 1661 /**
rc-web@69 1662 * Callback for script errors.
rc-web@69 1663 */
rc-web@69 1664 onScriptError: function (evt) {
rc-web@69 1665 var data = getScriptData(evt);
rc-web@69 1666 if (!hasPathFallback(data.id)) {
rc-web@69 1667 return onError(makeError('scripterror', 'Script error for: ' + data.id, evt, [data.id]));
rc-web@69 1668 }
rc-web@69 1669 }
rc-web@69 1670 };
rc-web@69 1671
rc-web@69 1672 context.require = context.makeRequire();
rc-web@69 1673 return context;
rc-web@69 1674 }
rc-web@69 1675
rc-web@69 1676 /**
rc-web@69 1677 * Main entry point.
rc-web@69 1678 *
rc-web@69 1679 * If the only argument to require is a string, then the module that
rc-web@69 1680 * is represented by that string is fetched for the appropriate context.
rc-web@69 1681 *
rc-web@69 1682 * If the first argument is an array, then it will be treated as an array
rc-web@69 1683 * of dependency string names to fetch. An optional function callback can
rc-web@69 1684 * be specified to execute when all of those dependencies are available.
rc-web@69 1685 *
rc-web@69 1686 * Make a local req variable to help Caja compliance (it assumes things
rc-web@69 1687 * on a require that are not standardized), and to give a short
rc-web@69 1688 * name for minification/local scope use.
rc-web@69 1689 */
rc-web@69 1690 req = requirejs = function (deps, callback, errback, optional) {
rc-web@69 1691
rc-web@69 1692 //Find the right context, use default
rc-web@69 1693 var context, config,
rc-web@69 1694 contextName = defContextName;
rc-web@69 1695
rc-web@69 1696 // Determine if have config object in the call.
rc-web@69 1697 if (!isArray(deps) && typeof deps !== 'string') {
rc-web@69 1698 // deps is a config object
rc-web@69 1699 config = deps;
rc-web@69 1700 if (isArray(callback)) {
rc-web@69 1701 // Adjust args if there are dependencies
rc-web@69 1702 deps = callback;
rc-web@69 1703 callback = errback;
rc-web@69 1704 errback = optional;
rc-web@69 1705 } else {
rc-web@69 1706 deps = [];
rc-web@69 1707 }
rc-web@69 1708 }
rc-web@69 1709
rc-web@69 1710 if (config && config.context) {
rc-web@69 1711 contextName = config.context;
rc-web@69 1712 }
rc-web@69 1713
rc-web@69 1714 context = getOwn(contexts, contextName);
rc-web@69 1715 if (!context) {
rc-web@69 1716 context = contexts[contextName] = req.s.newContext(contextName);
rc-web@69 1717 }
rc-web@69 1718
rc-web@69 1719 if (config) {
rc-web@69 1720 context.configure(config);
rc-web@69 1721 }
rc-web@69 1722
rc-web@69 1723 return context.require(deps, callback, errback);
rc-web@69 1724 };
rc-web@69 1725
rc-web@69 1726 /**
rc-web@69 1727 * Support require.config() to make it easier to cooperate with other
rc-web@69 1728 * AMD loaders on globally agreed names.
rc-web@69 1729 */
rc-web@69 1730 req.config = function (config) {
rc-web@69 1731 return req(config);
rc-web@69 1732 };
rc-web@69 1733
rc-web@69 1734 /**
rc-web@69 1735 * Execute something after the current tick
rc-web@69 1736 * of the event loop. Override for other envs
rc-web@69 1737 * that have a better solution than setTimeout.
rc-web@69 1738 * @param {Function} fn function to execute later.
rc-web@69 1739 */
rc-web@69 1740 req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) {
rc-web@69 1741 setTimeout(fn, 4);
rc-web@69 1742 } : function (fn) { fn(); };
rc-web@69 1743
rc-web@69 1744 /**
rc-web@69 1745 * Export require as a global, but only if it does not already exist.
rc-web@69 1746 */
rc-web@69 1747 if (!require) {
rc-web@69 1748 require = req;
rc-web@69 1749 }
rc-web@69 1750
rc-web@69 1751 req.version = version;
rc-web@69 1752
rc-web@69 1753 //Used to filter out dependencies that are already paths.
rc-web@69 1754 req.jsExtRegExp = /^\/|:|\?|\.js$/;
rc-web@69 1755 req.isBrowser = isBrowser;
rc-web@69 1756 s = req.s = {
rc-web@69 1757 contexts: contexts,
rc-web@69 1758 newContext: newContext
rc-web@69 1759 };
rc-web@69 1760
rc-web@69 1761 //Create default context.
rc-web@69 1762 req({});
rc-web@69 1763
rc-web@69 1764 //Exports some context-sensitive methods on global require.
rc-web@69 1765 each([
rc-web@69 1766 'toUrl',
rc-web@69 1767 'undef',
rc-web@69 1768 'defined',
rc-web@69 1769 'specified'
rc-web@69 1770 ], function (prop) {
rc-web@69 1771 //Reference from contexts instead of early binding to default context,
rc-web@69 1772 //so that during builds, the latest instance of the default context
rc-web@69 1773 //with its config gets used.
rc-web@69 1774 req[prop] = function () {
rc-web@69 1775 var ctx = contexts[defContextName];
rc-web@69 1776 return ctx.require[prop].apply(ctx, arguments);
rc-web@69 1777 };
rc-web@69 1778 });
rc-web@69 1779
rc-web@69 1780 if (isBrowser) {
rc-web@69 1781 head = s.head = document.getElementsByTagName('head')[0];
rc-web@69 1782 //If BASE tag is in play, using appendChild is a problem for IE6.
rc-web@69 1783 //When that browser dies, this can be removed. Details in this jQuery bug:
rc-web@69 1784 //http://dev.jquery.com/ticket/2709
rc-web@69 1785 baseElement = document.getElementsByTagName('base')[0];
rc-web@69 1786 if (baseElement) {
rc-web@69 1787 head = s.head = baseElement.parentNode;
rc-web@69 1788 }
rc-web@69 1789 }
rc-web@69 1790
rc-web@69 1791 /**
rc-web@69 1792 * Any errors that require explicitly generates will be passed to this
rc-web@69 1793 * function. Intercept/override it if you want custom error handling.
rc-web@69 1794 * @param {Error} err the error object.
rc-web@69 1795 */
rc-web@69 1796 req.onError = defaultOnError;
rc-web@69 1797
rc-web@69 1798 /**
rc-web@69 1799 * Creates the node for the load command. Only used in browser envs.
rc-web@69 1800 */
rc-web@69 1801 req.createNode = function (config, moduleName, url) {
rc-web@69 1802 var node = config.xhtml ?
rc-web@69 1803 document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') :
rc-web@69 1804 document.createElement('script');
rc-web@69 1805 node.type = config.scriptType || 'text/javascript';
rc-web@69 1806 node.charset = 'utf-8';
rc-web@69 1807 node.async = true;
rc-web@69 1808 return node;
rc-web@69 1809 };
rc-web@69 1810
rc-web@69 1811 /**
rc-web@69 1812 * Does the request to load a module for the browser case.
rc-web@69 1813 * Make this a separate function to allow other environments
rc-web@69 1814 * to override it.
rc-web@69 1815 *
rc-web@69 1816 * @param {Object} context the require context to find state.
rc-web@69 1817 * @param {String} moduleName the name of the module.
rc-web@69 1818 * @param {Object} url the URL to the module.
rc-web@69 1819 */
rc-web@69 1820 req.load = function (context, moduleName, url) {
rc-web@69 1821 var config = (context && context.config) || {},
rc-web@69 1822 node;
rc-web@69 1823 if (isBrowser) {
rc-web@69 1824 //In the browser so use a script tag
rc-web@69 1825 node = req.createNode(config, moduleName, url);
rc-web@69 1826
rc-web@69 1827 node.setAttribute('data-requirecontext', context.contextName);
rc-web@69 1828 node.setAttribute('data-requiremodule', moduleName);
rc-web@69 1829
rc-web@69 1830 //Set up load listener. Test attachEvent first because IE9 has
rc-web@69 1831 //a subtle issue in its addEventListener and script onload firings
rc-web@69 1832 //that do not match the behavior of all other browsers with
rc-web@69 1833 //addEventListener support, which fire the onload event for a
rc-web@69 1834 //script right after the script execution. See:
rc-web@69 1835 //https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution
rc-web@69 1836 //UNFORTUNATELY Opera implements attachEvent but does not follow the script
rc-web@69 1837 //script execution mode.
rc-web@69 1838 if (node.attachEvent &&
rc-web@69 1839 //Check if node.attachEvent is artificially added by custom script or
rc-web@69 1840 //natively supported by browser
rc-web@69 1841 //read https://github.com/jrburke/requirejs/issues/187
rc-web@69 1842 //if we can NOT find [native code] then it must NOT natively supported.
rc-web@69 1843 //in IE8, node.attachEvent does not have toString()
rc-web@69 1844 //Note the test for "[native code" with no closing brace, see:
rc-web@69 1845 //https://github.com/jrburke/requirejs/issues/273
rc-web@69 1846 !(node.attachEvent.toString && node.attachEvent.toString().indexOf('[native code') < 0) &&
rc-web@69 1847 !isOpera) {
rc-web@69 1848 //Probably IE. IE (at least 6-8) do not fire
rc-web@69 1849 //script onload right after executing the script, so
rc-web@69 1850 //we cannot tie the anonymous define call to a name.
rc-web@69 1851 //However, IE reports the script as being in 'interactive'
rc-web@69 1852 //readyState at the time of the define call.
rc-web@69 1853 useInteractive = true;
rc-web@69 1854
rc-web@69 1855 node.attachEvent('onreadystatechange', context.onScriptLoad);
rc-web@69 1856 //It would be great to add an error handler here to catch
rc-web@69 1857 //404s in IE9+. However, onreadystatechange will fire before
rc-web@69 1858 //the error handler, so that does not help. If addEventListener
rc-web@69 1859 //is used, then IE will fire error before load, but we cannot
rc-web@69 1860 //use that pathway given the connect.microsoft.com issue
rc-web@69 1861 //mentioned above about not doing the 'script execute,
rc-web@69 1862 //then fire the script load event listener before execute
rc-web@69 1863 //next script' that other browsers do.
rc-web@69 1864 //Best hope: IE10 fixes the issues,
rc-web@69 1865 //and then destroys all installs of IE 6-9.
rc-web@69 1866 //node.attachEvent('onerror', context.onScriptError);
rc-web@69 1867 } else {
rc-web@69 1868 node.addEventListener('load', context.onScriptLoad, false);
rc-web@69 1869 node.addEventListener('error', context.onScriptError, false);
rc-web@69 1870 }
rc-web@69 1871 node.src = url;
rc-web@69 1872
rc-web@69 1873 //For some cache cases in IE 6-8, the script executes before the end
rc-web@69 1874 //of the appendChild execution, so to tie an anonymous define
rc-web@69 1875 //call to the module name (which is stored on the node), hold on
rc-web@69 1876 //to a reference to this node, but clear after the DOM insertion.
rc-web@69 1877 currentlyAddingScript = node;
rc-web@69 1878 if (baseElement) {
rc-web@69 1879 head.insertBefore(node, baseElement);
rc-web@69 1880 } else {
rc-web@69 1881 head.appendChild(node);
rc-web@69 1882 }
rc-web@69 1883 currentlyAddingScript = null;
rc-web@69 1884
rc-web@69 1885 return node;
rc-web@69 1886 } else if (isWebWorker) {
rc-web@69 1887 try {
rc-web@69 1888 //In a web worker, use importScripts. This is not a very
rc-web@69 1889 //efficient use of importScripts, importScripts will block until
rc-web@69 1890 //its script is downloaded and evaluated. However, if web workers
rc-web@69 1891 //are in play, the expectation that a build has been done so that
rc-web@69 1892 //only one script needs to be loaded anyway. This may need to be
rc-web@69 1893 //reevaluated if other use cases become common.
rc-web@69 1894 importScripts(url);
rc-web@69 1895
rc-web@69 1896 //Account for anonymous modules
rc-web@69 1897 context.completeLoad(moduleName);
rc-web@69 1898 } catch (e) {
rc-web@69 1899 context.onError(makeError('importscripts',
rc-web@69 1900 'importScripts failed for ' +
rc-web@69 1901 moduleName + ' at ' + url,
rc-web@69 1902 e,
rc-web@69 1903 [moduleName]));
rc-web@69 1904 }
rc-web@69 1905 }
rc-web@69 1906 };
rc-web@69 1907
rc-web@69 1908 function getInteractiveScript() {
rc-web@69 1909 if (interactiveScript && interactiveScript.readyState === 'interactive') {
rc-web@69 1910 return interactiveScript;
rc-web@69 1911 }
rc-web@69 1912
rc-web@69 1913 eachReverse(scripts(), function (script) {
rc-web@69 1914 if (script.readyState === 'interactive') {
rc-web@69 1915 return (interactiveScript = script);
rc-web@69 1916 }
rc-web@69 1917 });
rc-web@69 1918 return interactiveScript;
rc-web@69 1919 }
rc-web@69 1920
rc-web@69 1921 //Look for a data-main script attribute, which could also adjust the baseUrl.
rc-web@69 1922 if (isBrowser && !cfg.skipDataMain) {
rc-web@69 1923 //Figure out baseUrl. Get it from the script tag with require.js in it.
rc-web@69 1924 eachReverse(scripts(), function (script) {
rc-web@69 1925 //Set the 'head' where we can append children by
rc-web@69 1926 //using the script's parent.
rc-web@69 1927 if (!head) {
rc-web@69 1928 head = script.parentNode;
rc-web@69 1929 }
rc-web@69 1930
rc-web@69 1931 //Look for a data-main attribute to set main script for the page
rc-web@69 1932 //to load. If it is there, the path to data main becomes the
rc-web@69 1933 //baseUrl, if it is not already set.
rc-web@69 1934 dataMain = script.getAttribute('data-main');
rc-web@69 1935 if (dataMain) {
rc-web@69 1936 //Preserve dataMain in case it is a path (i.e. contains '?')
rc-web@69 1937 mainScript = dataMain;
rc-web@69 1938
rc-web@69 1939 //Set final baseUrl if there is not already an explicit one.
rc-web@69 1940 if (!cfg.baseUrl) {
rc-web@69 1941 //Pull off the directory of data-main for use as the
rc-web@69 1942 //baseUrl.
rc-web@69 1943 src = mainScript.split('/');
rc-web@69 1944 mainScript = src.pop();
rc-web@69 1945 subPath = src.length ? src.join('/') + '/' : './';
rc-web@69 1946
rc-web@69 1947 cfg.baseUrl = subPath;
rc-web@69 1948 }
rc-web@69 1949
rc-web@69 1950 //Strip off any trailing .js since mainScript is now
rc-web@69 1951 //like a module name.
rc-web@69 1952 mainScript = mainScript.replace(jsSuffixRegExp, '');
rc-web@69 1953
rc-web@69 1954 //If mainScript is still a path, fall back to dataMain
rc-web@69 1955 if (req.jsExtRegExp.test(mainScript)) {
rc-web@69 1956 mainScript = dataMain;
rc-web@69 1957 }
rc-web@69 1958
rc-web@69 1959 //Put the data-main script in the files to load.
rc-web@69 1960 cfg.deps = cfg.deps ? cfg.deps.concat(mainScript) : [mainScript];
rc-web@69 1961
rc-web@69 1962 return true;
rc-web@69 1963 }
rc-web@69 1964 });
rc-web@69 1965 }
rc-web@69 1966
rc-web@69 1967 /**
rc-web@69 1968 * The function that handles definitions of modules. Differs from
rc-web@69 1969 * require() in that a string for the module should be the first argument,
rc-web@69 1970 * and the function to execute after dependencies are loaded should
rc-web@69 1971 * return a value to define the module corresponding to the first argument's
rc-web@69 1972 * name.
rc-web@69 1973 */
rc-web@69 1974 define = function (name, deps, callback) {
rc-web@69 1975 var node, context;
rc-web@69 1976
rc-web@69 1977 //Allow for anonymous modules
rc-web@69 1978 if (typeof name !== 'string') {
rc-web@69 1979 //Adjust args appropriately
rc-web@69 1980 callback = deps;
rc-web@69 1981 deps = name;
rc-web@69 1982 name = null;
rc-web@69 1983 }
rc-web@69 1984
rc-web@69 1985 //This module may not have dependencies
rc-web@69 1986 if (!isArray(deps)) {
rc-web@69 1987 callback = deps;
rc-web@69 1988 deps = null;
rc-web@69 1989 }
rc-web@69 1990
rc-web@69 1991 //If no name, and callback is a function, then figure out if it a
rc-web@69 1992 //CommonJS thing with dependencies.
rc-web@69 1993 if (!deps && isFunction(callback)) {
rc-web@69 1994 deps = [];
rc-web@69 1995 //Remove comments from the callback string,
rc-web@69 1996 //look for require calls, and pull them into the dependencies,
rc-web@69 1997 //but only if there are function args.
rc-web@69 1998 if (callback.length) {
rc-web@69 1999 callback
rc-web@69 2000 .toString()
rc-web@69 2001 .replace(commentRegExp, '')
rc-web@69 2002 .replace(cjsRequireRegExp, function (match, dep) {
rc-web@69 2003 deps.push(dep);
rc-web@69 2004 });
rc-web@69 2005
rc-web@69 2006 //May be a CommonJS thing even without require calls, but still
rc-web@69 2007 //could use exports, and module. Avoid doing exports and module
rc-web@69 2008 //work though if it just needs require.
rc-web@69 2009 //REQUIRES the function to expect the CommonJS variables in the
rc-web@69 2010 //order listed below.
rc-web@69 2011 deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps);
rc-web@69 2012 }
rc-web@69 2013 }
rc-web@69 2014
rc-web@69 2015 //If in IE 6-8 and hit an anonymous define() call, do the interactive
rc-web@69 2016 //work.
rc-web@69 2017 if (useInteractive) {
rc-web@69 2018 node = currentlyAddingScript || getInteractiveScript();
rc-web@69 2019 if (node) {
rc-web@69 2020 if (!name) {
rc-web@69 2021 name = node.getAttribute('data-requiremodule');
rc-web@69 2022 }
rc-web@69 2023 context = contexts[node.getAttribute('data-requirecontext')];
rc-web@69 2024 }
rc-web@69 2025 }
rc-web@69 2026
rc-web@69 2027 //Always save off evaluating the def call until the script onload handler.
rc-web@69 2028 //This allows multiple modules to be in a file without prematurely
rc-web@69 2029 //tracing dependencies, and allows for anonymous module support,
rc-web@69 2030 //where the module name is not known until the script onload event
rc-web@69 2031 //occurs. If no context, use the global queue, and get it processed
rc-web@69 2032 //in the onscript load callback.
rc-web@69 2033 (context ? context.defQueue : globalDefQueue).push([name, deps, callback]);
rc-web@69 2034 };
rc-web@69 2035
rc-web@69 2036 define.amd = {
rc-web@69 2037 jQuery: true
rc-web@69 2038 };
rc-web@69 2039
rc-web@69 2040
rc-web@69 2041 /**
rc-web@69 2042 * Executes the text. Normally just uses eval, but can be modified
rc-web@69 2043 * to use a better, environment-specific call. Only used for transpiling
rc-web@69 2044 * loader plugins, not for plain JS modules.
rc-web@69 2045 * @param {String} text the text to execute/evaluate.
rc-web@69 2046 */
rc-web@69 2047 req.exec = function (text) {
rc-web@69 2048 /*jslint evil: true */
rc-web@69 2049 return eval(text);
rc-web@69 2050 };
rc-web@69 2051
rc-web@69 2052 //Set up with config info.
rc-web@69 2053 req(cfg);
rc-web@69 2054 }(this));