Search code examples
javascriptmootoolsmootools-more

Which modules of mootools more I am using?


All mootools more modules are included in my app, but I would like to remove the ones I am not using. Is there a quick way to know which modules I am using starting from the script depending on mootools more?


Solution

  • no easy way, I am afraid. you can spy on stuff while your app runs so you can get some usage/coverage stats but because mootools is prototypal, the extensions to Array/String/Function/Date etc that more does may be more complicated to catch.

    To catch classes that have been instantiated, build a list and use something like that:

    Object.monitor = function(obj, match){
        var keys = (function(obj){
                // include from prototype also, any function.
                var keys = [], key;
                for (key in obj) typeof obj[key] === 'function' && keys.push(key);
                return keys;
            }(obj)),
            log = function(what, method){
                // more use goes red in console.
                console.log(obj, method, what);
            },
            counters = {};
    
        keys.forEach(function(key){
            var orig = obj[key];
            Object.defineProperty(obj, key, {
                get: function(){
                    key in counters || (counters[key] = 0);
                    counters[key]++;
                    key.test(match) && log(counters[key], key);
                    return orig;
                }
            });
        });
    };
    
    
    var protos = [Fx.Reveal, Fx.Slide, Request.JSONP]; // etc etc - stuff you are unsure of.
    protos.forEach(function(klass){
        Object.monitor(klass.prototype, /\$constructor/);
    });
    
    new Request.JSONP({});
    

    as soon as any of these items gets instantiated OR extended, the constructor will get referenced and you will get the log to show it. http://jsfiddle.net/dimitar/8nCe6/ - this will instantiate Request.JSONP().

    I wrote the Object.monitor to spy on methods being called on a particular instance but the same principle applies. The console formatting only works nice in FireBug and WebInspector - native FF console needs to be made simple.

    http://fragged.org/spy-on-any-method-on-an-object-and-profile-number-of-times-called_1661.html

    you can use it to spy on say, Array.prototype or any suchlike as well - but the difficulty is the code complexity of more. Hard to really nail it down :(

    probably easier to concatenate all your scripts EXCEPT for mootools-more then grep for known classes / methods from the Types.