requirejs-util.js 1.44 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

(function ($, window) {
    "use strict";

    // List of define() calls with arguments and call stack
    var defineCalls = [];

    // Get current call stack, including script path information
    var getFileStack = function() {
        try {
            throw new Error();
        } catch (e) {
            if (!e.stack) {
                throw new Error('The browser needs to support Error.stack property');
            }
            return e.stack;
        }
    };

    // Intercept RequireJS define() calls, which are performed by AMD scripts upon loading
    window.define = function () {
        var stack = getFileStack();
        defineCalls.push({
            stack: stack,
            args: arguments
        });
    };

    window.require = function(dependencies, callback){
        return callback && callback();
    };

    // Exposed interface
    var requirejsUtil = {
        getDefineArgsInScript: function (scriptPath) {
            var result;
            for (var i = 0; i < defineCalls.length; i++) {
                if (defineCalls[i].stack.indexOf(scriptPath) >= 0) {
                    result = defineCalls[i].args;
                    break;
                }
            }
            return result;
        }
    };

    window.jsunit = window.jsunit || {};
    $.extend(window.jsunit, {requirejsUtil: requirejsUtil});
})(jQuery, window);