rc-web@69: /** vim: et:ts=4:sw=4:sts=4 rob@76: * @license RequireJS 2.1.14 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved. rc-web@69: * Available via the MIT or new BSD license. rc-web@69: * see: http://github.com/jrburke/requirejs for details rc-web@69: */ rc-web@69: //Not using strict: uneven strict support in browsers, #392, and causes rc-web@69: //problems with requirejs.exec()/transpiler plugins that may not be strict. rc-web@69: /*jslint regexp: true, nomen: true, sloppy: true */ rc-web@69: /*global window, navigator, document, importScripts, setTimeout, opera */ rc-web@69: rc-web@69: var requirejs, require, define; rc-web@69: (function (global) { rc-web@69: var req, s, head, baseElement, dataMain, src, rc-web@69: interactiveScript, currentlyAddingScript, mainScript, subPath, rob@76: version = '2.1.14', rc-web@69: commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, rc-web@69: cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g, rc-web@69: jsSuffixRegExp = /\.js$/, rc-web@69: currDirRegExp = /^\.\//, rc-web@69: op = Object.prototype, rc-web@69: ostring = op.toString, rc-web@69: hasOwn = op.hasOwnProperty, rc-web@69: ap = Array.prototype, rc-web@69: apsp = ap.splice, rc-web@69: isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document), rc-web@69: isWebWorker = !isBrowser && typeof importScripts !== 'undefined', rc-web@69: //PS3 indicates loaded and complete, but need to wait for complete rc-web@69: //specifically. Sequence is 'loading', 'loaded', execution, rc-web@69: // then 'complete'. The UA check is unfortunate, but not sure how rc-web@69: //to feature test w/o causing perf issues. rc-web@69: readyRegExp = isBrowser && navigator.platform === 'PLAYSTATION 3' ? rc-web@69: /^complete$/ : /^(complete|loaded)$/, rc-web@69: defContextName = '_', rc-web@69: //Oh the tragedy, detecting opera. See the usage of isOpera for reason. rc-web@69: isOpera = typeof opera !== 'undefined' && opera.toString() === '[object Opera]', rc-web@69: contexts = {}, rc-web@69: cfg = {}, rc-web@69: globalDefQueue = [], rc-web@69: useInteractive = false; rc-web@69: rc-web@69: function isFunction(it) { rc-web@69: return ostring.call(it) === '[object Function]'; rc-web@69: } rc-web@69: rc-web@69: function isArray(it) { rc-web@69: return ostring.call(it) === '[object Array]'; rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Helper function for iterating over an array. If the func returns rc-web@69: * a true value, it will break out of the loop. rc-web@69: */ rc-web@69: function each(ary, func) { rc-web@69: if (ary) { rc-web@69: var i; rc-web@69: for (i = 0; i < ary.length; i += 1) { rc-web@69: if (ary[i] && func(ary[i], i, ary)) { rc-web@69: break; rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Helper function for iterating over an array backwards. If the func rc-web@69: * returns a true value, it will break out of the loop. rc-web@69: */ rc-web@69: function eachReverse(ary, func) { rc-web@69: if (ary) { rc-web@69: var i; rc-web@69: for (i = ary.length - 1; i > -1; i -= 1) { rc-web@69: if (ary[i] && func(ary[i], i, ary)) { rc-web@69: break; rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: function hasProp(obj, prop) { rc-web@69: return hasOwn.call(obj, prop); rc-web@69: } rc-web@69: rc-web@69: function getOwn(obj, prop) { rc-web@69: return hasProp(obj, prop) && obj[prop]; rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Cycles over properties in an object and calls a function for each rc-web@69: * property value. If the function returns a truthy value, then the rc-web@69: * iteration is stopped. rc-web@69: */ rc-web@69: function eachProp(obj, func) { rc-web@69: var prop; rc-web@69: for (prop in obj) { rc-web@69: if (hasProp(obj, prop)) { rc-web@69: if (func(obj[prop], prop)) { rc-web@69: break; rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Simple function to mix in properties from source into target, rc-web@69: * but only if target does not already have a property of the same name. rc-web@69: */ rc-web@69: function mixin(target, source, force, deepStringMixin) { rc-web@69: if (source) { rc-web@69: eachProp(source, function (value, prop) { rc-web@69: if (force || !hasProp(target, prop)) { rob@76: if (deepStringMixin && typeof value === 'object' && value && rob@76: !isArray(value) && !isFunction(value) && rob@76: !(value instanceof RegExp)) { rob@76: rc-web@69: if (!target[prop]) { rc-web@69: target[prop] = {}; rc-web@69: } rc-web@69: mixin(target[prop], value, force, deepStringMixin); rc-web@69: } else { rc-web@69: target[prop] = value; rc-web@69: } rc-web@69: } rc-web@69: }); rc-web@69: } rc-web@69: return target; rc-web@69: } rc-web@69: rc-web@69: //Similar to Function.prototype.bind, but the 'this' object is specified rc-web@69: //first, since it is easier to read/figure out what 'this' will be. rc-web@69: function bind(obj, fn) { rc-web@69: return function () { rc-web@69: return fn.apply(obj, arguments); rc-web@69: }; rc-web@69: } rc-web@69: rc-web@69: function scripts() { rc-web@69: return document.getElementsByTagName('script'); rc-web@69: } rc-web@69: rc-web@69: function defaultOnError(err) { rc-web@69: throw err; rc-web@69: } rc-web@69: rob@76: //Allow getting a global that is expressed in rc-web@69: //dot notation, like 'a.b.c'. rc-web@69: function getGlobal(value) { rc-web@69: if (!value) { rc-web@69: return value; rc-web@69: } rc-web@69: var g = global; rc-web@69: each(value.split('.'), function (part) { rc-web@69: g = g[part]; rc-web@69: }); rc-web@69: return g; rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Constructs an error with a pointer to an URL with more information. rc-web@69: * @param {String} id the error ID that maps to an ID on a web page. rc-web@69: * @param {String} message human readable error. rc-web@69: * @param {Error} [err] the original error, if there is one. rc-web@69: * rc-web@69: * @returns {Error} rc-web@69: */ rc-web@69: function makeError(id, msg, err, requireModules) { rc-web@69: var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#' + id); rc-web@69: e.requireType = id; rc-web@69: e.requireModules = requireModules; rc-web@69: if (err) { rc-web@69: e.originalError = err; rc-web@69: } rc-web@69: return e; rc-web@69: } rc-web@69: rc-web@69: if (typeof define !== 'undefined') { rc-web@69: //If a define is already in play via another AMD loader, rc-web@69: //do not overwrite. rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: if (typeof requirejs !== 'undefined') { rc-web@69: if (isFunction(requirejs)) { rob@76: //Do not overwrite an existing requirejs instance. rc-web@69: return; rc-web@69: } rc-web@69: cfg = requirejs; rc-web@69: requirejs = undefined; rc-web@69: } rc-web@69: rc-web@69: //Allow for a require config object rc-web@69: if (typeof require !== 'undefined' && !isFunction(require)) { rc-web@69: //assume it is a config object. rc-web@69: cfg = require; rc-web@69: require = undefined; rc-web@69: } rc-web@69: rc-web@69: function newContext(contextName) { rc-web@69: var inCheckLoaded, Module, context, handlers, rc-web@69: checkLoadedTimeoutId, rc-web@69: config = { rc-web@69: //Defaults. Do not set a default for map rc-web@69: //config to speed up normalize(), which rc-web@69: //will run faster if there is no default. rc-web@69: waitSeconds: 7, rc-web@69: baseUrl: './', rc-web@69: paths: {}, rob@76: bundles: {}, rc-web@69: pkgs: {}, rc-web@69: shim: {}, rc-web@69: config: {} rc-web@69: }, rc-web@69: registry = {}, rc-web@69: //registry of just enabled modules, to speed rc-web@69: //cycle breaking code when lots of modules rc-web@69: //are registered, but not activated. rc-web@69: enabledRegistry = {}, rc-web@69: undefEvents = {}, rc-web@69: defQueue = [], rc-web@69: defined = {}, rc-web@69: urlFetched = {}, rob@76: bundlesMap = {}, rc-web@69: requireCounter = 1, rc-web@69: unnormalizedCounter = 1; rc-web@69: rc-web@69: /** rc-web@69: * Trims the . and .. from an array of path segments. rc-web@69: * It will keep a leading path segment if a .. will become rc-web@69: * the first path segment, to help with module name lookups, rc-web@69: * which act like paths, but can be remapped. But the end result, rc-web@69: * all paths that use this function should look normalized. rc-web@69: * NOTE: this method MODIFIES the input array. rc-web@69: * @param {Array} ary the array of path segments. rc-web@69: */ rc-web@69: function trimDots(ary) { rc-web@69: var i, part; rob@76: for (i = 0; i < ary.length; i++) { rc-web@69: part = ary[i]; rc-web@69: if (part === '.') { rc-web@69: ary.splice(i, 1); rc-web@69: i -= 1; rc-web@69: } else if (part === '..') { rob@76: // If at the start, or previous value is still .., rob@76: // keep them so that when converted to a path it may rob@76: // still work when converted to a path, even though rob@76: // as an ID it is less than ideal. In larger point rob@76: // releases, may be better to just kick out an error. rob@76: if (i === 0 || (i == 1 && ary[2] === '..') || ary[i - 1] === '..') { rob@76: continue; rc-web@69: } else if (i > 0) { rc-web@69: ary.splice(i - 1, 2); rc-web@69: i -= 2; rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Given a relative module name, like ./something, normalize it to rc-web@69: * a real name that can be mapped to a path. rc-web@69: * @param {String} name the relative name rc-web@69: * @param {String} baseName a real name that the name arg is relative rc-web@69: * to. rc-web@69: * @param {Boolean} applyMap apply the map config to the value. Should rc-web@69: * only be done if this normalization is for a dependency ID. rc-web@69: * @returns {String} normalized name rc-web@69: */ rc-web@69: function normalize(name, baseName, applyMap) { rob@76: var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex, rob@76: foundMap, foundI, foundStarMap, starI, normalizedBaseParts, rob@76: baseParts = (baseName && baseName.split('/')), rc-web@69: map = config.map, rc-web@69: starMap = map && map['*']; rc-web@69: rc-web@69: //Adjust any relative paths. rob@76: if (name) { rob@76: name = name.split('/'); rob@76: lastIndex = name.length - 1; rc-web@69: rob@76: // If wanting node ID compatibility, strip .js from end rob@76: // of IDs. Have to do this here, and not in nameToUrl rob@76: // because node allows either .js or non .js to map rob@76: // to same file. rob@76: if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) { rob@76: name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, ''); rob@76: } rc-web@69: rob@76: // Starts with a '.' so need the baseName rob@76: if (name[0].charAt(0) === '.' && baseParts) { rob@76: //Convert baseName to array, and lop off the last part, rob@76: //so that . matches that 'directory' and not name of the baseName's rob@76: //module. For instance, baseName of 'one/two/three', maps to rob@76: //'one/two/three.js', but we want the directory, 'one/two' for rob@76: //this normalization. rob@76: normalizedBaseParts = baseParts.slice(0, baseParts.length - 1); rob@76: name = normalizedBaseParts.concat(name); rc-web@69: } rob@76: rob@76: trimDots(name); rob@76: name = name.join('/'); rc-web@69: } rc-web@69: rc-web@69: //Apply map config if available. rc-web@69: if (applyMap && map && (baseParts || starMap)) { rc-web@69: nameParts = name.split('/'); rc-web@69: rob@76: outerLoop: for (i = nameParts.length; i > 0; i -= 1) { rc-web@69: nameSegment = nameParts.slice(0, i).join('/'); rc-web@69: rc-web@69: if (baseParts) { rc-web@69: //Find the longest baseName segment match in the config. rc-web@69: //So, do joins on the biggest to smallest lengths of baseParts. rc-web@69: for (j = baseParts.length; j > 0; j -= 1) { rc-web@69: mapValue = getOwn(map, baseParts.slice(0, j).join('/')); rc-web@69: rc-web@69: //baseName segment has config, find if it has one for rc-web@69: //this name. rc-web@69: if (mapValue) { rc-web@69: mapValue = getOwn(mapValue, nameSegment); rc-web@69: if (mapValue) { rc-web@69: //Match, update name to the new value. rc-web@69: foundMap = mapValue; rc-web@69: foundI = i; rob@76: break outerLoop; rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //Check for a star map match, but just hold on to it, rc-web@69: //if there is a shorter segment match later in a matching rc-web@69: //config, then favor over this star map. rc-web@69: if (!foundStarMap && starMap && getOwn(starMap, nameSegment)) { rc-web@69: foundStarMap = getOwn(starMap, nameSegment); rc-web@69: starI = i; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: if (!foundMap && foundStarMap) { rc-web@69: foundMap = foundStarMap; rc-web@69: foundI = starI; rc-web@69: } rc-web@69: rc-web@69: if (foundMap) { rc-web@69: nameParts.splice(0, foundI, foundMap); rc-web@69: name = nameParts.join('/'); rc-web@69: } rc-web@69: } rc-web@69: rob@76: // If the name points to a package's name, use rob@76: // the package main instead. rob@76: pkgMain = getOwn(config.pkgs, name); rob@76: rob@76: return pkgMain ? pkgMain : name; rc-web@69: } rc-web@69: rc-web@69: function removeScript(name) { rc-web@69: if (isBrowser) { rc-web@69: each(scripts(), function (scriptNode) { rc-web@69: if (scriptNode.getAttribute('data-requiremodule') === name && rc-web@69: scriptNode.getAttribute('data-requirecontext') === context.contextName) { rc-web@69: scriptNode.parentNode.removeChild(scriptNode); rc-web@69: return true; rc-web@69: } rc-web@69: }); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: function hasPathFallback(id) { rc-web@69: var pathConfig = getOwn(config.paths, id); rc-web@69: if (pathConfig && isArray(pathConfig) && pathConfig.length > 1) { rc-web@69: //Pop off the first array value, since it failed, and rc-web@69: //retry rc-web@69: pathConfig.shift(); rc-web@69: context.require.undef(id); rob@76: rob@76: //Custom require that does not do map translation, since rob@76: //ID is "absolute", already mapped/resolved. rob@76: context.makeRequire(null, { rob@76: skipMap: true rob@76: })([id]); rob@76: rc-web@69: return true; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //Turns a plugin!resource to [plugin, resource] rc-web@69: //with the plugin being undefined if the name rc-web@69: //did not have a plugin prefix. rc-web@69: function splitPrefix(name) { rc-web@69: var prefix, rc-web@69: index = name ? name.indexOf('!') : -1; rc-web@69: if (index > -1) { rc-web@69: prefix = name.substring(0, index); rc-web@69: name = name.substring(index + 1, name.length); rc-web@69: } rc-web@69: return [prefix, name]; rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Creates a module mapping that includes plugin prefix, module rc-web@69: * name, and path. If parentModuleMap is provided it will rc-web@69: * also normalize the name via require.normalize() rc-web@69: * rc-web@69: * @param {String} name the module name rc-web@69: * @param {String} [parentModuleMap] parent module map rc-web@69: * for the module name, used to resolve relative names. rc-web@69: * @param {Boolean} isNormalized: is the ID already normalized. rc-web@69: * This is true if this call is done for a define() module ID. rc-web@69: * @param {Boolean} applyMap: apply the map config to the ID. rc-web@69: * Should only be true if this map is for a dependency. rc-web@69: * rc-web@69: * @returns {Object} rc-web@69: */ rc-web@69: function makeModuleMap(name, parentModuleMap, isNormalized, applyMap) { rc-web@69: var url, pluginModule, suffix, nameParts, rc-web@69: prefix = null, rc-web@69: parentName = parentModuleMap ? parentModuleMap.name : null, rc-web@69: originalName = name, rc-web@69: isDefine = true, rc-web@69: normalizedName = ''; rc-web@69: rc-web@69: //If no name, then it means it is a require call, generate an rc-web@69: //internal name. rc-web@69: if (!name) { rc-web@69: isDefine = false; rc-web@69: name = '_@r' + (requireCounter += 1); rc-web@69: } rc-web@69: rc-web@69: nameParts = splitPrefix(name); rc-web@69: prefix = nameParts[0]; rc-web@69: name = nameParts[1]; rc-web@69: rc-web@69: if (prefix) { rc-web@69: prefix = normalize(prefix, parentName, applyMap); rc-web@69: pluginModule = getOwn(defined, prefix); rc-web@69: } rc-web@69: rc-web@69: //Account for relative paths if there is a base name. rc-web@69: if (name) { rc-web@69: if (prefix) { rc-web@69: if (pluginModule && pluginModule.normalize) { rc-web@69: //Plugin is loaded, use its normalize method. rc-web@69: normalizedName = pluginModule.normalize(name, function (name) { rc-web@69: return normalize(name, parentName, applyMap); rc-web@69: }); rc-web@69: } else { rob@76: // If nested plugin references, then do not try to rob@76: // normalize, as it will not normalize correctly. This rob@76: // places a restriction on resourceIds, and the longer rob@76: // term solution is not to normalize until plugins are rob@76: // loaded and all normalizations to allow for async rob@76: // loading of a loader plugin. But for now, fixes the rob@76: // common uses. Details in #1131 rob@76: normalizedName = name.indexOf('!') === -1 ? rob@76: normalize(name, parentName, applyMap) : rob@76: name; rc-web@69: } rc-web@69: } else { rc-web@69: //A regular module. rc-web@69: normalizedName = normalize(name, parentName, applyMap); rc-web@69: rc-web@69: //Normalized name may be a plugin ID due to map config rc-web@69: //application in normalize. The map config values must rc-web@69: //already be normalized, so do not need to redo that part. rc-web@69: nameParts = splitPrefix(normalizedName); rc-web@69: prefix = nameParts[0]; rc-web@69: normalizedName = nameParts[1]; rc-web@69: isNormalized = true; rc-web@69: rc-web@69: url = context.nameToUrl(normalizedName); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //If the id is a plugin id that cannot be determined if it needs rc-web@69: //normalization, stamp it with a unique ID so two matching relative rc-web@69: //ids that may conflict can be separate. rc-web@69: suffix = prefix && !pluginModule && !isNormalized ? rc-web@69: '_unnormalized' + (unnormalizedCounter += 1) : rc-web@69: ''; rc-web@69: rc-web@69: return { rc-web@69: prefix: prefix, rc-web@69: name: normalizedName, rc-web@69: parentMap: parentModuleMap, rc-web@69: unnormalized: !!suffix, rc-web@69: url: url, rc-web@69: originalName: originalName, rc-web@69: isDefine: isDefine, rc-web@69: id: (prefix ? rc-web@69: prefix + '!' + normalizedName : rc-web@69: normalizedName) + suffix rc-web@69: }; rc-web@69: } rc-web@69: rc-web@69: function getModule(depMap) { rc-web@69: var id = depMap.id, rc-web@69: mod = getOwn(registry, id); rc-web@69: rc-web@69: if (!mod) { rc-web@69: mod = registry[id] = new context.Module(depMap); rc-web@69: } rc-web@69: rc-web@69: return mod; rc-web@69: } rc-web@69: rc-web@69: function on(depMap, name, fn) { rc-web@69: var id = depMap.id, rc-web@69: mod = getOwn(registry, id); rc-web@69: rc-web@69: if (hasProp(defined, id) && rc-web@69: (!mod || mod.defineEmitComplete)) { rc-web@69: if (name === 'defined') { rc-web@69: fn(defined[id]); rc-web@69: } rc-web@69: } else { rc-web@69: mod = getModule(depMap); rc-web@69: if (mod.error && name === 'error') { rc-web@69: fn(mod.error); rc-web@69: } else { rc-web@69: mod.on(name, fn); rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: function onError(err, errback) { rc-web@69: var ids = err.requireModules, rc-web@69: notified = false; rc-web@69: rc-web@69: if (errback) { rc-web@69: errback(err); rc-web@69: } else { rc-web@69: each(ids, function (id) { rc-web@69: var mod = getOwn(registry, id); rc-web@69: if (mod) { rc-web@69: //Set error on module, so it skips timeout checks. rc-web@69: mod.error = err; rc-web@69: if (mod.events.error) { rc-web@69: notified = true; rc-web@69: mod.emit('error', err); rc-web@69: } rc-web@69: } rc-web@69: }); rc-web@69: rc-web@69: if (!notified) { rc-web@69: req.onError(err); rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Internal method to transfer globalQueue items to this context's rc-web@69: * defQueue. rc-web@69: */ rc-web@69: function takeGlobalQueue() { rc-web@69: //Push all the globalDefQueue items into the context's defQueue rc-web@69: if (globalDefQueue.length) { rc-web@69: //Array splice in the values since the context code has a rc-web@69: //local var ref to defQueue, so cannot just reassign the one rc-web@69: //on context. rc-web@69: apsp.apply(defQueue, rob@76: [defQueue.length, 0].concat(globalDefQueue)); rc-web@69: globalDefQueue = []; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: handlers = { rc-web@69: 'require': function (mod) { rc-web@69: if (mod.require) { rc-web@69: return mod.require; rc-web@69: } else { rc-web@69: return (mod.require = context.makeRequire(mod.map)); rc-web@69: } rc-web@69: }, rc-web@69: 'exports': function (mod) { rc-web@69: mod.usingExports = true; rc-web@69: if (mod.map.isDefine) { rc-web@69: if (mod.exports) { rob@76: return (defined[mod.map.id] = mod.exports); rc-web@69: } else { rc-web@69: return (mod.exports = defined[mod.map.id] = {}); rc-web@69: } rc-web@69: } rc-web@69: }, rc-web@69: 'module': function (mod) { rc-web@69: if (mod.module) { rc-web@69: return mod.module; rc-web@69: } else { rc-web@69: return (mod.module = { rc-web@69: id: mod.map.id, rc-web@69: uri: mod.map.url, rc-web@69: config: function () { rob@76: return getOwn(config.config, mod.map.id) || {}; rc-web@69: }, rob@76: exports: mod.exports || (mod.exports = {}) rc-web@69: }); rc-web@69: } rc-web@69: } rc-web@69: }; rc-web@69: rc-web@69: function cleanRegistry(id) { rc-web@69: //Clean up machinery used for waiting modules. rc-web@69: delete registry[id]; rc-web@69: delete enabledRegistry[id]; rc-web@69: } rc-web@69: rc-web@69: function breakCycle(mod, traced, processed) { rc-web@69: var id = mod.map.id; rc-web@69: rc-web@69: if (mod.error) { rc-web@69: mod.emit('error', mod.error); rc-web@69: } else { rc-web@69: traced[id] = true; rc-web@69: each(mod.depMaps, function (depMap, i) { rc-web@69: var depId = depMap.id, rc-web@69: dep = getOwn(registry, depId); rc-web@69: rc-web@69: //Only force things that have not completed rc-web@69: //being defined, so still in the registry, rc-web@69: //and only if it has not been matched up rc-web@69: //in the module already. rc-web@69: if (dep && !mod.depMatched[i] && !processed[depId]) { rc-web@69: if (getOwn(traced, depId)) { rc-web@69: mod.defineDep(i, defined[depId]); rc-web@69: mod.check(); //pass false? rc-web@69: } else { rc-web@69: breakCycle(dep, traced, processed); rc-web@69: } rc-web@69: } rc-web@69: }); rc-web@69: processed[id] = true; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: function checkLoaded() { rob@76: var err, usingPathFallback, rc-web@69: waitInterval = config.waitSeconds * 1000, rc-web@69: //It is possible to disable the wait interval by using waitSeconds of 0. rc-web@69: expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(), rc-web@69: noLoads = [], rc-web@69: reqCalls = [], rc-web@69: stillLoading = false, rc-web@69: needCycleCheck = true; rc-web@69: rc-web@69: //Do not bother if this call was a result of a cycle break. rc-web@69: if (inCheckLoaded) { rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: inCheckLoaded = true; rc-web@69: rc-web@69: //Figure out the state of all the modules. rc-web@69: eachProp(enabledRegistry, function (mod) { rob@76: var map = mod.map, rob@76: modId = map.id; rc-web@69: rc-web@69: //Skip things that are not enabled or in error state. rc-web@69: if (!mod.enabled) { rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: if (!map.isDefine) { rc-web@69: reqCalls.push(mod); rc-web@69: } rc-web@69: rc-web@69: if (!mod.error) { rc-web@69: //If the module should be executed, and it has not rc-web@69: //been inited and time is up, remember it. rc-web@69: if (!mod.inited && expired) { rc-web@69: if (hasPathFallback(modId)) { rc-web@69: usingPathFallback = true; rc-web@69: stillLoading = true; rc-web@69: } else { rc-web@69: noLoads.push(modId); rc-web@69: removeScript(modId); rc-web@69: } rc-web@69: } else if (!mod.inited && mod.fetched && map.isDefine) { rc-web@69: stillLoading = true; rc-web@69: if (!map.prefix) { rc-web@69: //No reason to keep looking for unfinished rc-web@69: //loading. If the only stillLoading is a rc-web@69: //plugin resource though, keep going, rc-web@69: //because it may be that a plugin resource rc-web@69: //is waiting on a non-plugin cycle. rc-web@69: return (needCycleCheck = false); rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: }); rc-web@69: rc-web@69: if (expired && noLoads.length) { rc-web@69: //If wait time expired, throw error of unloaded modules. rc-web@69: err = makeError('timeout', 'Load timeout for modules: ' + noLoads, null, noLoads); rc-web@69: err.contextName = context.contextName; rc-web@69: return onError(err); rc-web@69: } rc-web@69: rc-web@69: //Not expired, check for a cycle. rc-web@69: if (needCycleCheck) { rc-web@69: each(reqCalls, function (mod) { rc-web@69: breakCycle(mod, {}, {}); rc-web@69: }); rc-web@69: } rc-web@69: rc-web@69: //If still waiting on loads, and the waiting load is something rc-web@69: //other than a plugin resource, or there are still outstanding rc-web@69: //scripts, then just try back later. rc-web@69: if ((!expired || usingPathFallback) && stillLoading) { rc-web@69: //Something is still waiting to load. Wait for it, but only rc-web@69: //if a timeout is not already in effect. rc-web@69: if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) { rc-web@69: checkLoadedTimeoutId = setTimeout(function () { rc-web@69: checkLoadedTimeoutId = 0; rc-web@69: checkLoaded(); rc-web@69: }, 50); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: inCheckLoaded = false; rc-web@69: } rc-web@69: rc-web@69: Module = function (map) { rc-web@69: this.events = getOwn(undefEvents, map.id) || {}; rc-web@69: this.map = map; rc-web@69: this.shim = getOwn(config.shim, map.id); rc-web@69: this.depExports = []; rc-web@69: this.depMaps = []; rc-web@69: this.depMatched = []; rc-web@69: this.pluginMaps = {}; rc-web@69: this.depCount = 0; rc-web@69: rc-web@69: /* this.exports this.factory rc-web@69: this.depMaps = [], rc-web@69: this.enabled, this.fetched rc-web@69: */ rc-web@69: }; rc-web@69: rc-web@69: Module.prototype = { rc-web@69: init: function (depMaps, factory, errback, options) { rc-web@69: options = options || {}; rc-web@69: rc-web@69: //Do not do more inits if already done. Can happen if there rc-web@69: //are multiple define calls for the same module. That is not rc-web@69: //a normal, common case, but it is also not unexpected. rc-web@69: if (this.inited) { rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: this.factory = factory; rc-web@69: rc-web@69: if (errback) { rc-web@69: //Register for errors on this module. rc-web@69: this.on('error', errback); rc-web@69: } else if (this.events.error) { rc-web@69: //If no errback already, but there are error listeners rc-web@69: //on this module, set up an errback to pass to the deps. rc-web@69: errback = bind(this, function (err) { rc-web@69: this.emit('error', err); rc-web@69: }); rc-web@69: } rc-web@69: rc-web@69: //Do a copy of the dependency array, so that rc-web@69: //source inputs are not modified. For example rc-web@69: //"shim" deps are passed in here directly, and rc-web@69: //doing a direct modification of the depMaps array rc-web@69: //would affect that config. rc-web@69: this.depMaps = depMaps && depMaps.slice(0); rc-web@69: rc-web@69: this.errback = errback; rc-web@69: rc-web@69: //Indicate this module has be initialized rc-web@69: this.inited = true; rc-web@69: rc-web@69: this.ignore = options.ignore; rc-web@69: rc-web@69: //Could have option to init this module in enabled mode, rc-web@69: //or could have been previously marked as enabled. However, rc-web@69: //the dependencies are not known until init is called. So rc-web@69: //if enabled previously, now trigger dependencies as enabled. rc-web@69: if (options.enabled || this.enabled) { rc-web@69: //Enable this module and dependencies. rc-web@69: //Will call this.check() rc-web@69: this.enable(); rc-web@69: } else { rc-web@69: this.check(); rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: defineDep: function (i, depExports) { rc-web@69: //Because of cycles, defined callback for a given rc-web@69: //export can be called more than once. rc-web@69: if (!this.depMatched[i]) { rc-web@69: this.depMatched[i] = true; rc-web@69: this.depCount -= 1; rc-web@69: this.depExports[i] = depExports; rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: fetch: function () { rc-web@69: if (this.fetched) { rc-web@69: return; rc-web@69: } rc-web@69: this.fetched = true; rc-web@69: rc-web@69: context.startTime = (new Date()).getTime(); rc-web@69: rc-web@69: var map = this.map; rc-web@69: rc-web@69: //If the manager is for a plugin managed resource, rc-web@69: //ask the plugin to load it now. rc-web@69: if (this.shim) { rc-web@69: context.makeRequire(this.map, { rc-web@69: enableBuildCallback: true rc-web@69: })(this.shim.deps || [], bind(this, function () { rc-web@69: return map.prefix ? this.callPlugin() : this.load(); rc-web@69: })); rc-web@69: } else { rc-web@69: //Regular dependency. rc-web@69: return map.prefix ? this.callPlugin() : this.load(); rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: load: function () { rc-web@69: var url = this.map.url; rc-web@69: rc-web@69: //Regular dependency. rc-web@69: if (!urlFetched[url]) { rc-web@69: urlFetched[url] = true; rc-web@69: context.load(this.map.id, url); rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * Checks if the module is ready to define itself, and if so, rc-web@69: * define it. rc-web@69: */ rc-web@69: check: function () { rc-web@69: if (!this.enabled || this.enabling) { rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: var err, cjsModule, rc-web@69: id = this.map.id, rc-web@69: depExports = this.depExports, rc-web@69: exports = this.exports, rc-web@69: factory = this.factory; rc-web@69: rc-web@69: if (!this.inited) { rc-web@69: this.fetch(); rc-web@69: } else if (this.error) { rc-web@69: this.emit('error', this.error); rc-web@69: } else if (!this.defining) { rc-web@69: //The factory could trigger another require call rc-web@69: //that would result in checking this module to rc-web@69: //define itself again. If already in the process rc-web@69: //of doing that, skip this work. rc-web@69: this.defining = true; rc-web@69: rc-web@69: if (this.depCount < 1 && !this.defined) { rc-web@69: if (isFunction(factory)) { rc-web@69: //If there is an error listener, favor passing rc-web@69: //to that instead of throwing an error. However, rc-web@69: //only do it for define()'d modules. require rc-web@69: //errbacks should not be called for failures in rc-web@69: //their callbacks (#699). However if a global rc-web@69: //onError is set, use that. rc-web@69: if ((this.events.error && this.map.isDefine) || rc-web@69: req.onError !== defaultOnError) { rc-web@69: try { rc-web@69: exports = context.execCb(id, factory, depExports, exports); rc-web@69: } catch (e) { rc-web@69: err = e; rc-web@69: } rc-web@69: } else { rc-web@69: exports = context.execCb(id, factory, depExports, exports); rc-web@69: } rc-web@69: rob@76: // Favor return value over exports. If node/cjs in play, rob@76: // then will not have a return value anyway. Favor rob@76: // module.exports assignment over exports object. rob@76: if (this.map.isDefine && exports === undefined) { rc-web@69: cjsModule = this.module; rob@76: if (cjsModule) { rc-web@69: exports = cjsModule.exports; rob@76: } else if (this.usingExports) { rc-web@69: //exports already set the defined value. rc-web@69: exports = this.exports; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: if (err) { rc-web@69: err.requireMap = this.map; rc-web@69: err.requireModules = this.map.isDefine ? [this.map.id] : null; rc-web@69: err.requireType = this.map.isDefine ? 'define' : 'require'; rc-web@69: return onError((this.error = err)); rc-web@69: } rc-web@69: rc-web@69: } else { rc-web@69: //Just a literal value rc-web@69: exports = factory; rc-web@69: } rc-web@69: rc-web@69: this.exports = exports; rc-web@69: rc-web@69: if (this.map.isDefine && !this.ignore) { rc-web@69: defined[id] = exports; rc-web@69: rc-web@69: if (req.onResourceLoad) { rc-web@69: req.onResourceLoad(context, this.map, this.depMaps); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //Clean up rc-web@69: cleanRegistry(id); rc-web@69: rc-web@69: this.defined = true; rc-web@69: } rc-web@69: rc-web@69: //Finished the define stage. Allow calling check again rc-web@69: //to allow define notifications below in the case of a rc-web@69: //cycle. rc-web@69: this.defining = false; rc-web@69: rc-web@69: if (this.defined && !this.defineEmitted) { rc-web@69: this.defineEmitted = true; rc-web@69: this.emit('defined', this.exports); rc-web@69: this.defineEmitComplete = true; rc-web@69: } rc-web@69: rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: callPlugin: function () { rc-web@69: var map = this.map, rc-web@69: id = map.id, rc-web@69: //Map already normalized the prefix. rc-web@69: pluginMap = makeModuleMap(map.prefix); rc-web@69: rc-web@69: //Mark this as a dependency for this plugin, so it rc-web@69: //can be traced for cycles. rc-web@69: this.depMaps.push(pluginMap); rc-web@69: rc-web@69: on(pluginMap, 'defined', bind(this, function (plugin) { rc-web@69: var load, normalizedMap, normalizedMod, rob@76: bundleId = getOwn(bundlesMap, this.map.id), rc-web@69: name = this.map.name, rc-web@69: parentName = this.map.parentMap ? this.map.parentMap.name : null, rc-web@69: localRequire = context.makeRequire(map.parentMap, { rc-web@69: enableBuildCallback: true rc-web@69: }); rc-web@69: rc-web@69: //If current map is not normalized, wait for that rc-web@69: //normalized name to load instead of continuing. rc-web@69: if (this.map.unnormalized) { rc-web@69: //Normalize the ID if the plugin allows it. rc-web@69: if (plugin.normalize) { rc-web@69: name = plugin.normalize(name, function (name) { rc-web@69: return normalize(name, parentName, true); rc-web@69: }) || ''; rc-web@69: } rc-web@69: rc-web@69: //prefix and name should already be normalized, no need rc-web@69: //for applying map config again either. rc-web@69: normalizedMap = makeModuleMap(map.prefix + '!' + name, rc-web@69: this.map.parentMap); rc-web@69: on(normalizedMap, rc-web@69: 'defined', bind(this, function (value) { rc-web@69: this.init([], function () { return value; }, null, { rc-web@69: enabled: true, rc-web@69: ignore: true rc-web@69: }); rc-web@69: })); rc-web@69: rc-web@69: normalizedMod = getOwn(registry, normalizedMap.id); rc-web@69: if (normalizedMod) { rc-web@69: //Mark this as a dependency for this plugin, so it rc-web@69: //can be traced for cycles. rc-web@69: this.depMaps.push(normalizedMap); rc-web@69: rc-web@69: if (this.events.error) { rc-web@69: normalizedMod.on('error', bind(this, function (err) { rc-web@69: this.emit('error', err); rc-web@69: })); rc-web@69: } rc-web@69: normalizedMod.enable(); rc-web@69: } rc-web@69: rc-web@69: return; rc-web@69: } rc-web@69: rob@76: //If a paths config, then just load that file instead to rob@76: //resolve the plugin, as it is built into that paths layer. rob@76: if (bundleId) { rob@76: this.map.url = context.nameToUrl(bundleId); rob@76: this.load(); rob@76: return; rob@76: } rob@76: rc-web@69: load = bind(this, function (value) { rc-web@69: this.init([], function () { return value; }, null, { rc-web@69: enabled: true rc-web@69: }); rc-web@69: }); rc-web@69: rc-web@69: load.error = bind(this, function (err) { rc-web@69: this.inited = true; rc-web@69: this.error = err; rc-web@69: err.requireModules = [id]; rc-web@69: rc-web@69: //Remove temp unnormalized modules for this module, rc-web@69: //since they will never be resolved otherwise now. rc-web@69: eachProp(registry, function (mod) { rc-web@69: if (mod.map.id.indexOf(id + '_unnormalized') === 0) { rc-web@69: cleanRegistry(mod.map.id); rc-web@69: } rc-web@69: }); rc-web@69: rc-web@69: onError(err); rc-web@69: }); rc-web@69: rc-web@69: //Allow plugins to load other code without having to know the rc-web@69: //context or how to 'complete' the load. rc-web@69: load.fromText = bind(this, function (text, textAlt) { rc-web@69: /*jslint evil: true */ rc-web@69: var moduleName = map.name, rc-web@69: moduleMap = makeModuleMap(moduleName), rc-web@69: hasInteractive = useInteractive; rc-web@69: rc-web@69: //As of 2.1.0, support just passing the text, to reinforce rc-web@69: //fromText only being called once per resource. Still rc-web@69: //support old style of passing moduleName but discard rc-web@69: //that moduleName in favor of the internal ref. rc-web@69: if (textAlt) { rc-web@69: text = textAlt; rc-web@69: } rc-web@69: rc-web@69: //Turn off interactive script matching for IE for any define rc-web@69: //calls in the text, then turn it back on at the end. rc-web@69: if (hasInteractive) { rc-web@69: useInteractive = false; rc-web@69: } rc-web@69: rc-web@69: //Prime the system by creating a module instance for rc-web@69: //it. rc-web@69: getModule(moduleMap); rc-web@69: rc-web@69: //Transfer any config to this other module. rc-web@69: if (hasProp(config.config, id)) { rc-web@69: config.config[moduleName] = config.config[id]; rc-web@69: } rc-web@69: rc-web@69: try { rc-web@69: req.exec(text); rc-web@69: } catch (e) { rc-web@69: return onError(makeError('fromtexteval', rc-web@69: 'fromText eval for ' + id + rc-web@69: ' failed: ' + e, rc-web@69: e, rc-web@69: [id])); rc-web@69: } rc-web@69: rc-web@69: if (hasInteractive) { rc-web@69: useInteractive = true; rc-web@69: } rc-web@69: rc-web@69: //Mark this as a dependency for the plugin rc-web@69: //resource rc-web@69: this.depMaps.push(moduleMap); rc-web@69: rc-web@69: //Support anonymous modules. rc-web@69: context.completeLoad(moduleName); rc-web@69: rc-web@69: //Bind the value of that module to the value for this rc-web@69: //resource ID. rc-web@69: localRequire([moduleName], load); rc-web@69: }); rc-web@69: rc-web@69: //Use parentName here since the plugin's name is not reliable, rc-web@69: //could be some weird string with no path that actually wants to rc-web@69: //reference the parentName's path. rc-web@69: plugin.load(map.name, localRequire, load, config); rc-web@69: })); rc-web@69: rc-web@69: context.enable(pluginMap, this); rc-web@69: this.pluginMaps[pluginMap.id] = pluginMap; rc-web@69: }, rc-web@69: rc-web@69: enable: function () { rc-web@69: enabledRegistry[this.map.id] = this; rc-web@69: this.enabled = true; rc-web@69: rc-web@69: //Set flag mentioning that the module is enabling, rc-web@69: //so that immediate calls to the defined callbacks rc-web@69: //for dependencies do not trigger inadvertent load rc-web@69: //with the depCount still being zero. rc-web@69: this.enabling = true; rc-web@69: rc-web@69: //Enable each dependency rc-web@69: each(this.depMaps, bind(this, function (depMap, i) { rc-web@69: var id, mod, handler; rc-web@69: rc-web@69: if (typeof depMap === 'string') { rc-web@69: //Dependency needs to be converted to a depMap rc-web@69: //and wired up to this module. rc-web@69: depMap = makeModuleMap(depMap, rc-web@69: (this.map.isDefine ? this.map : this.map.parentMap), rc-web@69: false, rc-web@69: !this.skipMap); rc-web@69: this.depMaps[i] = depMap; rc-web@69: rc-web@69: handler = getOwn(handlers, depMap.id); rc-web@69: rc-web@69: if (handler) { rc-web@69: this.depExports[i] = handler(this); rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: this.depCount += 1; rc-web@69: rc-web@69: on(depMap, 'defined', bind(this, function (depExports) { rc-web@69: this.defineDep(i, depExports); rc-web@69: this.check(); rc-web@69: })); rc-web@69: rc-web@69: if (this.errback) { rc-web@69: on(depMap, 'error', bind(this, this.errback)); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: id = depMap.id; rc-web@69: mod = registry[id]; rc-web@69: rc-web@69: //Skip special modules like 'require', 'exports', 'module' rc-web@69: //Also, don't call enable if it is already enabled, rc-web@69: //important in circular dependency cases. rc-web@69: if (!hasProp(handlers, id) && mod && !mod.enabled) { rc-web@69: context.enable(depMap, this); rc-web@69: } rc-web@69: })); rc-web@69: rc-web@69: //Enable each plugin that is used in rc-web@69: //a dependency rc-web@69: eachProp(this.pluginMaps, bind(this, function (pluginMap) { rc-web@69: var mod = getOwn(registry, pluginMap.id); rc-web@69: if (mod && !mod.enabled) { rc-web@69: context.enable(pluginMap, this); rc-web@69: } rc-web@69: })); rc-web@69: rc-web@69: this.enabling = false; rc-web@69: rc-web@69: this.check(); rc-web@69: }, rc-web@69: rc-web@69: on: function (name, cb) { rc-web@69: var cbs = this.events[name]; rc-web@69: if (!cbs) { rc-web@69: cbs = this.events[name] = []; rc-web@69: } rc-web@69: cbs.push(cb); rc-web@69: }, rc-web@69: rc-web@69: emit: function (name, evt) { rc-web@69: each(this.events[name], function (cb) { rc-web@69: cb(evt); rc-web@69: }); rc-web@69: if (name === 'error') { rc-web@69: //Now that the error handler was triggered, remove rc-web@69: //the listeners, since this broken Module instance rc-web@69: //can stay around for a while in the registry. rc-web@69: delete this.events[name]; rc-web@69: } rc-web@69: } rc-web@69: }; rc-web@69: rc-web@69: function callGetModule(args) { rc-web@69: //Skip modules already defined. rc-web@69: if (!hasProp(defined, args[0])) { rc-web@69: getModule(makeModuleMap(args[0], null, true)).init(args[1], args[2]); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: function removeListener(node, func, name, ieName) { rc-web@69: //Favor detachEvent because of IE9 rc-web@69: //issue, see attachEvent/addEventListener comment elsewhere rc-web@69: //in this file. rc-web@69: if (node.detachEvent && !isOpera) { rc-web@69: //Probably IE. If not it will throw an error, which will be rc-web@69: //useful to know. rc-web@69: if (ieName) { rc-web@69: node.detachEvent(ieName, func); rc-web@69: } rc-web@69: } else { rc-web@69: node.removeEventListener(name, func, false); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Given an event from a script node, get the requirejs info from it, rc-web@69: * and then removes the event listeners on the node. rc-web@69: * @param {Event} evt rc-web@69: * @returns {Object} rc-web@69: */ rc-web@69: function getScriptData(evt) { rc-web@69: //Using currentTarget instead of target for Firefox 2.0's sake. Not rc-web@69: //all old browsers will be supported, but this one was easy enough rc-web@69: //to support and still makes sense. rc-web@69: var node = evt.currentTarget || evt.srcElement; rc-web@69: rc-web@69: //Remove the listeners once here. rc-web@69: removeListener(node, context.onScriptLoad, 'load', 'onreadystatechange'); rc-web@69: removeListener(node, context.onScriptError, 'error'); rc-web@69: rc-web@69: return { rc-web@69: node: node, rc-web@69: id: node && node.getAttribute('data-requiremodule') rc-web@69: }; rc-web@69: } rc-web@69: rc-web@69: function intakeDefines() { rc-web@69: var args; rc-web@69: rc-web@69: //Any defined modules in the global queue, intake them now. rc-web@69: takeGlobalQueue(); rc-web@69: rc-web@69: //Make sure any remaining defQueue items get properly processed. rc-web@69: while (defQueue.length) { rc-web@69: args = defQueue.shift(); rc-web@69: if (args[0] === null) { rc-web@69: return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1])); rc-web@69: } else { rc-web@69: //args are id, deps, factory. Should be normalized by the rc-web@69: //define() function. rc-web@69: callGetModule(args); rc-web@69: } rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: context = { rc-web@69: config: config, rc-web@69: contextName: contextName, rc-web@69: registry: registry, rc-web@69: defined: defined, rc-web@69: urlFetched: urlFetched, rc-web@69: defQueue: defQueue, rc-web@69: Module: Module, rc-web@69: makeModuleMap: makeModuleMap, rc-web@69: nextTick: req.nextTick, rc-web@69: onError: onError, rc-web@69: rc-web@69: /** rc-web@69: * Set a configuration for the context. rc-web@69: * @param {Object} cfg config object to integrate. rc-web@69: */ rc-web@69: configure: function (cfg) { rc-web@69: //Make sure the baseUrl ends in a slash. rc-web@69: if (cfg.baseUrl) { rc-web@69: if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== '/') { rc-web@69: cfg.baseUrl += '/'; rc-web@69: } rc-web@69: } rc-web@69: rob@76: //Save off the paths since they require special processing, rc-web@69: //they are additive. rob@76: var shim = config.shim, rc-web@69: objs = { rc-web@69: paths: true, rob@76: bundles: true, rc-web@69: config: true, rc-web@69: map: true rc-web@69: }; rc-web@69: rc-web@69: eachProp(cfg, function (value, prop) { rc-web@69: if (objs[prop]) { rob@76: if (!config[prop]) { rob@76: config[prop] = {}; rc-web@69: } rob@76: mixin(config[prop], value, true, true); rc-web@69: } else { rc-web@69: config[prop] = value; rc-web@69: } rc-web@69: }); rc-web@69: rob@76: //Reverse map the bundles rob@76: if (cfg.bundles) { rob@76: eachProp(cfg.bundles, function (value, prop) { rob@76: each(value, function (v) { rob@76: if (v !== prop) { rob@76: bundlesMap[v] = prop; rob@76: } rob@76: }); rob@76: }); rob@76: } rob@76: rc-web@69: //Merge shim rc-web@69: if (cfg.shim) { rc-web@69: eachProp(cfg.shim, function (value, id) { rc-web@69: //Normalize the structure rc-web@69: if (isArray(value)) { rc-web@69: value = { rc-web@69: deps: value rc-web@69: }; rc-web@69: } rc-web@69: if ((value.exports || value.init) && !value.exportsFn) { rc-web@69: value.exportsFn = context.makeShimExports(value); rc-web@69: } rc-web@69: shim[id] = value; rc-web@69: }); rc-web@69: config.shim = shim; rc-web@69: } rc-web@69: rc-web@69: //Adjust packages if necessary. rc-web@69: if (cfg.packages) { rc-web@69: each(cfg.packages, function (pkgObj) { rob@76: var location, name; rc-web@69: rc-web@69: pkgObj = typeof pkgObj === 'string' ? { name: pkgObj } : pkgObj; rob@76: rob@76: name = pkgObj.name; rc-web@69: location = pkgObj.location; rob@76: if (location) { rob@76: config.paths[name] = pkgObj.location; rob@76: } rc-web@69: rob@76: //Save pointer to main module ID for pkg name. rob@76: //Remove leading dot in main, so main paths are normalized, rob@76: //and remove any trailing .js, since different package rob@76: //envs have different conventions: some use a module name, rob@76: //some use a file name. rob@76: config.pkgs[name] = pkgObj.name + '/' + (pkgObj.main || 'main') rob@76: .replace(currDirRegExp, '') rob@76: .replace(jsSuffixRegExp, ''); rc-web@69: }); rc-web@69: } rc-web@69: rc-web@69: //If there are any "waiting to execute" modules in the registry, rc-web@69: //update the maps for them, since their info, like URLs to load, rc-web@69: //may have changed. rc-web@69: eachProp(registry, function (mod, id) { rc-web@69: //If module already has init called, since it is too rc-web@69: //late to modify them, and ignore unnormalized ones rc-web@69: //since they are transient. rc-web@69: if (!mod.inited && !mod.map.unnormalized) { rc-web@69: mod.map = makeModuleMap(id); rc-web@69: } rc-web@69: }); rc-web@69: rc-web@69: //If a deps array or a config callback is specified, then call rc-web@69: //require with those args. This is useful when require is defined as a rc-web@69: //config object before require.js is loaded. rc-web@69: if (cfg.deps || cfg.callback) { rc-web@69: context.require(cfg.deps || [], cfg.callback); rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: makeShimExports: function (value) { rc-web@69: function fn() { rc-web@69: var ret; rc-web@69: if (value.init) { rc-web@69: ret = value.init.apply(global, arguments); rc-web@69: } rc-web@69: return ret || (value.exports && getGlobal(value.exports)); rc-web@69: } rc-web@69: return fn; rc-web@69: }, rc-web@69: rc-web@69: makeRequire: function (relMap, options) { rc-web@69: options = options || {}; rc-web@69: rc-web@69: function localRequire(deps, callback, errback) { rc-web@69: var id, map, requireMod; rc-web@69: rc-web@69: if (options.enableBuildCallback && callback && isFunction(callback)) { rc-web@69: callback.__requireJsBuild = true; rc-web@69: } rc-web@69: rc-web@69: if (typeof deps === 'string') { rc-web@69: if (isFunction(callback)) { rc-web@69: //Invalid call rc-web@69: return onError(makeError('requireargs', 'Invalid require call'), errback); rc-web@69: } rc-web@69: rc-web@69: //If require|exports|module are requested, get the rc-web@69: //value for them from the special handlers. Caveat: rc-web@69: //this only works while module is being defined. rc-web@69: if (relMap && hasProp(handlers, deps)) { rc-web@69: return handlers[deps](registry[relMap.id]); rc-web@69: } rc-web@69: rc-web@69: //Synchronous access to one module. If require.get is rc-web@69: //available (as in the Node adapter), prefer that. rc-web@69: if (req.get) { rc-web@69: return req.get(context, deps, relMap, localRequire); rc-web@69: } rc-web@69: rc-web@69: //Normalize module name, if it contains . or .. rc-web@69: map = makeModuleMap(deps, relMap, false, true); rc-web@69: id = map.id; rc-web@69: rc-web@69: if (!hasProp(defined, id)) { rc-web@69: return onError(makeError('notloaded', 'Module name "' + rc-web@69: id + rc-web@69: '" has not been loaded yet for context: ' + rc-web@69: contextName + rc-web@69: (relMap ? '' : '. Use require([])'))); rc-web@69: } rc-web@69: return defined[id]; rc-web@69: } rc-web@69: rc-web@69: //Grab defines waiting in the global queue. rc-web@69: intakeDefines(); rc-web@69: rc-web@69: //Mark all the dependencies as needing to be loaded. rc-web@69: context.nextTick(function () { rc-web@69: //Some defines could have been added since the rc-web@69: //require call, collect them. rc-web@69: intakeDefines(); rc-web@69: rc-web@69: requireMod = getModule(makeModuleMap(null, relMap)); rc-web@69: rc-web@69: //Store if map config should be applied to this require rc-web@69: //call for dependencies. rc-web@69: requireMod.skipMap = options.skipMap; rc-web@69: rc-web@69: requireMod.init(deps, callback, errback, { rc-web@69: enabled: true rc-web@69: }); rc-web@69: rc-web@69: checkLoaded(); rc-web@69: }); rc-web@69: rc-web@69: return localRequire; rc-web@69: } rc-web@69: rc-web@69: mixin(localRequire, { rc-web@69: isBrowser: isBrowser, rc-web@69: rc-web@69: /** rc-web@69: * Converts a module name + .extension into an URL path. rc-web@69: * *Requires* the use of a module name. It does not support using rc-web@69: * plain URLs like nameToUrl. rc-web@69: */ rc-web@69: toUrl: function (moduleNamePlusExt) { rc-web@69: var ext, rc-web@69: index = moduleNamePlusExt.lastIndexOf('.'), rc-web@69: segment = moduleNamePlusExt.split('/')[0], rc-web@69: isRelative = segment === '.' || segment === '..'; rc-web@69: rc-web@69: //Have a file extension alias, and it is not the rc-web@69: //dots from a relative path. rc-web@69: if (index !== -1 && (!isRelative || index > 1)) { rc-web@69: ext = moduleNamePlusExt.substring(index, moduleNamePlusExt.length); rc-web@69: moduleNamePlusExt = moduleNamePlusExt.substring(0, index); rc-web@69: } rc-web@69: rc-web@69: return context.nameToUrl(normalize(moduleNamePlusExt, rc-web@69: relMap && relMap.id, true), ext, true); rc-web@69: }, rc-web@69: rc-web@69: defined: function (id) { rc-web@69: return hasProp(defined, makeModuleMap(id, relMap, false, true).id); rc-web@69: }, rc-web@69: rc-web@69: specified: function (id) { rc-web@69: id = makeModuleMap(id, relMap, false, true).id; rc-web@69: return hasProp(defined, id) || hasProp(registry, id); rc-web@69: } rc-web@69: }); rc-web@69: rc-web@69: //Only allow undef on top level require calls rc-web@69: if (!relMap) { rc-web@69: localRequire.undef = function (id) { rc-web@69: //Bind any waiting define() calls to this context, rc-web@69: //fix for #408 rc-web@69: takeGlobalQueue(); rc-web@69: rc-web@69: var map = makeModuleMap(id, relMap, true), rc-web@69: mod = getOwn(registry, id); rc-web@69: rc-web@69: removeScript(id); rc-web@69: rc-web@69: delete defined[id]; rc-web@69: delete urlFetched[map.url]; rc-web@69: delete undefEvents[id]; rc-web@69: rob@76: //Clean queued defines too. Go backwards rob@76: //in array so that the splices do not rob@76: //mess up the iteration. rob@76: eachReverse(defQueue, function(args, i) { rob@76: if(args[0] === id) { rob@76: defQueue.splice(i, 1); rob@76: } rob@76: }); rob@76: rc-web@69: if (mod) { rc-web@69: //Hold on to listeners in case the rc-web@69: //module will be attempted to be reloaded rc-web@69: //using a different config. rc-web@69: if (mod.events.defined) { rc-web@69: undefEvents[id] = mod.events; rc-web@69: } rc-web@69: rc-web@69: cleanRegistry(id); rc-web@69: } rc-web@69: }; rc-web@69: } rc-web@69: rc-web@69: return localRequire; rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * Called to enable a module if it is still in the registry rc-web@69: * awaiting enablement. A second arg, parent, the parent module, rob@76: * is passed in for context, when this method is overridden by rc-web@69: * the optimizer. Not shown here to keep code compact. rc-web@69: */ rc-web@69: enable: function (depMap) { rc-web@69: var mod = getOwn(registry, depMap.id); rc-web@69: if (mod) { rc-web@69: getModule(depMap).enable(); rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * Internal method used by environment adapters to complete a load event. rc-web@69: * A load event could be a script load or just a load pass from a synchronous rc-web@69: * load call. rc-web@69: * @param {String} moduleName the name of the module to potentially complete. rc-web@69: */ rc-web@69: completeLoad: function (moduleName) { rc-web@69: var found, args, mod, rc-web@69: shim = getOwn(config.shim, moduleName) || {}, rc-web@69: shExports = shim.exports; rc-web@69: rc-web@69: takeGlobalQueue(); rc-web@69: rc-web@69: while (defQueue.length) { rc-web@69: args = defQueue.shift(); rc-web@69: if (args[0] === null) { rc-web@69: args[0] = moduleName; rc-web@69: //If already found an anonymous module and bound it rc-web@69: //to this name, then this is some other anon module rc-web@69: //waiting for its completeLoad to fire. rc-web@69: if (found) { rc-web@69: break; rc-web@69: } rc-web@69: found = true; rc-web@69: } else if (args[0] === moduleName) { rc-web@69: //Found matching define call for this script! rc-web@69: found = true; rc-web@69: } rc-web@69: rc-web@69: callGetModule(args); rc-web@69: } rc-web@69: rc-web@69: //Do this after the cycle of callGetModule in case the result rc-web@69: //of those calls/init calls changes the registry. rc-web@69: mod = getOwn(registry, moduleName); rc-web@69: rc-web@69: if (!found && !hasProp(defined, moduleName) && mod && !mod.inited) { rc-web@69: if (config.enforceDefine && (!shExports || !getGlobal(shExports))) { rc-web@69: if (hasPathFallback(moduleName)) { rc-web@69: return; rc-web@69: } else { rc-web@69: return onError(makeError('nodefine', rc-web@69: 'No define call for ' + moduleName, rc-web@69: null, rc-web@69: [moduleName])); rc-web@69: } rc-web@69: } else { rc-web@69: //A script that does not call define(), so just simulate rc-web@69: //the call for it. rc-web@69: callGetModule([moduleName, (shim.deps || []), shim.exportsFn]); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: checkLoaded(); rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * Converts a module name to a file path. Supports cases where rc-web@69: * moduleName may actually be just an URL. rc-web@69: * Note that it **does not** call normalize on the moduleName, rc-web@69: * it is assumed to have already been normalized. This is an rc-web@69: * internal API, not a public one. Use toUrl for the public API. rc-web@69: */ rc-web@69: nameToUrl: function (moduleName, ext, skipExt) { rob@76: var paths, syms, i, parentModule, url, rob@76: parentPath, bundleId, rob@76: pkgMain = getOwn(config.pkgs, moduleName); rob@76: rob@76: if (pkgMain) { rob@76: moduleName = pkgMain; rob@76: } rob@76: rob@76: bundleId = getOwn(bundlesMap, moduleName); rob@76: rob@76: if (bundleId) { rob@76: return context.nameToUrl(bundleId, ext, skipExt); rob@76: } rc-web@69: rc-web@69: //If a colon is in the URL, it indicates a protocol is used and it is just rc-web@69: //an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?) rc-web@69: //or ends with .js, then assume the user meant to use an url and not a module id. rc-web@69: //The slash is important for protocol-less URLs as well as full paths. rc-web@69: if (req.jsExtRegExp.test(moduleName)) { rc-web@69: //Just a plain path, not module name lookup, so just return it. rc-web@69: //Add extension if it is included. This is a bit wonky, only non-.js things pass rc-web@69: //an extension, this method probably needs to be reworked. rc-web@69: url = moduleName + (ext || ''); rc-web@69: } else { rc-web@69: //A module that needs to be converted to a path. rc-web@69: paths = config.paths; rc-web@69: rc-web@69: syms = moduleName.split('/'); rc-web@69: //For each module name segment, see if there is a path rc-web@69: //registered for it. Start with most specific name rc-web@69: //and work up from it. rc-web@69: for (i = syms.length; i > 0; i -= 1) { rc-web@69: parentModule = syms.slice(0, i).join('/'); rob@76: rc-web@69: parentPath = getOwn(paths, parentModule); rc-web@69: if (parentPath) { rc-web@69: //If an array, it means there are a few choices, rc-web@69: //Choose the one that is desired rc-web@69: if (isArray(parentPath)) { rc-web@69: parentPath = parentPath[0]; rc-web@69: } rc-web@69: syms.splice(0, i, parentPath); rc-web@69: break; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //Join the path parts together, then figure out if baseUrl is needed. rc-web@69: url = syms.join('/'); rc-web@69: url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js')); rc-web@69: url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url; rc-web@69: } rc-web@69: rc-web@69: return config.urlArgs ? url + rc-web@69: ((url.indexOf('?') === -1 ? '?' : '&') + rc-web@69: config.urlArgs) : url; rc-web@69: }, rc-web@69: rc-web@69: //Delegates to req.load. Broken out as a separate function to rc-web@69: //allow overriding in the optimizer. rc-web@69: load: function (id, url) { rc-web@69: req.load(context, id, url); rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * Executes a module callback function. Broken out as a separate function rc-web@69: * solely to allow the build system to sequence the files in the built rc-web@69: * layer in the right sequence. rc-web@69: * rc-web@69: * @private rc-web@69: */ rc-web@69: execCb: function (name, callback, args, exports) { rc-web@69: return callback.apply(exports, args); rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * callback for script loads, used to check status of loading. rc-web@69: * rc-web@69: * @param {Event} evt the event from the browser for the script rc-web@69: * that was loaded. rc-web@69: */ rc-web@69: onScriptLoad: function (evt) { rc-web@69: //Using currentTarget instead of target for Firefox 2.0's sake. Not rc-web@69: //all old browsers will be supported, but this one was easy enough rc-web@69: //to support and still makes sense. rc-web@69: if (evt.type === 'load' || rc-web@69: (readyRegExp.test((evt.currentTarget || evt.srcElement).readyState))) { rc-web@69: //Reset interactive script so a script node is not held onto for rc-web@69: //to long. rc-web@69: interactiveScript = null; rc-web@69: rc-web@69: //Pull out the name of the module and the context. rc-web@69: var data = getScriptData(evt); rc-web@69: context.completeLoad(data.id); rc-web@69: } rc-web@69: }, rc-web@69: rc-web@69: /** rc-web@69: * Callback for script errors. rc-web@69: */ rc-web@69: onScriptError: function (evt) { rc-web@69: var data = getScriptData(evt); rc-web@69: if (!hasPathFallback(data.id)) { rc-web@69: return onError(makeError('scripterror', 'Script error for: ' + data.id, evt, [data.id])); rc-web@69: } rc-web@69: } rc-web@69: }; rc-web@69: rc-web@69: context.require = context.makeRequire(); rc-web@69: return context; rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Main entry point. rc-web@69: * rc-web@69: * If the only argument to require is a string, then the module that rc-web@69: * is represented by that string is fetched for the appropriate context. rc-web@69: * rc-web@69: * If the first argument is an array, then it will be treated as an array rc-web@69: * of dependency string names to fetch. An optional function callback can rc-web@69: * be specified to execute when all of those dependencies are available. rc-web@69: * rc-web@69: * Make a local req variable to help Caja compliance (it assumes things rc-web@69: * on a require that are not standardized), and to give a short rc-web@69: * name for minification/local scope use. rc-web@69: */ rc-web@69: req = requirejs = function (deps, callback, errback, optional) { rc-web@69: rc-web@69: //Find the right context, use default rc-web@69: var context, config, rc-web@69: contextName = defContextName; rc-web@69: rc-web@69: // Determine if have config object in the call. rc-web@69: if (!isArray(deps) && typeof deps !== 'string') { rc-web@69: // deps is a config object rc-web@69: config = deps; rc-web@69: if (isArray(callback)) { rc-web@69: // Adjust args if there are dependencies rc-web@69: deps = callback; rc-web@69: callback = errback; rc-web@69: errback = optional; rc-web@69: } else { rc-web@69: deps = []; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: if (config && config.context) { rc-web@69: contextName = config.context; rc-web@69: } rc-web@69: rc-web@69: context = getOwn(contexts, contextName); rc-web@69: if (!context) { rc-web@69: context = contexts[contextName] = req.s.newContext(contextName); rc-web@69: } rc-web@69: rc-web@69: if (config) { rc-web@69: context.configure(config); rc-web@69: } rc-web@69: rc-web@69: return context.require(deps, callback, errback); rc-web@69: }; rc-web@69: rc-web@69: /** rc-web@69: * Support require.config() to make it easier to cooperate with other rc-web@69: * AMD loaders on globally agreed names. rc-web@69: */ rc-web@69: req.config = function (config) { rc-web@69: return req(config); rc-web@69: }; rc-web@69: rc-web@69: /** rc-web@69: * Execute something after the current tick rc-web@69: * of the event loop. Override for other envs rc-web@69: * that have a better solution than setTimeout. rc-web@69: * @param {Function} fn function to execute later. rc-web@69: */ rc-web@69: req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) { rc-web@69: setTimeout(fn, 4); rc-web@69: } : function (fn) { fn(); }; rc-web@69: rc-web@69: /** rc-web@69: * Export require as a global, but only if it does not already exist. rc-web@69: */ rc-web@69: if (!require) { rc-web@69: require = req; rc-web@69: } rc-web@69: rc-web@69: req.version = version; rc-web@69: rc-web@69: //Used to filter out dependencies that are already paths. rc-web@69: req.jsExtRegExp = /^\/|:|\?|\.js$/; rc-web@69: req.isBrowser = isBrowser; rc-web@69: s = req.s = { rc-web@69: contexts: contexts, rc-web@69: newContext: newContext rc-web@69: }; rc-web@69: rc-web@69: //Create default context. rc-web@69: req({}); rc-web@69: rc-web@69: //Exports some context-sensitive methods on global require. rc-web@69: each([ rc-web@69: 'toUrl', rc-web@69: 'undef', rc-web@69: 'defined', rc-web@69: 'specified' rc-web@69: ], function (prop) { rc-web@69: //Reference from contexts instead of early binding to default context, rc-web@69: //so that during builds, the latest instance of the default context rc-web@69: //with its config gets used. rc-web@69: req[prop] = function () { rc-web@69: var ctx = contexts[defContextName]; rc-web@69: return ctx.require[prop].apply(ctx, arguments); rc-web@69: }; rc-web@69: }); rc-web@69: rc-web@69: if (isBrowser) { rc-web@69: head = s.head = document.getElementsByTagName('head')[0]; rc-web@69: //If BASE tag is in play, using appendChild is a problem for IE6. rc-web@69: //When that browser dies, this can be removed. Details in this jQuery bug: rc-web@69: //http://dev.jquery.com/ticket/2709 rc-web@69: baseElement = document.getElementsByTagName('base')[0]; rc-web@69: if (baseElement) { rc-web@69: head = s.head = baseElement.parentNode; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * Any errors that require explicitly generates will be passed to this rc-web@69: * function. Intercept/override it if you want custom error handling. rc-web@69: * @param {Error} err the error object. rc-web@69: */ rc-web@69: req.onError = defaultOnError; rc-web@69: rc-web@69: /** rc-web@69: * Creates the node for the load command. Only used in browser envs. rc-web@69: */ rc-web@69: req.createNode = function (config, moduleName, url) { rc-web@69: var node = config.xhtml ? rc-web@69: document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') : rc-web@69: document.createElement('script'); rc-web@69: node.type = config.scriptType || 'text/javascript'; rc-web@69: node.charset = 'utf-8'; rc-web@69: node.async = true; rc-web@69: return node; rc-web@69: }; rc-web@69: rc-web@69: /** rc-web@69: * Does the request to load a module for the browser case. rc-web@69: * Make this a separate function to allow other environments rc-web@69: * to override it. rc-web@69: * rc-web@69: * @param {Object} context the require context to find state. rc-web@69: * @param {String} moduleName the name of the module. rc-web@69: * @param {Object} url the URL to the module. rc-web@69: */ rc-web@69: req.load = function (context, moduleName, url) { rc-web@69: var config = (context && context.config) || {}, rc-web@69: node; rc-web@69: if (isBrowser) { rc-web@69: //In the browser so use a script tag rc-web@69: node = req.createNode(config, moduleName, url); rc-web@69: rc-web@69: node.setAttribute('data-requirecontext', context.contextName); rc-web@69: node.setAttribute('data-requiremodule', moduleName); rc-web@69: rc-web@69: //Set up load listener. Test attachEvent first because IE9 has rc-web@69: //a subtle issue in its addEventListener and script onload firings rc-web@69: //that do not match the behavior of all other browsers with rc-web@69: //addEventListener support, which fire the onload event for a rc-web@69: //script right after the script execution. See: rc-web@69: //https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution rc-web@69: //UNFORTUNATELY Opera implements attachEvent but does not follow the script rc-web@69: //script execution mode. rc-web@69: if (node.attachEvent && rc-web@69: //Check if node.attachEvent is artificially added by custom script or rc-web@69: //natively supported by browser rc-web@69: //read https://github.com/jrburke/requirejs/issues/187 rc-web@69: //if we can NOT find [native code] then it must NOT natively supported. rc-web@69: //in IE8, node.attachEvent does not have toString() rc-web@69: //Note the test for "[native code" with no closing brace, see: rc-web@69: //https://github.com/jrburke/requirejs/issues/273 rc-web@69: !(node.attachEvent.toString && node.attachEvent.toString().indexOf('[native code') < 0) && rc-web@69: !isOpera) { rc-web@69: //Probably IE. IE (at least 6-8) do not fire rc-web@69: //script onload right after executing the script, so rc-web@69: //we cannot tie the anonymous define call to a name. rc-web@69: //However, IE reports the script as being in 'interactive' rc-web@69: //readyState at the time of the define call. rc-web@69: useInteractive = true; rc-web@69: rc-web@69: node.attachEvent('onreadystatechange', context.onScriptLoad); rc-web@69: //It would be great to add an error handler here to catch rc-web@69: //404s in IE9+. However, onreadystatechange will fire before rc-web@69: //the error handler, so that does not help. If addEventListener rc-web@69: //is used, then IE will fire error before load, but we cannot rc-web@69: //use that pathway given the connect.microsoft.com issue rc-web@69: //mentioned above about not doing the 'script execute, rc-web@69: //then fire the script load event listener before execute rc-web@69: //next script' that other browsers do. rc-web@69: //Best hope: IE10 fixes the issues, rc-web@69: //and then destroys all installs of IE 6-9. rc-web@69: //node.attachEvent('onerror', context.onScriptError); rc-web@69: } else { rc-web@69: node.addEventListener('load', context.onScriptLoad, false); rc-web@69: node.addEventListener('error', context.onScriptError, false); rc-web@69: } rc-web@69: node.src = url; rc-web@69: rc-web@69: //For some cache cases in IE 6-8, the script executes before the end rc-web@69: //of the appendChild execution, so to tie an anonymous define rc-web@69: //call to the module name (which is stored on the node), hold on rc-web@69: //to a reference to this node, but clear after the DOM insertion. rc-web@69: currentlyAddingScript = node; rc-web@69: if (baseElement) { rc-web@69: head.insertBefore(node, baseElement); rc-web@69: } else { rc-web@69: head.appendChild(node); rc-web@69: } rc-web@69: currentlyAddingScript = null; rc-web@69: rc-web@69: return node; rc-web@69: } else if (isWebWorker) { rc-web@69: try { rc-web@69: //In a web worker, use importScripts. This is not a very rc-web@69: //efficient use of importScripts, importScripts will block until rc-web@69: //its script is downloaded and evaluated. However, if web workers rc-web@69: //are in play, the expectation that a build has been done so that rc-web@69: //only one script needs to be loaded anyway. This may need to be rc-web@69: //reevaluated if other use cases become common. rc-web@69: importScripts(url); rc-web@69: rc-web@69: //Account for anonymous modules rc-web@69: context.completeLoad(moduleName); rc-web@69: } catch (e) { rc-web@69: context.onError(makeError('importscripts', rc-web@69: 'importScripts failed for ' + rc-web@69: moduleName + ' at ' + url, rc-web@69: e, rc-web@69: [moduleName])); rc-web@69: } rc-web@69: } rc-web@69: }; rc-web@69: rc-web@69: function getInteractiveScript() { rc-web@69: if (interactiveScript && interactiveScript.readyState === 'interactive') { rc-web@69: return interactiveScript; rc-web@69: } rc-web@69: rc-web@69: eachReverse(scripts(), function (script) { rc-web@69: if (script.readyState === 'interactive') { rc-web@69: return (interactiveScript = script); rc-web@69: } rc-web@69: }); rc-web@69: return interactiveScript; rc-web@69: } rc-web@69: rc-web@69: //Look for a data-main script attribute, which could also adjust the baseUrl. rc-web@69: if (isBrowser && !cfg.skipDataMain) { rc-web@69: //Figure out baseUrl. Get it from the script tag with require.js in it. rc-web@69: eachReverse(scripts(), function (script) { rc-web@69: //Set the 'head' where we can append children by rc-web@69: //using the script's parent. rc-web@69: if (!head) { rc-web@69: head = script.parentNode; rc-web@69: } rc-web@69: rc-web@69: //Look for a data-main attribute to set main script for the page rc-web@69: //to load. If it is there, the path to data main becomes the rc-web@69: //baseUrl, if it is not already set. rc-web@69: dataMain = script.getAttribute('data-main'); rc-web@69: if (dataMain) { rc-web@69: //Preserve dataMain in case it is a path (i.e. contains '?') rc-web@69: mainScript = dataMain; rc-web@69: rc-web@69: //Set final baseUrl if there is not already an explicit one. rc-web@69: if (!cfg.baseUrl) { rc-web@69: //Pull off the directory of data-main for use as the rc-web@69: //baseUrl. rc-web@69: src = mainScript.split('/'); rc-web@69: mainScript = src.pop(); rc-web@69: subPath = src.length ? src.join('/') + '/' : './'; rc-web@69: rc-web@69: cfg.baseUrl = subPath; rc-web@69: } rc-web@69: rc-web@69: //Strip off any trailing .js since mainScript is now rc-web@69: //like a module name. rc-web@69: mainScript = mainScript.replace(jsSuffixRegExp, ''); rc-web@69: rc-web@69: //If mainScript is still a path, fall back to dataMain rc-web@69: if (req.jsExtRegExp.test(mainScript)) { rc-web@69: mainScript = dataMain; rc-web@69: } rc-web@69: rc-web@69: //Put the data-main script in the files to load. rc-web@69: cfg.deps = cfg.deps ? cfg.deps.concat(mainScript) : [mainScript]; rc-web@69: rc-web@69: return true; rc-web@69: } rc-web@69: }); rc-web@69: } rc-web@69: rc-web@69: /** rc-web@69: * The function that handles definitions of modules. Differs from rc-web@69: * require() in that a string for the module should be the first argument, rc-web@69: * and the function to execute after dependencies are loaded should rc-web@69: * return a value to define the module corresponding to the first argument's rc-web@69: * name. rc-web@69: */ rc-web@69: define = function (name, deps, callback) { rc-web@69: var node, context; rc-web@69: rc-web@69: //Allow for anonymous modules rc-web@69: if (typeof name !== 'string') { rc-web@69: //Adjust args appropriately rc-web@69: callback = deps; rc-web@69: deps = name; rc-web@69: name = null; rc-web@69: } rc-web@69: rc-web@69: //This module may not have dependencies rc-web@69: if (!isArray(deps)) { rc-web@69: callback = deps; rc-web@69: deps = null; rc-web@69: } rc-web@69: rc-web@69: //If no name, and callback is a function, then figure out if it a rc-web@69: //CommonJS thing with dependencies. rc-web@69: if (!deps && isFunction(callback)) { rc-web@69: deps = []; rc-web@69: //Remove comments from the callback string, rc-web@69: //look for require calls, and pull them into the dependencies, rc-web@69: //but only if there are function args. rc-web@69: if (callback.length) { rc-web@69: callback rc-web@69: .toString() rc-web@69: .replace(commentRegExp, '') rc-web@69: .replace(cjsRequireRegExp, function (match, dep) { rc-web@69: deps.push(dep); rc-web@69: }); rc-web@69: rc-web@69: //May be a CommonJS thing even without require calls, but still rc-web@69: //could use exports, and module. Avoid doing exports and module rc-web@69: //work though if it just needs require. rc-web@69: //REQUIRES the function to expect the CommonJS variables in the rc-web@69: //order listed below. rc-web@69: deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps); rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //If in IE 6-8 and hit an anonymous define() call, do the interactive rc-web@69: //work. rc-web@69: if (useInteractive) { rc-web@69: node = currentlyAddingScript || getInteractiveScript(); rc-web@69: if (node) { rc-web@69: if (!name) { rc-web@69: name = node.getAttribute('data-requiremodule'); rc-web@69: } rc-web@69: context = contexts[node.getAttribute('data-requirecontext')]; rc-web@69: } rc-web@69: } rc-web@69: rc-web@69: //Always save off evaluating the def call until the script onload handler. rc-web@69: //This allows multiple modules to be in a file without prematurely rc-web@69: //tracing dependencies, and allows for anonymous module support, rc-web@69: //where the module name is not known until the script onload event rc-web@69: //occurs. If no context, use the global queue, and get it processed rc-web@69: //in the onscript load callback. rc-web@69: (context ? context.defQueue : globalDefQueue).push([name, deps, callback]); rc-web@69: }; rc-web@69: rc-web@69: define.amd = { rc-web@69: jQuery: true rc-web@69: }; rc-web@69: rc-web@69: rc-web@69: /** rc-web@69: * Executes the text. Normally just uses eval, but can be modified rc-web@69: * to use a better, environment-specific call. Only used for transpiling rc-web@69: * loader plugins, not for plain JS modules. rc-web@69: * @param {String} text the text to execute/evaluate. rc-web@69: */ rc-web@69: req.exec = function (text) { rc-web@69: /*jslint evil: true */ rc-web@69: return eval(text); rc-web@69: }; rc-web@69: rc-web@69: //Set up with config info. rc-web@69: req(cfg); rc-web@69: }(this));