function setupAmdLoader()

in Extensions/GoogleAnalyticsExperiments/Src/UIContribution/lib/VSS.SDK.js [1157:1281]


    function setupAmdLoader() {
        var hostRootUri = getRootUri(hostPageContext.webContext);
        // Place context so that VSS scripts pick it up correctly
        window.__vssPageContext = hostPageContext;
        // MS Ajax config needs to exist before loading MS Ajax library
        window.__cultureInfo = hostPageContext.microsoftAjaxConfig.cultureInfo;
        // Append CSS first
        if (usingPlatformStyles !== false) {
            if (hostPageContext.coreReferences.stylesheets) {
                hostPageContext.coreReferences.stylesheets.forEach(function (stylesheet) {
                    if (stylesheet.isCoreStylesheet) {
                        var cssLink = document.createElement("link");
                        cssLink.href = getAbsoluteUrl(stylesheet.url, hostRootUri);
                        cssLink.rel = "stylesheet";
                        safeAppendToDom(cssLink, "head");
                    }
                });
            }
        }
        if (!usingPlatformScripts) {
            // Just wanted to load CSS, no scripts. Can exit here.
            loaderConfigured = true;
            triggerReady();
            return;
        }
        var scripts = [];
        var anyCoreScriptLoaded = false;
        // Add scripts and loader configuration
        if (hostPageContext.coreReferences.scripts) {
            hostPageContext.coreReferences.scripts.forEach(function (script) {
                if (script.isCoreModule) {
                    var alreadyLoaded = false;
                    var global = window;
                    if (script.identifier === "JQuery") {
                        alreadyLoaded = !!global.jQuery;
                    }
                    else if (script.identifier === "JQueryUI") {
                        alreadyLoaded = !!(global.jQuery && global.jQuery.ui && global.jQuery.ui.version);
                    }
                    else if (script.identifier === "AMDLoader") {
                        alreadyLoaded = typeof global.define === "function" && !!global.define.amd;
                    }
                    if (!alreadyLoaded) {
                        scripts.push({ source: getAbsoluteUrl(script.url, hostRootUri) });
                    }
                    else {
                        anyCoreScriptLoaded = true;
                    }
                }
            });
            if (hostPageContext.coreReferences.coreScriptsBundle && !anyCoreScriptLoaded) {
                // If core scripts bundle exists and no core scripts already loaded by extension,
                // we are free to add core bundle. otherwise, load core scripts individually.
                scripts = [{ source: getAbsoluteUrl(hostPageContext.coreReferences.coreScriptsBundle.url, hostRootUri) }];
            }
            if (hostPageContext.coreReferences.extensionCoreReferences) {
                scripts.push({ source: getAbsoluteUrl(hostPageContext.coreReferences.extensionCoreReferences.url, hostRootUri) });
            }
        }
        // Define a new config for extension loader
        var newConfig = {
            baseUrl: extensionContext.baseUri,
            contributionPaths: null,
            paths: {},
            shim: {}
        };
        // See whether any configuration specified initially. If yes, copy them to new config
        if (initOptions.moduleLoaderConfig) {
            if (initOptions.moduleLoaderConfig.baseUrl) {
                newConfig.baseUrl = initOptions.moduleLoaderConfig.baseUrl;
            }
            // Copy paths
            extendLoaderPaths(initOptions.moduleLoaderConfig, newConfig);
            // Copy shim
            extendLoaderShim(initOptions.moduleLoaderConfig, newConfig);
        }
        // Use some of the host config to support VSSF and TFS platform as well as some 3rd party libraries
        if (hostPageContext.moduleLoaderConfig) {
            // Copy host shim
            extendLoaderShim(hostPageContext.moduleLoaderConfig, newConfig);
            // Add contribution paths to new config
            var contributionPaths = hostPageContext.moduleLoaderConfig.contributionPaths;
            if (contributionPaths) {
                for (var p in contributionPaths) {
                    if (contributionPaths.hasOwnProperty(p) && !newConfig.paths[p]) {
                        // Add the contribution path
                        var contributionPathValue = contributionPaths[p].value;
                        if (!contributionPathValue.match("^https?://")) {
                            newConfig.paths[p] = hostRootUri + contributionPathValue;
                        }
                        else {
                            newConfig.paths[p] = contributionPathValue;
                        }
                        // Look for other path mappings that fall under the contribution path (e.g. "bundles")
                        var configPaths = hostPageContext.moduleLoaderConfig.paths;
                        if (configPaths) {
                            var contributionRoot = p + "/";
                            var rootScriptPath = combinePaths(hostRootUri, hostPageContext.moduleLoaderConfig.baseUrl);
                            for (var pathKey in configPaths) {
                                if (startsWith(pathKey, contributionRoot)) {
                                    var pathValue = configPaths[pathKey];
                                    if (!pathValue.match("^https?://")) {
                                        if (pathValue[0] === "/") {
                                            pathValue = combinePaths(hostRootUri, pathValue);
                                        }
                                        else {
                                            pathValue = combinePaths(rootScriptPath, pathValue);
                                        }
                                    }
                                    newConfig.paths[pathKey] = pathValue;
                                }
                            }
                        }
                    }
                }
            }
        }
        // requireJS public api doesn't support reading the current config, so save it off for use by our internal host control.
        window.__vssModuleLoaderConfig = newConfig;
        scripts.push({ content: "require.config(" + JSON.stringify(newConfig) + ");" });
        addScriptElements(scripts, 0, function () {
            loaderConfigured = true;
            triggerReady();
        });
    }