From abd94a7f5a50f43c797a11b53549ae48fff667c3 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 10 Oct 2016 03:43:44 +0200 Subject: add node_modules to address #4364 --- node_modules/systemjs/dist/system.src.js | 5153 ++++++++++++++++++++++++++++++ 1 file changed, 5153 insertions(+) create mode 100644 node_modules/systemjs/dist/system.src.js (limited to 'node_modules/systemjs/dist/system.src.js') diff --git a/node_modules/systemjs/dist/system.src.js b/node_modules/systemjs/dist/system.src.js new file mode 100644 index 000000000..3734388df --- /dev/null +++ b/node_modules/systemjs/dist/system.src.js @@ -0,0 +1,5153 @@ +/* + * SystemJS v0.19.39 + */ +(function() { +function bootstrap() {// from https://gist.github.com/Yaffle/1088850 +(function(global) { +function URLPolyfill(url, baseURL) { + if (typeof url != 'string') + throw new TypeError('URL must be a string'); + var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/); + if (!m) + throw new RangeError('Invalid URL format'); + var protocol = m[1] || ""; + var username = m[2] || ""; + var password = m[3] || ""; + var host = m[4] || ""; + var hostname = m[5] || ""; + var port = m[6] || ""; + var pathname = m[7] || ""; + var search = m[8] || ""; + var hash = m[9] || ""; + if (baseURL !== undefined) { + var base = baseURL instanceof URLPolyfill ? baseURL : new URLPolyfill(baseURL); + var flag = !protocol && !host && !username; + if (flag && !pathname && !search) + search = base.search; + if (flag && pathname[0] !== "/") + pathname = (pathname ? (((base.host || base.username) && !base.pathname ? "/" : "") + base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + pathname) : base.pathname); + // dot segments removal + var output = []; + pathname.replace(/^(\.\.?(\/|$))+/, "") + .replace(/\/(\.(\/|$))+/g, "/") + .replace(/\/\.\.$/, "/../") + .replace(/\/?[^\/]*/g, function (p) { + if (p === "/..") + output.pop(); + else + output.push(p); + }); + pathname = output.join("").replace(/^\//, pathname[0] === "/" ? "/" : ""); + if (flag) { + port = base.port; + hostname = base.hostname; + host = base.host; + password = base.password; + username = base.username; + } + if (!protocol) + protocol = base.protocol; + } + + // convert URLs to use / always + pathname = pathname.replace(/\\/g, '/'); + + this.origin = host ? protocol + (protocol !== "" || host !== "" ? "//" : "") + host : ""; + this.href = protocol + (protocol && host || protocol == "file:" ? "//" : "") + (username !== "" ? username + (password !== "" ? ":" + password : "") + "@" : "") + host + pathname + search + hash; + this.protocol = protocol; + this.username = username; + this.password = password; + this.host = host; + this.hostname = hostname; + this.port = port; + this.pathname = pathname; + this.search = search; + this.hash = hash; +} +global.URLPolyfill = URLPolyfill; +})(typeof self != 'undefined' ? self : global);(function(__global) { + + var isWorker = typeof window == 'undefined' && typeof self != 'undefined' && typeof importScripts != 'undefined'; + var isBrowser = typeof window != 'undefined' && typeof document != 'undefined'; + var isWindows = typeof process != 'undefined' && typeof process.platform != 'undefined' && !!process.platform.match(/^win/); + + if (!__global.console) + __global.console = { assert: function() {} }; + + // IE8 support + var indexOf = Array.prototype.indexOf || function(item) { + for (var i = 0, thisLen = this.length; i < thisLen; i++) { + if (this[i] === item) { + return i; + } + } + return -1; + }; + + var defineProperty; + (function () { + try { + if (!!Object.defineProperty({}, 'a', {})) + defineProperty = Object.defineProperty; + } + catch (e) { + defineProperty = function(obj, prop, opt) { + try { + obj[prop] = opt.value || opt.get.call(obj); + } + catch(e) {} + } + } + })(); + + var errArgs = new Error(0, '_').fileName == '_'; + + function addToError(err, msg) { + // parse the stack removing loader code lines for simplification + if (!err.originalErr) { + var stack = ((err.message || err) + (err.stack ? '\n' + err.stack : '')).toString().split('\n'); + var newStack = []; + for (var i = 0; i < stack.length; i++) { + if (typeof $__curScript == 'undefined' || stack[i].indexOf($__curScript.src) == -1) + newStack.push(stack[i]); + } + } + + var newMsg = '(SystemJS) ' + (newStack ? newStack.join('\n\t') : err.message.substr(11)) + '\n\t' + msg; + + // Convert file:/// URLs to paths in Node + if (!isBrowser) + newMsg = newMsg.replace(isWindows ? /file:\/\/\//g : /file:\/\//g, ''); + + var newErr = errArgs ? new Error(newMsg, err.fileName, err.lineNumber) : new Error(newMsg); + + newErr.stack = newMsg; + + // track the original error + newErr.originalErr = err.originalErr || err; + + return newErr; + } + + function __eval(source, debugName, context) { + try { + new Function(source).call(context); + } + catch(e) { + throw addToError(e, 'Evaluating ' + debugName); + } + } + + var baseURI; + + // environent baseURI detection + if (typeof document != 'undefined' && document.getElementsByTagName) { + baseURI = document.baseURI; + + if (!baseURI) { + var bases = document.getElementsByTagName('base'); + baseURI = bases[0] && bases[0].href || window.location.href; + } + } + else if (typeof location != 'undefined') { + baseURI = __global.location.href; + } + + // sanitize out the hash and querystring + if (baseURI) { + baseURI = baseURI.split('#')[0].split('?')[0]; + baseURI = baseURI.substr(0, baseURI.lastIndexOf('/') + 1); + } + else if (typeof process != 'undefined' && process.cwd) { + baseURI = 'file://' + (isWindows ? '/' : '') + process.cwd() + '/'; + if (isWindows) + baseURI = baseURI.replace(/\\/g, '/'); + } + else { + throw new TypeError('No environment baseURI'); + } + + try { + var nativeURL = new __global.URL('test:///').protocol == 'test:'; + } + catch(e) {} + + var URL = nativeURL ? __global.URL : __global.URLPolyfill; + +/* +********************************************************************************************* + + Dynamic Module Loader Polyfill + + - Implemented exactly to the former 2014-08-24 ES6 Specification Draft Rev 27, Section 15 + http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts#august_24_2014_draft_rev_27 + + - Functions are commented with their spec numbers, with spec differences commented. + + - Spec bugs are commented in this code with links. + + - Abstract functions have been combined where possible, and their associated functions + commented. + + - Realm implementation is entirely omitted. + +********************************************************************************************* +*/ + +function Module() {} +// http://www.ecma-international.org/ecma-262/6.0/#sec-@@tostringtag +defineProperty(Module.prototype, 'toString', { + value: function() { + return 'Module'; + } +}); +function Loader(options) { + this._loader = { + loaderObj: this, + loads: [], + modules: {}, + importPromises: {}, + moduleRecords: {} + }; + + // 26.3.3.6 + defineProperty(this, 'global', { + get: function() { + return __global; + } + }); + + // 26.3.3.13 realm not implemented +} + +(function() { + +// Some Helpers + +// logs a linkset snapshot for debugging +/* function snapshot(loader) { + console.log('---Snapshot---'); + for (var i = 0; i < loader.loads.length; i++) { + var load = loader.loads[i]; + var linkSetLog = ' ' + load.name + ' (' + load.status + '): '; + + for (var j = 0; j < load.linkSets.length; j++) { + linkSetLog += '{' + logloads(load.linkSets[j].loads) + '} '; + } + console.log(linkSetLog); + } + console.log(''); +} +function logloads(loads) { + var log = ''; + for (var k = 0; k < loads.length; k++) + log += loads[k].name + (k != loads.length - 1 ? ' ' : ''); + return log; +} */ + + +/* function checkInvariants() { + // see https://bugs.ecmascript.org/show_bug.cgi?id=2603#c1 + + var loads = System._loader.loads; + var linkSets = []; + + for (var i = 0; i < loads.length; i++) { + var load = loads[i]; + console.assert(load.status == 'loading' || load.status == 'loaded', 'Each load is loading or loaded'); + + for (var j = 0; j < load.linkSets.length; j++) { + var linkSet = load.linkSets[j]; + + for (var k = 0; k < linkSet.loads.length; k++) + console.assert(loads.indexOf(linkSet.loads[k]) != -1, 'linkSet loads are a subset of loader loads'); + + if (linkSets.indexOf(linkSet) == -1) + linkSets.push(linkSet); + } + } + + for (var i = 0; i < loads.length; i++) { + var load = loads[i]; + for (var j = 0; j < linkSets.length; j++) { + var linkSet = linkSets[j]; + + if (linkSet.loads.indexOf(load) != -1) + console.assert(load.linkSets.indexOf(linkSet) != -1, 'linkSet contains load -> load contains linkSet'); + + if (load.linkSets.indexOf(linkSet) != -1) + console.assert(linkSet.loads.indexOf(load) != -1, 'load contains linkSet -> linkSet contains load'); + } + } + + for (var i = 0; i < linkSets.length; i++) { + var linkSet = linkSets[i]; + for (var j = 0; j < linkSet.loads.length; j++) { + var load = linkSet.loads[j]; + + for (var k = 0; k < load.dependencies.length; k++) { + var depName = load.dependencies[k].value; + var depLoad; + for (var l = 0; l < loads.length; l++) { + if (loads[l].name != depName) + continue; + depLoad = loads[l]; + break; + } + + // loading records are allowed not to have their dependencies yet + // if (load.status != 'loading') + // console.assert(depLoad, 'depLoad found'); + + // console.assert(linkSet.loads.indexOf(depLoad) != -1, 'linkset contains all dependencies'); + } + } + } +} */ + + // 15.2.3 - Runtime Semantics: Loader State + + // 15.2.3.11 + function createLoaderLoad(object) { + return { + // modules is an object for ES5 implementation + modules: {}, + loads: [], + loaderObj: object + }; + } + + // 15.2.3.2 Load Records and LoadRequest Objects + + var anonCnt = 0; + + // 15.2.3.2.1 + function createLoad(name) { + return { + status: 'loading', + name: name || '', + linkSets: [], + dependencies: [], + metadata: {} + }; + } + + // 15.2.3.2.2 createLoadRequestObject, absorbed into calling functions + + // 15.2.4 + + // 15.2.4.1 + function loadModule(loader, name, options) { + return new Promise(asyncStartLoadPartwayThrough({ + step: options.address ? 'fetch' : 'locate', + loader: loader, + moduleName: name, + // allow metadata for import https://bugs.ecmascript.org/show_bug.cgi?id=3091 + moduleMetadata: options && options.metadata || {}, + moduleSource: options.source, + moduleAddress: options.address + })); + } + + // 15.2.4.2 + function requestLoad(loader, request, refererName, refererAddress) { + // 15.2.4.2.1 CallNormalize + return new Promise(function(resolve, reject) { + resolve(loader.loaderObj.normalize(request, refererName, refererAddress)); + }) + // 15.2.4.2.2 GetOrCreateLoad + .then(function(name) { + var load; + if (loader.modules[name]) { + load = createLoad(name); + load.status = 'linked'; + // https://bugs.ecmascript.org/show_bug.cgi?id=2795 + load.module = loader.modules[name]; + return load; + } + + for (var i = 0, l = loader.loads.length; i < l; i++) { + load = loader.loads[i]; + if (load.name != name) + continue; + return load; + } + + load = createLoad(name); + loader.loads.push(load); + + proceedToLocate(loader, load); + + return load; + }); + } + + // 15.2.4.3 + function proceedToLocate(loader, load) { + proceedToFetch(loader, load, + Promise.resolve() + // 15.2.4.3.1 CallLocate + .then(function() { + return loader.loaderObj.locate({ name: load.name, metadata: load.metadata }); + }) + ); + } + + // 15.2.4.4 + function proceedToFetch(loader, load, p) { + proceedToTranslate(loader, load, + p + // 15.2.4.4.1 CallFetch + .then(function(address) { + // adjusted, see https://bugs.ecmascript.org/show_bug.cgi?id=2602 + if (load.status != 'loading') + return; + load.address = address; + + return loader.loaderObj.fetch({ name: load.name, metadata: load.metadata, address: address }); + }) + ); + } + + // 15.2.4.5 + function proceedToTranslate(loader, load, p) { + p + // 15.2.4.5.1 CallTranslate + .then(function(source) { + if (load.status != 'loading') + return; + + load.address = load.address || load.name; + + return Promise.resolve(loader.loaderObj.translate({ name: load.name, metadata: load.metadata, address: load.address, source: source })) + + // 15.2.4.5.2 CallInstantiate + .then(function(source) { + load.source = source; + return loader.loaderObj.instantiate({ name: load.name, metadata: load.metadata, address: load.address, source: source }); + }) + + // 15.2.4.5.3 InstantiateSucceeded + .then(function(instantiateResult) { + if (instantiateResult === undefined) + throw new TypeError('Declarative modules unsupported in the polyfill.'); + + if (typeof instantiateResult != 'object') + throw new TypeError('Invalid instantiate return value'); + + load.depsList = instantiateResult.deps || []; + load.execute = instantiateResult.execute; + }) + // 15.2.4.6 ProcessLoadDependencies + .then(function() { + load.dependencies = []; + var depsList = load.depsList; + + var loadPromises = []; + for (var i = 0, l = depsList.length; i < l; i++) (function(request, index) { + loadPromises.push( + requestLoad(loader, request, load.name, load.address) + + // 15.2.4.6.1 AddDependencyLoad (load is parentLoad) + .then(function(depLoad) { + + // adjusted from spec to maintain dependency order + // this is due to the System.register internal implementation needs + load.dependencies[index] = { + key: request, + value: depLoad.name + }; + + if (depLoad.status != 'linked') { + var linkSets = load.linkSets.concat([]); + for (var i = 0, l = linkSets.length; i < l; i++) + addLoadToLinkSet(linkSets[i], depLoad); + } + + // console.log('AddDependencyLoad ' + depLoad.name + ' for ' + load.name); + // snapshot(loader); + }) + ); + })(depsList[i], i); + + return Promise.all(loadPromises); + }) + + // 15.2.4.6.2 LoadSucceeded + .then(function() { + // console.log('LoadSucceeded ' + load.name); + // snapshot(loader); + + load.status = 'loaded'; + + var linkSets = load.linkSets.concat([]); + for (var i = 0, l = linkSets.length; i < l; i++) + updateLinkSetOnLoad(linkSets[i], load); + }); + }) + // 15.2.4.5.4 LoadFailed + ['catch'](function(exc) { + load.status = 'failed'; + load.exception = exc; + + var linkSets = load.linkSets.concat([]); + for (var i = 0, l = linkSets.length; i < l; i++) { + linkSetFailed(linkSets[i], load, exc); + } + + console.assert(load.linkSets.length == 0, 'linkSets not removed'); + }); + } + + // 15.2.4.7 PromiseOfStartLoadPartwayThrough absorbed into calling functions + + // 15.2.4.7.1 + function asyncStartLoadPartwayThrough(stepState) { + return function(resolve, reject) { + var loader = stepState.loader; + var name = stepState.moduleName; + var step = stepState.step; + + if (loader.modules[name]) + throw new TypeError('"' + name + '" already exists in the module table'); + + // adjusted to pick up existing loads + var existingLoad; + for (var i = 0, l = loader.loads.length; i < l; i++) { + if (loader.loads[i].name == name) { + existingLoad = loader.loads[i]; + + if (step == 'translate' && !existingLoad.source) { + existingLoad.address = stepState.moduleAddress; + proceedToTranslate(loader, existingLoad, Promise.resolve(stepState.moduleSource)); + } + + // a primary load -> use that existing linkset if it is for the direct load here + // otherwise create a new linkset unit + if (existingLoad.linkSets.length && existingLoad.linkSets[0].loads[0].name == existingLoad.name) + return existingLoad.linkSets[0].done.then(function() { + resolve(existingLoad); + }); + } + } + + var load = existingLoad || createLoad(name); + + load.metadata = stepState.moduleMetadata; + + var linkSet = createLinkSet(loader, load); + + loader.loads.push(load); + + resolve(linkSet.done); + + if (step == 'locate') + proceedToLocate(loader, load); + + else if (step == 'fetch') + proceedToFetch(loader, load, Promise.resolve(stepState.moduleAddress)); + + else { + console.assert(step == 'translate', 'translate step'); + load.address = stepState.moduleAddress; + proceedToTranslate(loader, load, Promise.resolve(stepState.moduleSource)); + } + } + } + + // Declarative linking functions run through alternative implementation: + // 15.2.5.1.1 CreateModuleLinkageRecord not implemented + // 15.2.5.1.2 LookupExport not implemented + // 15.2.5.1.3 LookupModuleDependency not implemented + + // 15.2.5.2.1 + function createLinkSet(loader, startingLoad) { + var linkSet = { + loader: loader, + loads: [], + startingLoad: startingLoad, // added see spec bug https://bugs.ecmascript.org/show_bug.cgi?id=2995 + loadingCount: 0 + }; + linkSet.done = new Promise(function(resolve, reject) { + linkSet.resolve = resolve; + linkSet.reject = reject; + }); + addLoadToLinkSet(linkSet, startingLoad); + return linkSet; + } + // 15.2.5.2.2 + function addLoadToLinkSet(linkSet, load) { + if (load.status == 'failed') + return; + + for (var i = 0, l = linkSet.loads.length; i < l; i++) + if (linkSet.loads[i] == load) + return; + + linkSet.loads.push(load); + load.linkSets.push(linkSet); + + // adjustment, see https://bugs.ecmascript.org/show_bug.cgi?id=2603 + if (load.status != 'loaded') { + linkSet.loadingCount++; + } + + var loader = linkSet.loader; + + for (var i = 0, l = load.dependencies.length; i < l; i++) { + if (!load.dependencies[i]) + continue; + + var name = load.dependencies[i].value; + + if (loader.modules[name]) + continue; + + for (var j = 0, d = loader.loads.length; j < d; j++) { + if (loader.loads[j].name != name) + continue; + + addLoadToLinkSet(linkSet, loader.loads[j]); + break; + } + } + // console.log('add to linkset ' + load.name); + // snapshot(linkSet.loader); + } + + // linking errors can be generic or load-specific + // this is necessary for debugging info + function doLink(linkSet) { + var error = false; + try { + link(linkSet, function(load, exc) { + linkSetFailed(linkSet, load, exc); + error = true; + }); + } + catch(e) { + linkSetFailed(linkSet, null, e); + error = true; + } + return error; + } + + // 15.2.5.2.3 + function updateLinkSetOnLoad(linkSet, load) { + // console.log('update linkset on load ' + load.name); + // snapshot(linkSet.loader); + + console.assert(load.status == 'loaded' || load.status == 'linked', 'loaded or linked'); + + linkSet.loadingCount--; + + if (linkSet.loadingCount > 0) + return; + + // adjusted for spec bug https://bugs.ecmascript.org/show_bug.cgi?id=2995 + var startingLoad = linkSet.startingLoad; + + // non-executing link variation for loader tracing + // on the server. Not in spec. + /***/ + if (linkSet.loader.loaderObj.execute === false) { + var loads = [].concat(linkSet.loads); + for (var i = 0, l = loads.length; i < l; i++) { + var load = loads[i]; + load.module = { + name: load.name, + module: _newModule({}), + evaluated: true + }; + load.status = 'linked'; + finishLoad(linkSet.loader, load); + } + return linkSet.resolve(startingLoad); + } + /***/ + + var abrupt = doLink(linkSet); + + if (abrupt) + return; + + console.assert(linkSet.loads.length == 0, 'loads cleared'); + + linkSet.resolve(startingLoad); + } + + // 15.2.5.2.4 + function linkSetFailed(linkSet, load, exc) { + var loader = linkSet.loader; + var requests; + + checkError: + if (load) { + if (linkSet.loads[0].name == load.name) { + exc = addToError(exc, 'Error loading ' + load.name); + } + else { + for (var i = 0; i < linkSet.loads.length; i++) { + var pLoad = linkSet.loads[i]; + for (var j = 0; j < pLoad.dependencies.length; j++) { + var dep = pLoad.dependencies[j]; + if (dep.value == load.name) { + exc = addToError(exc, 'Error loading ' + load.name + ' as "' + dep.key + '" from ' + pLoad.name); + break checkError; + } + } + } + exc = addToError(exc, 'Error loading ' + load.name + ' from ' + linkSet.loads[0].name); + } + } + else { + exc = addToError(exc, 'Error linking ' + linkSet.loads[0].name); + } + + + var loads = linkSet.loads.concat([]); + for (var i = 0, l = loads.length; i < l; i++) { + var load = loads[i]; + + // store all failed load records + loader.loaderObj.failed = loader.loaderObj.failed || []; + if (indexOf.call(loader.loaderObj.failed, load) == -1) + loader.loaderObj.failed.push(load); + + var linkIndex = indexOf.call(load.linkSets, linkSet); + console.assert(linkIndex != -1, 'link not present'); + load.linkSets.splice(linkIndex, 1); + if (load.linkSets.length == 0) { + var globalLoadsIndex = indexOf.call(linkSet.loader.loads, load); + if (globalLoadsIndex != -1) + linkSet.loader.loads.splice(globalLoadsIndex, 1); + } + } + linkSet.reject(exc); + } + + // 15.2.5.2.5 + function finishLoad(loader, load) { + // add to global trace if tracing + if (loader.loaderObj.trace) { + if (!loader.loaderObj.loads) + loader.loaderObj.loads = {}; + var depMap = {}; + load.dependencies.forEach(function(dep) { + depMap[dep.key] = dep.value; + }); + loader.loaderObj.loads[load.name] = { + name: load.name, + deps: load.dependencies.map(function(dep){ return dep.key }), + depMap: depMap, + address: load.address, + metadata: load.metadata, + source: load.source + }; + } + // if not anonymous, add to the module table + if (load.name) { + console.assert(!loader.modules[load.name] || loader.modules[load.name].module === load.module.module, 'load not in module table'); + loader.modules[load.name] = load.module; + } + var loadIndex = indexOf.call(loader.loads, load); + if (loadIndex != -1) + loader.loads.splice(loadIndex, 1); + for (var i = 0, l = load.linkSets.length; i < l; i++) { + loadIndex = indexOf.call(load.linkSets[i].loads, load); + if (loadIndex != -1) + load.linkSets[i].loads.splice(loadIndex, 1); + } + load.linkSets.splice(0, load.linkSets.length); + } + + function doDynamicExecute(linkSet, load, linkError) { + try { + var module = load.execute(); + } + catch(e) { + linkError(load, e); + return; + } + if (!module || !(module instanceof Module)) + linkError(load, new TypeError('Execution must define a Module instance')); + else + return module; + } + + // 26.3 Loader + + // 26.3.1.1 + // defined at top + + // importPromises adds ability to import a module twice without error - https://bugs.ecmascript.org/show_bug.cgi?id=2601 + function createImportPromise(loader, name, promise) { + var importPromises = loader._loader.importPromises; + return importPromises[name] = promise.then(function(m) { + importPromises[name] = undefined; + return m; + }, function(e) { + importPromises[name] = undefined; + throw e; + }); + } + + Loader.prototype = { + // 26.3.3.1 + constructor: Loader, + // 26.3.3.2 + define: function(name, source, options) { + // check if already defined + if (this._loader.importPromises[name]) + throw new TypeError('Module is already loading.'); + return createImportPromise(this, name, new Promise(asyncStartLoadPartwayThrough({ + step: 'translate', + loader: this._loader, + moduleName: name, + moduleMetadata: options && options.metadata || {}, + moduleSource: source, + moduleAddress: options && options.address + }))); + }, + // 26.3.3.3 + 'delete': function(name) { + var loader = this._loader; + delete loader.importPromises[name]; + delete loader.moduleRecords[name]; + return loader.modules[name] ? delete loader.modules[name] : false; + }, + // 26.3.3.4 entries not implemented + // 26.3.3.5 + get: function(key) { + if (!this._loader.modules[key]) + return; + return this._loader.modules[key].module; + }, + // 26.3.3.7 + has: function(name) { + return !!this._loader.modules[name]; + }, + // 26.3.3.8 + 'import': function(name, parentName, parentAddress) { + if (typeof parentName == 'object') + parentName = parentName.name; + + // run normalize first + var loaderObj = this; + + // added, see https://bugs.ecmascript.org/show_bug.cgi?id=2659 + return Promise.resolve(loaderObj.normalize(name, parentName)) + .then(function(name) { + var loader = loaderObj._loader; + + if (loader.modules[name]) + return loader.modules[name].module; + + return loader.importPromises[name] || createImportPromise(loaderObj, name, + loadModule(loader, name, {}) + .then(function(load) { + delete loader.importPromises[name]; + return load.module.module; + })); + }); + }, + // 26.3.3.9 keys not implemented + // 26.3.3.10 + load: function(name) { + var loader = this._loader; + if (loader.modules[name]) + return Promise.resolve(); + return loader.importPromises[name] || createImportPromise(this, name, new Promise(asyncStartLoadPartwayThrough({ + step: 'locate', + loader: loader, + moduleName: name, + moduleMetadata: {}, + moduleSource: undefined, + moduleAddress: undefined + })) + .then(function() { + delete loader.importPromises[name]; + })); + }, + // 26.3.3.11 + module: function(source, options) { + var load = createLoad(); + load.address = options && options.address; + var linkSet = createLinkSet(this._loader, load); + var sourcePromise = Promise.resolve(source); + var loader = this._loader; + var p = linkSet.done.then(function() { + return load.module.module; + }); + proceedToTranslate(loader, load, sourcePromise); + return p; + }, + // 26.3.3.12 + newModule: function (obj) { + if (typeof obj != 'object') + throw new TypeError('Expected object'); + + var m = new Module(); + + var pNames = []; + if (Object.getOwnPropertyNames && obj != null) + pNames = Object.getOwnPropertyNames(obj); + else + for (var key in obj) + pNames.push(key); + + for (var i = 0; i < pNames.length; i++) (function(key) { + defineProperty(m, key, { + configurable: false, + enumerable: true, + get: function () { + return obj[key]; + }, + set: function() { + throw new Error('Module exports cannot be changed externally.'); + } + }); + })(pNames[i]); + + if (Object.freeze) + Object.freeze(m); + + return m; + }, + // 26.3.3.14 + set: function(name, module) { + if (!(module instanceof Module)) + throw new TypeError('Loader.set(' + name + ', module) must be a module'); + this._loader.modules[name] = { + module: module + }; + }, + // 26.3.3.15 values not implemented + // 26.3.3.16 @@iterator not implemented + // 26.3.3.17 @@toStringTag not implemented + + // 26.3.3.18.1 + normalize: function(name, referrerName, referrerAddress) {}, + // 26.3.3.18.2 + locate: function(load) { + return load.name; + }, + // 26.3.3.18.3 + fetch: function(load) { + }, + // 26.3.3.18.4 + translate: function(load) { + return load.source; + }, + // 26.3.3.18.5 + instantiate: function(load) { + } + }; + + var _newModule = Loader.prototype.newModule; + +/* + * ES6 Module Declarative Linking Code + */ + function link(linkSet, linkError) { + + var loader = linkSet.loader; + + if (!linkSet.loads.length) + return; + + var loads = linkSet.loads.concat([]); + + for (var i = 0; i < loads.length; i++) { + var load = loads[i]; + + var module = doDynamicExecute(linkSet, load, linkError); + if (!module) + return; + load.module = { + name: load.name, + module: module + }; + load.status = 'linked'; + + finishLoad(loader, load); + } + } + +})(); + +var System; + + var fetchTextFromURL; + if (typeof XMLHttpRequest != 'undefined') { + fetchTextFromURL = function(url, authorization, fulfill, reject) { + var xhr = new XMLHttpRequest(); + var sameDomain = true; + var doTimeout = false; + if (!('withCredentials' in xhr)) { + // check if same domain + var domainCheck = /^(\w+:)?\/\/([^\/]+)/.exec(url); + if (domainCheck) { + sameDomain = domainCheck[2] === window.location.host; + if (domainCheck[1]) + sameDomain &= domainCheck[1] === window.location.protocol; + } + } + if (!sameDomain && typeof XDomainRequest != 'undefined') { + xhr = new XDomainRequest(); + xhr.onload = load; + xhr.onerror = error; + xhr.ontimeout = error; + xhr.onprogress = function() {}; + xhr.timeout = 0; + doTimeout = true; + } + function load() { + fulfill(xhr.responseText); + } + function error() { + reject(new Error('XHR error' + (xhr.status ? ' (' + xhr.status + (xhr.statusText ? ' ' + xhr.statusText : '') + ')' : '') + ' loading ' + url)); + } + + xhr.onreadystatechange = function () { + if (xhr.readyState === 4) { + // in Chrome on file:/// URLs, status is 0 + if (xhr.status == 0) { + if (xhr.responseText) { + load(); + } + else { + // when responseText is empty, wait for load or error event + // to inform if it is a 404 or empty file + xhr.addEventListener('error', error); + xhr.addEventListener('load', load); + } + } + else if (xhr.status === 200) { + load(); + } + else { + error(); + } + } + }; + xhr.open("GET", url, true); + + if (xhr.setRequestHeader) { + xhr.setRequestHeader('Accept', 'application/x-es-module, */*'); + // can set "authorization: true" to enable withCredentials only + if (authorization) { + if (typeof authorization == 'string') + xhr.setRequestHeader('Authorization', authorization); + xhr.withCredentials = true; + } + } + + if (doTimeout) { + setTimeout(function() { + xhr.send(); + }, 0); + } else { + xhr.send(null); + } + }; + } + else if (typeof require != 'undefined' && typeof process != 'undefined') { + var fs; + fetchTextFromURL = function(url, authorization, fulfill, reject) { + if (url.substr(0, 8) != 'file:///') + throw new Error('Unable to fetch "' + url + '". Only file URLs of the form file:/// allowed running in Node.'); + fs = fs || require('fs'); + if (isWindows) + url = url.replace(/\//g, '\\').substr(8); + else + url = url.substr(7); + return fs.readFile(url, function(err, data) { + if (err) { + return reject(err); + } + else { + // Strip Byte Order Mark out if it's the leading char + var dataString = data + ''; + if (dataString[0] === '\ufeff') + dataString = dataString.substr(1); + + fulfill(dataString); + } + }); + }; + } + else if (typeof self != 'undefined' && typeof self.fetch != 'undefined') { + fetchTextFromURL = function(url, authorization, fulfill, reject) { + var opts = { + headers: {'Accept': 'application/x-es-module, */*'} + }; + + if (authorization) { + if (typeof authorization == 'string') + opts.headers['Authorization'] = authorization; + opts.credentials = 'include'; + } + + fetch(url, opts) + .then(function (r) { + if (r.ok) { + return r.text(); + } else { + throw new Error('Fetch error: ' + r.status + ' ' + r.statusText); + } + }) + .then(fulfill, reject); + } + } + else { + throw new TypeError('No environment fetch API available.'); + } +/* + * Traceur, Babel and TypeScript transpile hook for Loader + */ +var transpile = (function() { + + // use Traceur by default + Loader.prototype.transpiler = 'traceur'; + + function transpile(load) { + var self = this; + + return Promise.resolve(__global[self.transpiler == 'typescript' ? 'ts' : self.transpiler] + || (self.pluginLoader || self)['import'](self.transpiler)) + .then(function(transpiler) { + if (transpiler.__useDefault) + transpiler = transpiler['default']; + + var transpileFunction; + if (transpiler.Compiler) + transpileFunction = traceurTranspile; + else if (transpiler.createLanguageService) + transpileFunction = typescriptTranspile; + else + transpileFunction = babelTranspile; + + // note __moduleName will be part of the transformer meta in future when we have the spec for this + return '(function(__moduleName){' + transpileFunction.call(self, load, transpiler) + '\n})("' + load.name + '");\n//# sourceURL=' + load.address + '!transpiled'; + }); + }; + + function traceurTranspile(load, traceur) { + var options = this.traceurOptions || {}; + options.modules = 'instantiate'; + options.script = false; + if (options.sourceMaps === undefined) + options.sourceMaps = 'inline'; + options.filename = load.address; + options.inputSourceMap = load.metadata.sourceMap; + options.moduleName = false; + + var compiler = new traceur.Compiler(options); + + return doTraceurCompile(load.source, compiler, options.filename); + } + function doTraceurCompile(source, compiler, filename) { + try { + return compiler.compile(source, filename); + } + catch(e) { + // on older versions of traceur (<0.9.3), an array of errors is thrown + // rather than a single error. + if (e.length) { + throw e[0]; + } + throw e; + } + } + + function babelTranspile(load, babel) { + var options = this.babelOptions || {}; + options.modules = 'system'; + if (options.sourceMap === undefined) + options.sourceMap = 'inline'; + options.inputSourceMap = load.metadata.sourceMap; + options.filename = load.address; + options.code = true; + options.ast = false; + + return babel.transform(load.source, options).code; + } + + function typescriptTranspile(load, ts) { + var options = this.typescriptOptions || {}; + options.target = options.target || ts.ScriptTarget.ES5; + if (options.sourceMap === undefined) + options.sourceMap = true; + if (options.sourceMap && options.inlineSourceMap !== false) + options.inlineSourceMap = true; + + options.module = ts.ModuleKind.System; + + return ts.transpile(load.source, options, load.address); + } + + return transpile; +})(); +// SystemJS Loader Class and Extension helpers +function SystemJSLoader() { + Loader.call(this); + + this.paths = {}; + this._loader.paths = {}; + + systemJSConstructor.call(this); +} + +// inline Object.create-style class extension +function SystemProto() {}; +SystemProto.prototype = Loader.prototype; +SystemJSLoader.prototype = new SystemProto(); +SystemJSLoader.prototype.constructor = SystemJSLoader; + +var systemJSConstructor; + +function hook(name, hook) { + SystemJSLoader.prototype[name] = hook(SystemJSLoader.prototype[name] || function() {}); +} +function hookConstructor(hook) { + systemJSConstructor = hook(systemJSConstructor || function() {}); +} + + +var absURLRegEx = /^[^\/]+:\/\//; +function isAbsolute(name) { + return name.match(absURLRegEx); +} +function isRel(name) { + return (name[0] == '.' && (!name[1] || name[1] == '/' || name[1] == '.')) || name[0] == '/'; +} +function isPlain(name) { + return !isRel(name) && !isAbsolute(name); +} + +var baseURIObj = new URL(baseURI); + +function urlResolve(name, parent) { + // url resolution shortpaths + if (name[0] == '.') { + // dot-relative url normalization + if (name[1] == '/' && name[2] != '.') + return (parent && parent.substr(0, parent.lastIndexOf('/') + 1) || baseURI) + name.substr(2); + } + else if (name[0] != '/' && name.indexOf(':') == -1) { + // plain parent normalization + return (parent && parent.substr(0, parent.lastIndexOf('/') + 1) || baseURI) + name; + } + + return new URL(name, parent && parent.replace(/#/g, '%05') || baseURIObj).href.replace(/%05/g, '#'); +} + +// NB no specification provided for System.paths, used ideas discussed in https://github.com/jorendorff/js-loaders/issues/25 +function applyPaths(loader, name) { + // most specific (most number of slashes in path) match wins + var pathMatch = '', wildcard, maxWildcardPrefixLen = 0; + + var paths = loader.paths; + var pathsCache = loader._loader.paths; + + // check to see if we have a paths entry + for (var p in paths) { + if (paths.hasOwnProperty && !paths.hasOwnProperty(p)) + continue; + + // paths sanitization + var path = paths[p]; + if (path !== pathsCache[p]) + path = paths[p] = pathsCache[p] = urlResolve(paths[p], isRel(paths[p]) ? baseURI : loader.baseURL); + + // exact path match + if (p.indexOf('*') === -1) { + if (name == p) + return paths[p]; + + // support trailing / in paths rules + else if (name.substr(0, p.length - 1) == p.substr(0, p.length - 1) && (name.length < p.length || name[p.length - 1] == p[p.length - 1]) && (paths[p][paths[p].length - 1] == '/' || paths[p] == '')) { + return paths[p].substr(0, paths[p].length - 1) + (name.length > p.length ? (paths[p] && '/' || '') + name.substr(p.length) : ''); + } + } + // wildcard path match + else { + var pathParts = p.split('*'); + if (pathParts.length > 2) + throw new TypeError('Only one wildcard in a path is permitted'); + + var wildcardPrefixLen = pathParts[0].length; + if (wildcardPrefixLen >= maxWildcardPrefixLen && + name.substr(0, pathParts[0].length) == pathParts[0] && + name.substr(name.length - pathParts[1].length) == pathParts[1]) { + maxWildcardPrefixLen = wildcardPrefixLen; + pathMatch = p; + wildcard = name.substr(pathParts[0].length, name.length - pathParts[1].length - pathParts[0].length); + } + } + } + + var outPath = paths[pathMatch]; + if (typeof wildcard == 'string') + outPath = outPath.replace('*', wildcard); + + return outPath; +} + +function dedupe(deps) { + var newDeps = []; + for (var i = 0, l = deps.length; i < l; i++) + if (indexOf.call(newDeps, deps[i]) == -1) + newDeps.push(deps[i]) + return newDeps; +} + +function group(deps) { + var names = []; + var indices = []; + for (var i = 0, l = deps.length; i < l; i++) { + var index = indexOf.call(names, deps[i]); + if (index === -1) { + names.push(deps[i]); + indices.push([i]); + } + else { + indices[index].push(i); + } + } + return { names: names, indices: indices }; +} + +var getOwnPropertyDescriptor = true; +try { + Object.getOwnPropertyDescriptor({ a: 0 }, 'a'); +} +catch(e) { + getOwnPropertyDescriptor = false; +} + +// converts any module.exports object into an object ready for SystemJS.newModule +function getESModule(exports) { + var esModule = {}; + // don't trigger getters/setters in environments that support them + if ((typeof exports == 'object' || typeof exports == 'function') && exports !== __global) { + if (getOwnPropertyDescriptor) { + for (var p in exports) { + // The default property is copied to esModule later on + if (p === 'default') + continue; + defineOrCopyProperty(esModule, exports, p); + } + } + else { + extend(esModule, exports); + } + } + esModule['default'] = exports; + defineProperty(esModule, '__useDefault', { + value: true + }); + return esModule; +} + +function defineOrCopyProperty(targetObj, sourceObj, propName) { + try { + var d; + if (d = Object.getOwnPropertyDescriptor(sourceObj, propName)) + defineProperty(targetObj, propName, d); + } + catch (ex) { + // Object.getOwnPropertyDescriptor threw an exception, fall back to normal set property + // we dont need hasOwnProperty here because getOwnPropertyDescriptor would have returned undefined above + targetObj[propName] = sourceObj[propName]; + return false; + } +} + +function extend(a, b, prepend) { + var hasOwnProperty = b && b.hasOwnProperty; + for (var p in b) { + if (hasOwnProperty && !b.hasOwnProperty(p)) + continue; + if (!prepend || !(p in a)) + a[p] = b[p]; + } + return a; +} + +// meta first-level extends where: +// array + array appends +// object + object extends +// other properties replace +function extendMeta(a, b, prepend) { + var hasOwnProperty = b && b.hasOwnProperty; + for (var p in b) { + if (hasOwnProperty && !b.hasOwnProperty(p)) + continue; + var val = b[p]; + if (!(p in a)) + a[p] = val; + else if (val instanceof Array && a[p] instanceof Array) + a[p] = [].concat(prepend ? val : a[p]).concat(prepend ? a[p] : val); + else if (typeof val == 'object' && val !== null && typeof a[p] == 'object') + a[p] = extend(extend({}, a[p]), val, prepend); + else if (!prepend) + a[p] = val; + } +} + +function extendPkgConfig(pkgCfgA, pkgCfgB, pkgName, loader, warnInvalidProperties) { + for (var prop in pkgCfgB) { + if (indexOf.call(['main', 'format', 'defaultExtension', 'basePath'], prop) != -1) { + pkgCfgA[prop] = pkgCfgB[prop]; + } + else if (prop == 'map') { + extend(pkgCfgA.map = pkgCfgA.map || {}, pkgCfgB.map); + } + else if (prop == 'meta') { + extend(pkgCfgA.meta = pkgCfgA.meta || {}, pkgCfgB.meta); + } + else if (prop == 'depCache') { + for (var d in pkgCfgB.depCache) { + var dNormalized; + + if (d.substr(0, 2) == './') + dNormalized = pkgName + '/' + d.substr(2); + else + dNormalized = coreResolve.call(loader, d); + loader.depCache[dNormalized] = (loader.depCache[dNormalized] || []).concat(pkgCfgB.depCache[d]); + } + } + else if (warnInvalidProperties && indexOf.call(['browserConfig', 'nodeConfig', 'devConfig', 'productionConfig'], prop) == -1 && + (!pkgCfgB.hasOwnProperty || pkgCfgB.hasOwnProperty(prop))) { + warn.call(loader, '"' + prop + '" is not a valid package configuration option in package ' + pkgName); + } + } +} + +// deeply-merge (to first level) config with any existing package config +function setPkgConfig(loader, pkgName, cfg, prependConfig) { + var pkg; + + // first package is config by reference for fast path, cloned after that + if (!loader.packages[pkgName]) { + pkg = loader.packages[pkgName] = cfg; + } + else { + var basePkg = loader.packages[pkgName]; + pkg = loader.packages[pkgName] = {}; + + extendPkgConfig(pkg, prependConfig ? cfg : basePkg, pkgName, loader, prependConfig); + extendPkgConfig(pkg, prependConfig ? basePkg : cfg, pkgName, loader, !prependConfig); + } + + // main object becomes main map + if (typeof pkg.main == 'object') { + pkg.map = pkg.map || {}; + pkg.map['./@main'] = pkg.main; + pkg.main['default'] = pkg.main['default'] || './'; + pkg.main = '@main'; + } + + return pkg; +} + +function warn(msg) { + if (this.warnings && typeof console != 'undefined' && console.warn) + console.warn(msg); +} +// we define a __exec for globally-scoped execution +// used by module format implementations +var __exec; + +(function() { + + var hasBuffer = typeof Buffer != 'undefined'; + try { + if (hasBuffer && new Buffer('a').toString('base64') != 'YQ==') + hasBuffer = false; + } + catch(e) { + hasBuffer = false; + } + + var sourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,'; + function inlineSourceMap(sourceMapString) { + if (hasBuffer) + return sourceMapPrefix + new Buffer(sourceMapString).toString('base64'); + else if (typeof btoa != 'undefined') + return sourceMapPrefix + btoa(unescape(encodeURIComponent(sourceMapString))); + else + return ''; + } + + function getSource(load, wrap) { + var lastLineIndex = load.source.lastIndexOf('\n'); + + // wrap ES formats with a System closure for System global encapsulation + if (load.metadata.format == 'global') + wrap = false; + + var sourceMap = load.metadata.sourceMap; + if (sourceMap) { + if (typeof sourceMap != 'object') + throw new TypeError('load.metadata.sourceMap must be set to an object.'); + + sourceMap = JSON.stringify(sourceMap); + } + + return (wrap ? '(function(System, SystemJS) {' : '') + load.source + (wrap ? '\n})(System, System);' : '') + // adds the sourceURL comment if not already present + + (load.source.substr(lastLineIndex, 15) != '\n//# sourceURL=' + ? '\n//# sourceURL=' + load.address + (sourceMap ? '!transpiled' : '') : '') + // add sourceMappingURL if load.metadata.sourceMap is set + + (sourceMap && inlineSourceMap(sourceMap) || ''); + } + + var curLoad; + + // System.register, System.registerDynamic, AMD define pipeline + // if currently evalling code here, immediately reduce the registered entry against the load record + hook('pushRegister_', function() { + return function(register) { + if (!curLoad) + return false; + + this.reduceRegister_(curLoad, register); + return true; + }; + }); + + // System clobbering protection (mostly for Traceur) + var curSystem; + var callCounter = 0; + function preExec(loader, load) { + curLoad = load; + if (callCounter++ == 0) + curSystem = __global.System; + __global.System = __global.SystemJS = loader; + } + function postExec() { + if (--callCounter == 0) + __global.System = __global.SystemJS = curSystem; + curLoad = undefined; + } + + var useVm; + var vm; + __exec = function(load) { + if (!load.source) + return; + if ((load.metadata.integrity || load.metadata.nonce) && supportsScriptExec) + return scriptExec.call(this, load); + try { + preExec(this, load); + curLoad = load; + // global scoped eval for node (avoids require scope leak) + if (!vm && this._nodeRequire) { + vm = this._nodeRequire('vm'); + useVm = vm.runInThisContext("typeof System !== 'undefined' && System") === this; + } + if (useVm) + vm.runInThisContext(getSource(load, true), { filename: load.address + (load.metadata.sourceMap ? '!transpiled' : '') }); + else + (0, eval)(getSource(load, true)); + postExec(); + } + catch(e) { + postExec(); + throw addToError(e, 'Evaluating ' + load.address); + } + }; + + var supportsScriptExec = false; + if (isBrowser && typeof document != 'undefined' && document.getElementsByTagName) { + var scripts = document.getElementsByTagName('script'); + $__curScript = scripts[scripts.length - 1]; + + if (!(window.chrome && window.chrome.extension || navigator.userAgent.match(/^Node\.js/))) + supportsScriptExec = true; + } + + // script execution via injecting a script tag into the page + // this allows CSP integrity and nonce to be set for CSP environments + var head; + function scriptExec(load) { + if (!head) + head = document.head || document.body || document.documentElement; + + var script = document.createElement('script'); + script.text = getSource(load, false); + var onerror = window.onerror; + var e; + window.onerror = function(_e) { + e = addToError(_e, 'Evaluating ' + load.address); + if (onerror) + onerror.apply(this, arguments); + } + preExec(this, load); + + if (load.metadata.integrity) + script.setAttribute('integrity', load.metadata.integrity); + if (load.metadata.nonce) + script.setAttribute('nonce', load.metadata.nonce); + + head.appendChild(script); + head.removeChild(script); + postExec(); + window.onerror = onerror; + if (e) + throw e; + } + +})(); +function readMemberExpression(p, value) { + var pParts = p.split('.'); + while (pParts.length) + value = value[pParts.shift()]; + return value; +} + +function getMapMatch(map, name) { + var bestMatch, bestMatchLength = 0; + + for (var p in map) { + if (name.substr(0, p.length) == p && (name.length == p.length || name[p.length] == '/')) { + var curMatchLength = p.split('/').length; + if (curMatchLength <= bestMatchLength) + continue; + bestMatch = p; + bestMatchLength = curMatchLength; + } + } + + return bestMatch; +} + +function prepareBaseURL(loader) { + // ensure baseURl is fully normalized + if (this._loader.baseURL !== this.baseURL) { + if (this.baseURL[this.baseURL.length - 1] != '/') + this.baseURL += '/'; + + this._loader.baseURL = this.baseURL = new URL(this.baseURL, baseURIObj).href; + } +} + +var envModule; +function setProduction(isProduction, isBuilder) { + this.set('@system-env', envModule = this.newModule({ + browser: isBrowser, + node: !!this._nodeRequire, + production: !isBuilder && isProduction, + dev: isBuilder || !isProduction, + build: isBuilder, + 'default': true + })); +} + +hookConstructor(function(constructor) { + return function() { + constructor.call(this); + + // support baseURL + this.baseURL = baseURI; + + // support map and paths + this.map = {}; + + // make the location of the system.js script accessible + if (typeof $__curScript != 'undefined') + this.scriptSrc = $__curScript.src; + + // global behaviour flags + this.warnings = false; + this.defaultJSExtensions = false; + this.pluginFirst = false; + this.loaderErrorStack = false; + + // by default load ".json" files as json + // leading * meta doesn't need normalization + // NB add this in next breaking release + // this.meta['*.json'] = { format: 'json' }; + + // support the empty module, as a concept + this.set('@empty', this.newModule({})); + + setProduction.call(this, false, false); + }; +}); + +// include the node require since we're overriding it +if (typeof require != 'undefined' && typeof process != 'undefined' && !process.browser) + SystemJSLoader.prototype._nodeRequire = require; + +/* + Core SystemJS Normalization + + If a name is relative, we apply URL normalization to the page + If a name is an absolute URL, we leave it as-is + + Plain names (neither of the above) run through the map and paths + normalization phases. + + The paths normalization phase applies last (paths extension), which + defines the `decanonicalize` function and normalizes everything into + a URL. + */ + +var parentModuleContext; +function getNodeModule(name, baseURL) { + if (!isPlain(name)) + throw new Error('Node module ' + name + ' can\'t be loaded as it is not a package require.'); + + if (!parentModuleContext) { + var Module = this._nodeRequire('module'); + var base = baseURL.substr(isWindows ? 8 : 7); + parentModuleContext = new Module(base); + parentModuleContext.paths = Module._nodeModulePaths(base); + } + return parentModuleContext.require(name); +} + +function coreResolve(name, parentName) { + // standard URL resolution + if (isRel(name)) + return urlResolve(name, parentName); + else if (isAbsolute(name)) + return name; + + // plain names not starting with './', '://' and '/' go through custom resolution + var mapMatch = getMapMatch(this.map, name); + + if (mapMatch) { + name = this.map[mapMatch] + name.substr(mapMatch.length); + + if (isRel(name)) + return urlResolve(name); + else if (isAbsolute(name)) + return name; + } + + if (this.has(name)) + return name; + + // dynamically load node-core modules when requiring `@node/fs` for example + if (name.substr(0, 6) == '@node/') { + if (!this._nodeRequire) + throw new TypeError('Error loading ' + name + '. Can only load node core modules in Node.'); + if (this.builder) + this.set(name, this.newModule({})); + else + this.set(name, this.newModule(getESModule(getNodeModule.call(this, name.substr(6), this.baseURL)))); + return name; + } + + // prepare the baseURL to ensure it is normalized + prepareBaseURL.call(this); + + return applyPaths(this, name) || this.baseURL + name; +} + +hook('normalize', function(normalize) { + return function(name, parentName, skipExt) { + var resolved = coreResolve.call(this, name, parentName); + if (this.defaultJSExtensions && !skipExt && resolved.substr(resolved.length - 3, 3) != '.js' && !isPlain(resolved)) + resolved += '.js'; + return resolved; + }; +}); + +// percent encode just '#' in urls if using HTTP requests +var httpRequest = typeof XMLHttpRequest != 'undefined'; +hook('locate', function(locate) { + return function(load) { + return Promise.resolve(locate.call(this, load)) + .then(function(address) { + if (httpRequest) + return address.replace(/#/g, '%23'); + return address; + }); + }; +}); + +/* + * Fetch with authorization + */ +hook('fetch', function() { + return function(load) { + return new Promise(function(resolve, reject) { + fetchTextFromURL(load.address, load.metadata.authorization, resolve, reject); + }); + }; +}); + +/* + __useDefault + + When a module object looks like: + newModule( + __useDefault: true, + default: 'some-module' + }) + + Then importing that module provides the 'some-module' + result directly instead of the full module. + + Useful for eg module.exports = function() {} +*/ +hook('import', function(systemImport) { + return function(name, parentName, parentAddress) { + if (parentName && parentName.name) + warn.call(this, 'SystemJS.import(name, { name: parentName }) is deprecated for SystemJS.import(name, parentName), while importing ' + name + ' from ' + parentName.name); + return systemImport.call(this, name, parentName, parentAddress).then(function(module) { + return module.__useDefault ? module['default'] : module; + }); + }; +}); + +/* + * Allow format: 'detect' meta to enable format detection + */ +hook('translate', function(systemTranslate) { + return function(load) { + if (load.metadata.format == 'detect') + load.metadata.format = undefined; + return systemTranslate.apply(this, arguments); + }; +}); + + +/* + * JSON format support + * + * Supports loading JSON files as a module format itself + * + * Usage: + * + * SystemJS.config({ + * meta: { + * '*.json': { format: 'json' } + * } + * }); + * + * Module is returned as if written: + * + * export default {JSON} + * + * No named exports are provided + * + * Files ending in ".json" are treated as json automatically by SystemJS + */ +hook('instantiate', function(instantiate) { + return function(load) { + if (load.metadata.format == 'json' && !this.builder) { + var entry = load.metadata.entry = createEntry(); + entry.deps = []; + entry.execute = function() { + try { + return JSON.parse(load.source); + } + catch(e) { + throw new Error("Invalid JSON file " + load.name); + } + }; + } + }; +}) + +/* + Extend config merging one deep only + + loader.config({ + some: 'random', + config: 'here', + deep: { + config: { too: 'too' } + } + }); + + <=> + + loader.some = 'random'; + loader.config = 'here' + loader.deep = loader.deep || {}; + loader.deep.config = { too: 'too' }; + + + Normalizes meta and package configs allowing for: + + SystemJS.config({ + meta: { + './index.js': {} + } + }); + + To become + + SystemJS.meta['https://thissite.com/index.js'] = {}; + + For easy normalization canonicalization with latest URL support. + +*/ +function envSet(loader, cfg, envCallback) { + if (envModule.browser && cfg.browserConfig) + envCallback(cfg.browserConfig); + if (envModule.node && cfg.nodeConfig) + envCallback(cfg.nodeConfig); + if (envModule.dev && cfg.devConfig) + envCallback(cfg.devConfig); + if (envModule.build && cfg.buildConfig) + envCallback(cfg.buildConfig); + if (envModule.production && cfg.productionConfig) + envCallback(cfg.productionConfig); +} + +SystemJSLoader.prototype.getConfig = function(name) { + var cfg = {}; + var loader = this; + for (var p in loader) { + if (loader.hasOwnProperty && !loader.hasOwnProperty(p) || p in SystemJSLoader.prototype && p != 'transpiler') + continue; + if (indexOf.call(['_loader', 'amdDefine', 'amdRequire', 'defined', 'failed', 'version', 'loads'], p) == -1) + cfg[p] = loader[p]; + } + cfg.production = envModule.production; + return cfg; +}; + +var curCurScript; +SystemJSLoader.prototype.config = function(cfg, isEnvConfig) { + var loader = this; + + if ('loaderErrorStack' in cfg) { + curCurScript = $__curScript; + if (cfg.loaderErrorStack) + $__curScript = undefined; + else + $__curScript = curCurScript; + } + + if ('warnings' in cfg) + loader.warnings = cfg.warnings; + + // transpiler deprecation path + if (cfg.transpilerRuntime === false) + loader._loader.loadedTranspilerRuntime = true; + + if ('production' in cfg || 'build' in cfg) + setProduction.call(loader, !!cfg.production, !!(cfg.build || envModule && envModule.build)); + + if (!isEnvConfig) { + // if using nodeConfig / browserConfig / productionConfig, take baseURL from there + // these exceptions will be unnecessary when we can properly implement config queuings + var baseURL; + envSet(loader, cfg, function(cfg) { + baseURL = baseURL || cfg.baseURL; + }); + baseURL = baseURL || cfg.baseURL; + + // always configure baseURL first + if (baseURL) { + var hasConfig = false; + function checkHasConfig(obj) { + for (var p in obj) + if (obj.hasOwnProperty(p)) + return true; + } + if (checkHasConfig(loader.packages) || checkHasConfig(loader.meta) || checkHasConfig(loader.depCache) || checkHasConfig(loader.bundles) || checkHasConfig(loader.packageConfigPaths)) + throw new TypeError('Incorrect configuration order. The baseURL must be configured with the first SystemJS.config call.'); + + this.baseURL = baseURL; + prepareBaseURL.call(this); + } + + if (cfg.paths) + extend(loader.paths, cfg.paths); + + envSet(loader, cfg, function(cfg) { + if (cfg.paths) + extend(loader.paths, cfg.paths); + }); + + // warn on wildcard path deprecations + if (this.warnings) { + for (var p in loader.paths) + if (p.indexOf('*') != -1) + warn.call(loader, 'Paths configuration "' + p + '" -> "' + loader.paths[p] + '" uses wildcards which are being deprecated for simpler trailing "/" folder paths.'); + } + } + + if (cfg.defaultJSExtensions) { + loader.defaultJSExtensions = cfg.defaultJSExtensions; + warn.call(loader, 'The defaultJSExtensions configuration option is deprecated, use packages configuration instead.'); + } + + if (cfg.pluginFirst) + loader.pluginFirst = cfg.pluginFirst; + + if (cfg.map) { + var objMaps = ''; + for (var p in cfg.map) { + var v = cfg.map[p]; + + // object map backwards-compat into packages configuration + if (typeof v !== 'string') { + objMaps += (objMaps.length ? ', ' : '') + '"' + p + '"'; + + var defaultJSExtension = loader.defaultJSExtensions && p.substr(p.length - 3, 3) != '.js'; + var prop = loader.decanonicalize(p); + if (defaultJSExtension && prop.substr(prop.length - 3, 3) == '.js') + prop = prop.substr(0, prop.length - 3); + + // if a package main, revert it + var pkgMatch = ''; + for (var pkg in loader.packages) { + if (prop.substr(0, pkg.length) == pkg + && (!prop[pkg.length] || prop[pkg.length] == '/') + && pkgMatch.split('/').length < pkg.split('/').length) + pkgMatch = pkg; + } + if (pkgMatch && loader.packages[pkgMatch].main) + prop = prop.substr(0, prop.length - loader.packages[pkgMatch].main.length - 1); + + var pkg = loader.packages[prop] = loader.packages[prop] || {}; + pkg.map = v; + } + else { + loader.map[p] = v; + } + } + if (objMaps) + warn.call(loader, 'The map configuration for ' + objMaps + ' uses object submaps, which is deprecated in global map.\nUpdate this to use package contextual map with configs like SystemJS.config({ packages: { "' + p + '": { map: {...} } } }).'); + } + + if (cfg.packageConfigPaths) { + var packageConfigPaths = []; + for (var i = 0; i < cfg.packageConfigPaths.length; i++) { + var path = cfg.packageConfigPaths[i]; + var packageLength = Math.max(path.lastIndexOf('*') + 1, path.lastIndexOf('/')); + var normalized = coreResolve.call(loader, path.substr(0, packageLength)); + packageConfigPaths[i] = normalized + path.substr(packageLength); + } + loader.packageConfigPaths = packageConfigPaths; + } + + if (cfg.bundles) { + for (var p in cfg.bundles) { + var bundle = []; + for (var i = 0; i < cfg.bundles[p].length; i++) { + var defaultJSExtension = loader.defaultJSExtensions && cfg.bundles[p][i].substr(cfg.bundles[p][i].length - 3, 3) != '.js'; + var normalizedBundleDep = loader.decanonicalize(cfg.bundles[p][i]); + if (defaultJSExtension && normalizedBundleDep.substr(normalizedBundleDep.length - 3, 3) == '.js') + normalizedBundleDep = normalizedBundleDep.substr(0, normalizedBundleDep.length - 3); + bundle.push(normalizedBundleDep); + } + loader.bundles[p] = bundle; + } + } + + if (cfg.packages) { + for (var p in cfg.packages) { + if (p.match(/^([^\/]+:)?\/\/$/)) + throw new TypeError('"' + p + '" is not a valid package name.'); + + var prop = coreResolve.call(loader, p); + + // allow trailing slash in packages + if (prop[prop.length - 1] == '/') + prop = prop.substr(0, prop.length - 1); + + setPkgConfig(loader, prop, cfg.packages[p], false); + } + } + + for (var c in cfg) { + var v = cfg[c]; + + if (indexOf.call(['baseURL', 'map', 'packages', 'bundles', 'paths', 'warnings', 'packageConfigPaths', + 'loaderErrorStack', 'browserConfig', 'nodeConfig', 'devConfig', 'buildConfig', 'productionConfig'], c) != -1) + continue; + + if (typeof v != 'object' || v instanceof Array) { + loader[c] = v; + } + else { + loader[c] = loader[c] || {}; + + for (var p in v) { + // base-level wildcard meta does not normalize to retain catch-all quality + if (c == 'meta' && p[0] == '*') { + extend(loader[c][p] = loader[c][p] || {}, v[p]); + } + else if (c == 'meta') { + // meta can go through global map, with defaultJSExtensions adding + var resolved = coreResolve.call(loader, p); + if (loader.defaultJSExtensions && resolved.substr(resolved.length - 3, 3) != '.js' && !isPlain(resolved)) + resolved += '.js'; + extend(loader[c][resolved] = loader[c][resolved] || {}, v[p]); + } + else if (c == 'depCache') { + var defaultJSExtension = loader.defaultJSExtensions && p.substr(p.length - 3, 3) != '.js'; + var prop = loader.decanonicalize(p); + if (defaultJSExtension && prop.substr(prop.length - 3, 3) == '.js') + prop = prop.substr(0, prop.length - 3); + loader[c][prop] = [].concat(v[p]); + } + else { + loader[c][p] = v[p]; + } + } + } + } + + envSet(loader, cfg, function(cfg) { + loader.config(cfg, true); + }); +};/* + * Package Configuration Extension + * + * Example: + * + * SystemJS.packages = { + * jquery: { + * main: 'index.js', // when not set, package name is requested directly + * format: 'amd', + * defaultExtension: 'ts', // defaults to 'js', can be set to false + * modules: { + * '*.ts': { + * loader: 'typescript' + * }, + * 'vendor/sizzle.js': { + * format: 'global' + * } + * }, + * map: { + * // map internal require('sizzle') to local require('./vendor/sizzle') + * sizzle: './vendor/sizzle.js', + * // map any internal or external require of 'jquery/vendor/another' to 'another/index.js' + * './vendor/another.js': './another/index.js', + * // test.js / test -> lib/test.js + * './test.js': './lib/test.js', + * + * // environment-specific map configurations + * './index.js': { + * '~browser': './index-node.js', + * './custom-condition.js|~export': './index-custom.js' + * } + * }, + * // allows for setting package-prefixed depCache + * // keys are normalized module names relative to the package itself + * depCache: { + * // import 'package/index.js' loads in parallel package/lib/test.js,package/vendor/sizzle.js + * './index.js': ['./test'], + * './test.js': ['external-dep'], + * 'external-dep/path.js': ['./another.js'] + * } + * } + * }; + * + * Then: + * import 'jquery' -> jquery/index.js + * import 'jquery/submodule' -> jquery/submodule.js + * import 'jquery/submodule.ts' -> jquery/submodule.ts loaded as typescript + * import 'jquery/vendor/another' -> another/index.js + * + * Detailed Behaviours + * - main can have a leading "./" can be added optionally + * - map and defaultExtension are applied to the main + * - defaultExtension adds the extension only if the exact extension is not present + * - defaultJSExtensions applies after map when defaultExtension is not set + * - if a meta value is available for a module, map and defaultExtension are skipped + * - like global map, package map also applies to subpaths (sizzle/x, ./vendor/another/sub) + * - condition module map is '@env' module in package or '@system-env' globally + * - map targets support conditional interpolation ('./x': './x.#{|env}.js') + * - internal package map targets cannot use boolean conditionals + * + * Package Configuration Loading + * + * Not all packages may already have their configuration present in the System config + * For these cases, a list of packageConfigPaths can be provided, which when matched against + * a request, will first request a ".json" file by the package name to derive the package + * configuration from. This allows dynamic loading of non-predetermined code, a key use + * case in SystemJS. + * + * Example: + * + * SystemJS.packageConfigPaths = ['packages/test/package.json', 'packages/*.json']; + * + * // will first request 'packages/new-package/package.json' for the package config + * // before completing the package request to 'packages/new-package/path' + * SystemJS.import('packages/new-package/path'); + * + * // will first request 'packages/test/package.json' before the main + * SystemJS.import('packages/test'); + * + * When a package matches packageConfigPaths, it will always send a config request for + * the package configuration. + * The package name itself is taken to be the match up to and including the last wildcard + * or trailing slash. + * The most specific package config path will be used. + * Any existing package configurations for the package will deeply merge with the + * package config, with the existing package configurations taking preference. + * To opt-out of the package configuration request for a package that matches + * packageConfigPaths, use the { configured: true } package config option. + * + */ +(function() { + + hookConstructor(function(constructor) { + return function() { + constructor.call(this); + this.packages = {}; + this.packageConfigPaths = []; + }; + }); + + function getPackage(loader, normalized) { + // use most specific package + var curPkg, curPkgLen = 0, pkgLen; + for (var p in loader.packages) { + if (normalized.substr(0, p.length) === p && (normalized.length === p.length || normalized[p.length] === '/')) { + pkgLen = p.split('/').length; + if (pkgLen > curPkgLen) { + curPkg = p; + curPkgLen = pkgLen; + } + } + } + return curPkg; + } + + function addDefaultExtension(loader, pkg, pkgName, subPath, skipExtensions) { + // don't apply extensions to folders or if defaultExtension = false + if (!subPath || subPath[subPath.length - 1] == '/' || skipExtensions || pkg.defaultExtension === false) + return subPath; + + var metaMatch = false; + + // exact meta or meta with any content after the last wildcard skips extension + if (pkg.meta) + getMetaMatches(pkg.meta, subPath, function(metaPattern, matchMeta, matchDepth) { + if (matchDepth == 0 || metaPattern.lastIndexOf('*') != metaPattern.length - 1) + return metaMatch = true; + }); + + // exact global meta or meta with any content after the last wildcard skips extension + if (!metaMatch && loader.meta) + getMetaMatches(loader.meta, pkgName + '/' + subPath, function(metaPattern, matchMeta, matchDepth) { + if (matchDepth == 0 || metaPattern.lastIndexOf('*') != metaPattern.length - 1) + return metaMatch = true; + }); + + if (metaMatch) + return subPath; + + // work out what the defaultExtension is and add if not there already + // NB reconsider if default should really be ".js"? + var defaultExtension = '.' + (pkg.defaultExtension || 'js'); + if (subPath.substr(subPath.length - defaultExtension.length) != defaultExtension) + return subPath + defaultExtension; + else + return subPath; + } + + function applyPackageConfigSync(loader, pkg, pkgName, subPath, skipExtensions) { + // main + if (!subPath) { + if (pkg.main) + subPath = pkg.main.substr(0, 2) == './' ? pkg.main.substr(2) : pkg.main; + // also no submap if name is package itself (import 'pkg' -> 'path/to/pkg.js') + else + // NB can add a default package main convention here when defaultJSExtensions is deprecated + // if it becomes internal to the package then it would no longer be an exit path + return pkgName + (loader.defaultJSExtensions ? '.js' : ''); + } + + // map config checking without then with extensions + if (pkg.map) { + var mapPath = './' + subPath; + + var mapMatch = getMapMatch(pkg.map, mapPath); + + // we then check map with the default extension adding + if (!mapMatch) { + mapPath = './' + addDefaultExtension(loader, pkg, pkgName, subPath, skipExtensions); + if (mapPath != './' + subPath) + mapMatch = getMapMatch(pkg.map, mapPath); + } + if (mapMatch) { + var mapped = doMapSync(loader, pkg, pkgName, mapMatch, mapPath, skipExtensions); + if (mapped) + return mapped; + } + } + + // normal package resolution + return pkgName + '/' + addDefaultExtension(loader, pkg, pkgName, subPath, skipExtensions); + } + + function validMapping(mapMatch, mapped, pkgName, path) { + // disallow internal to subpath maps + if (mapMatch == '.') + throw new Error('Package ' + pkgName + ' has a map entry for "." which is not permitted.'); + + // allow internal ./x -> ./x/y or ./x/ -> ./x/y recursive maps + // but only if the path is exactly ./x and not ./x/z + if (mapped.substr(0, mapMatch.length) == mapMatch && path.length > mapMatch.length) + return false; + + return true; + } + + function doMapSync(loader, pkg, pkgName, mapMatch, path, skipExtensions) { + if (path[path.length - 1] == '/') + path = path.substr(0, path.length - 1); + var mapped = pkg.map[mapMatch]; + + if (typeof mapped == 'object') + throw new Error('Synchronous conditional normalization not supported sync normalizing ' + mapMatch + ' in ' + pkgName); + + if (!validMapping(mapMatch, mapped, pkgName, path) || typeof mapped != 'string') + return; + + // package map to main / base-level + if (mapped == '.') + mapped = pkgName; + + // internal package map + else if (mapped.substr(0, 2) == './') + return pkgName + '/' + addDefaultExtension(loader, pkg, pkgName, mapped.substr(2) + path.substr(mapMatch.length), skipExtensions); + + // external map reference + return loader.normalizeSync(mapped + path.substr(mapMatch.length), pkgName + '/'); + } + + function applyPackageConfig(loader, pkg, pkgName, subPath, skipExtensions) { + // main + if (!subPath) { + if (pkg.main) + subPath = pkg.main.substr(0, 2) == './' ? pkg.main.substr(2) : pkg.main; + // also no submap if name is package itself (import 'pkg' -> 'path/to/pkg.js') + else + // NB can add a default package main convention here when defaultJSExtensions is deprecated + // if it becomes internal to the package then it would no longer be an exit path + return Promise.resolve(pkgName + (loader.defaultJSExtensions ? '.js' : '')); + } + + // map config checking without then with extensions + var mapPath, mapMatch; + + if (pkg.map) { + mapPath = './' + subPath; + mapMatch = getMapMatch(pkg.map, mapPath); + + // we then check map with the default extension adding + if (!mapMatch) { + mapPath = './' + addDefaultExtension(loader, pkg, pkgName, subPath, skipExtensions); + if (mapPath != './' + subPath) + mapMatch = getMapMatch(pkg.map, mapPath); + } + } + + return (mapMatch ? doMap(loader, pkg, pkgName, mapMatch, mapPath, skipExtensions) : Promise.resolve()) + .then(function(mapped) { + if (mapped) + return Promise.resolve(mapped); + + // normal package resolution / fallback resolution for no conditional match + return Promise.resolve(pkgName + '/' + addDefaultExtension(loader, pkg, pkgName, subPath, skipExtensions)); + }); + } + + function doStringMap(loader, pkg, pkgName, mapMatch, mapped, path, skipExtensions) { + // NB the interpolation cases should strictly skip subsequent interpolation + // package map to main / base-level + if (mapped == '.') + mapped = pkgName; + + // internal package map + else if (mapped.substr(0, 2) == './') + return Promise.resolve(pkgName + '/' + addDefaultExtension(loader, pkg, pkgName, mapped.substr(2) + path.substr(mapMatch.length), skipExtensions)) + .then(function(name) { + return interpolateConditional.call(loader, name, pkgName + '/'); + }); + + // external map reference + return loader.normalize(mapped + path.substr(mapMatch.length), pkgName + '/'); + } + + function doMap(loader, pkg, pkgName, mapMatch, path, skipExtensions) { + if (path[path.length - 1] == '/') + path = path.substr(0, path.length - 1); + + var mapped = pkg.map[mapMatch]; + + if (typeof mapped == 'string') { + if (!validMapping(mapMatch, mapped, pkgName, path)) + return Promise.resolve(); + return doStringMap(loader, pkg, pkgName, mapMatch, mapped, path, skipExtensions); + } + + // we use a special conditional syntax to allow the builder to handle conditional branch points further + if (loader.builder) + return Promise.resolve(pkgName + '/#:' + path); + + // we load all conditions upfront + var conditionPromises = []; + var conditions = []; + for (var e in mapped) { + var c = parseCondition(e); + conditions.push({ + condition: c, + map: mapped[e] + }); + conditionPromises.push(loader['import'](c.module, pkgName)); + } + + // map object -> conditional map + return Promise.all(conditionPromises) + .then(function(conditionValues) { + // first map condition to match is used + for (var i = 0; i < conditions.length; i++) { + var c = conditions[i].condition; + var value = readMemberExpression(c.prop, conditionValues[i]); + if (!c.negate && value || c.negate && !value) + return conditions[i].map; + } + }) + .then(function(mapped) { + if (mapped) { + if (!validMapping(mapMatch, mapped, pkgName, path)) + return; + return doStringMap(loader, pkg, pkgName, mapMatch, mapped, path, skipExtensions); + } + + // no environment match -> fallback to original subPath by returning undefined + }); + } + + // normalizeSync = decanonicalize + package resolution + SystemJSLoader.prototype.normalizeSync = SystemJSLoader.prototype.decanonicalize = SystemJSLoader.prototype.normalize; + + // decanonicalize must JUST handle package defaultExtension: false case when defaultJSExtensions is set + // to be deprecated! + hook('decanonicalize', function(decanonicalize) { + return function(name, parentName) { + if (this.builder) + return decanonicalize.call(this, name, parentName, true); + + var decanonicalized = decanonicalize.call(this, name, parentName, false); + + if (!this.defaultJSExtensions) + return decanonicalized; + + var pkgName = getPackage(this, decanonicalized); + + var pkg = this.packages[pkgName]; + var defaultExtension = pkg && pkg.defaultExtension; + + if (defaultExtension == undefined && pkg && pkg.meta) + getMetaMatches(pkg.meta, decanonicalized.substr(pkgName), function(metaPattern, matchMeta, matchDepth) { + if (matchDepth == 0 || metaPattern.lastIndexOf('*') != metaPattern.length - 1) { + defaultExtension = false; + return true; + } + }); + + if ((defaultExtension === false || defaultExtension && defaultExtension != '.js') && name.substr(name.length - 3, 3) != '.js' && decanonicalized.substr(decanonicalized.length - 3, 3) == '.js') + decanonicalized = decanonicalized.substr(0, decanonicalized.length - 3); + + return decanonicalized; + }; + }); + + hook('normalizeSync', function(normalizeSync) { + return function(name, parentName, isPlugin) { + var loader = this; + isPlugin = isPlugin === true; + + // apply contextual package map first + // (we assume the parent package config has already been loaded) + if (parentName) + var parentPackageName = getPackage(loader, parentName) || + loader.defaultJSExtensions && parentName.substr(parentName.length - 3, 3) == '.js' && + getPackage(loader, parentName.substr(0, parentName.length - 3)); + + var parentPackage = parentPackageName && loader.packages[parentPackageName]; + + // ignore . since internal maps handled by standard package resolution + if (parentPackage && name[0] != '.') { + var parentMap = parentPackage.map; + var parentMapMatch = parentMap && getMapMatch(parentMap, name); + + if (parentMapMatch && typeof parentMap[parentMapMatch] == 'string') { + var mapped = doMapSync(loader, parentPackage, parentPackageName, parentMapMatch, name, isPlugin); + if (mapped) + return mapped; + } + } + + var defaultJSExtension = loader.defaultJSExtensions && name.substr(name.length - 3, 3) != '.js'; + + // apply map, core, paths, contextual package map + var normalized = normalizeSync.call(loader, name, parentName, false); + + // undo defaultJSExtension + if (defaultJSExtension && normalized.substr(normalized.length - 3, 3) != '.js') + defaultJSExtension = false; + if (defaultJSExtension) + normalized = normalized.substr(0, normalized.length - 3); + + var pkgConfigMatch = getPackageConfigMatch(loader, normalized); + var pkgName = pkgConfigMatch && pkgConfigMatch.packageName || getPackage(loader, normalized); + + if (!pkgName) + return normalized + (defaultJSExtension ? '.js' : ''); + + var subPath = normalized.substr(pkgName.length + 1); + + return applyPackageConfigSync(loader, loader.packages[pkgName] || {}, pkgName, subPath, isPlugin); + }; + }); + + hook('normalize', function(normalize) { + return function(name, parentName, isPlugin) { + var loader = this; + isPlugin = isPlugin === true; + + return Promise.resolve() + .then(function() { + // apply contextual package map first + // (we assume the parent package config has already been loaded) + if (parentName) + var parentPackageName = getPackage(loader, parentName) || + loader.defaultJSExtensions && parentName.substr(parentName.length - 3, 3) == '.js' && + getPackage(loader, parentName.substr(0, parentName.length - 3)); + + var parentPackage = parentPackageName && loader.packages[parentPackageName]; + + // ignore . since internal maps handled by standard package resolution + if (parentPackage && name.substr(0, 2) != './') { + var parentMap = parentPackage.map; + var parentMapMatch = parentMap && getMapMatch(parentMap, name); + + if (parentMapMatch) + return doMap(loader, parentPackage, parentPackageName, parentMapMatch, name, isPlugin); + } + + return Promise.resolve(); + }) + .then(function(mapped) { + if (mapped) + return mapped; + + var defaultJSExtension = loader.defaultJSExtensions && name.substr(name.length - 3, 3) != '.js'; + + // apply map, core, paths, contextual package map + var normalized = normalize.call(loader, name, parentName, false); + + // undo defaultJSExtension + if (defaultJSExtension && normalized.substr(normalized.length - 3, 3) != '.js') + defaultJSExtension = false; + if (defaultJSExtension) + normalized = normalized.substr(0, normalized.length - 3); + + var pkgConfigMatch = getPackageConfigMatch(loader, normalized); + var pkgName = pkgConfigMatch && pkgConfigMatch.packageName || getPackage(loader, normalized); + + if (!pkgName) + return Promise.resolve(normalized + (defaultJSExtension ? '.js' : '')); + + var pkg = loader.packages[pkgName]; + + // if package is already configured or not a dynamic config package, use existing package config + var isConfigured = pkg && (pkg.configured || !pkgConfigMatch); + return (isConfigured ? Promise.resolve(pkg) : loadPackageConfigPath(loader, pkgName, pkgConfigMatch.configPath)) + .then(function(pkg) { + var subPath = normalized.substr(pkgName.length + 1); + + return applyPackageConfig(loader, pkg, pkgName, subPath, isPlugin); + }); + }); + }; + }); + + // check if the given normalized name matches a packageConfigPath + // if so, loads the config + var packageConfigPaths = {}; + + // data object for quick checks against package paths + function createPkgConfigPathObj(path) { + var lastWildcard = path.lastIndexOf('*'); + var length = Math.max(lastWildcard + 1, path.lastIndexOf('/')); + return { + length: length, + regEx: new RegExp('^(' + path.substr(0, length).replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^\\/]+') + ')(\\/|$)'), + wildcard: lastWildcard != -1 + }; + } + + // most specific match wins + function getPackageConfigMatch(loader, normalized) { + var pkgName, exactMatch = false, configPath; + for (var i = 0; i < loader.packageConfigPaths.length; i++) { + var packageConfigPath = loader.packageConfigPaths[i]; + var p = packageConfigPaths[packageConfigPath] || (packageConfigPaths[packageConfigPath] = createPkgConfigPathObj(packageConfigPath)); + if (normalized.length < p.length) + continue; + var match = normalized.match(p.regEx); + if (match && (!pkgName || (!(exactMatch && p.wildcard) && pkgName.length < match[1].length))) { + pkgName = match[1]; + exactMatch = !p.wildcard; + configPath = pkgName + packageConfigPath.substr(p.length); + } + } + + if (!pkgName) + return; + + return { + packageName: pkgName, + configPath: configPath + }; + } + + function loadPackageConfigPath(loader, pkgName, pkgConfigPath) { + var configLoader = loader.pluginLoader || loader; + + // NB remove this when json is default + (configLoader.meta[pkgConfigPath] = configLoader.meta[pkgConfigPath] || {}).format = 'json'; + configLoader.meta[pkgConfigPath].loader = null; + + return configLoader.load(pkgConfigPath) + .then(function() { + var cfg = configLoader.get(pkgConfigPath)['default']; + + // support "systemjs" prefixing + if (cfg.systemjs) + cfg = cfg.systemjs; + + // modules backwards compatibility + if (cfg.modules) { + cfg.meta = cfg.modules; + warn.call(loader, 'Package config file ' + pkgConfigPath + ' is configured with "modules", which is deprecated as it has been renamed to "meta".'); + } + + return setPkgConfig(loader, pkgName, cfg, true); + }); + } + + function getMetaMatches(pkgMeta, subPath, matchFn) { + // wildcard meta + var meta = {}; + var wildcardIndex; + for (var module in pkgMeta) { + // allow meta to start with ./ for flexibility + var dotRel = module.substr(0, 2) == './' ? './' : ''; + if (dotRel) + module = module.substr(2); + + wildcardIndex = module.indexOf('*'); + if (wildcardIndex === -1) + continue; + + if (module.substr(0, wildcardIndex) == subPath.substr(0, wildcardIndex) + && module.substr(wildcardIndex + 1) == subPath.substr(subPath.length - module.length + wildcardIndex + 1)) { + // alow match function to return true for an exit path + if (matchFn(module, pkgMeta[dotRel + module], module.split('/').length)) + return; + } + } + // exact meta + var exactMeta = pkgMeta[subPath] && pkgMeta.hasOwnProperty && pkgMeta.hasOwnProperty(subPath) ? pkgMeta[subPath] : pkgMeta['./' + subPath]; + if (exactMeta) + matchFn(exactMeta, exactMeta, 0); + } + + hook('locate', function(locate) { + return function(load) { + var loader = this; + return Promise.resolve(locate.call(this, load)) + .then(function(address) { + var pkgName = getPackage(loader, load.name); + if (pkgName) { + var pkg = loader.packages[pkgName]; + var subPath = load.name.substr(pkgName.length + 1); + + var meta = {}; + if (pkg.meta) { + var bestDepth = 0; + + // NB support a main shorthand in meta here? + getMetaMatches(pkg.meta, subPath, function(metaPattern, matchMeta, matchDepth) { + if (matchDepth > bestDepth) + bestDepth = matchDepth; + extendMeta(meta, matchMeta, matchDepth && bestDepth > matchDepth); + }); + + extendMeta(load.metadata, meta); + } + + // format + if (pkg.format && !load.metadata.loader) + load.metadata.format = load.metadata.format || pkg.format; + } + + return address; + }); + }; + }); + +})(); +/* + * Script tag fetch + * + * When load.metadata.scriptLoad is true, we load via script tag injection. + */ +(function() { + + if (typeof document != 'undefined') + var head = document.getElementsByTagName('head')[0]; + + var curSystem; + var curRequire; + + // if doing worker executing, this is set to the load record being executed + var workerLoad = null; + + // interactive mode handling method courtesy RequireJS + var ieEvents = head && (function() { + var s = document.createElement('script'); + var isOpera = typeof opera !== 'undefined' && opera.toString() === '[object Opera]'; + return s.attachEvent && !(s.attachEvent.toString && s.attachEvent.toString().indexOf('[native code') < 0) && !isOpera; + })(); + + // IE interactive-only part + // we store loading scripts array as { script: