Running dependencies in the console for requirejs

Is there any trick for registering require.js loadable modules in the console? e.g. loading jQuery loading underline loading baseline

I need this to understand how long it takes to download each module and register it in order to test it in different environments.

Thank you Mandar Katre

+4
source share
1 answer

You can try something similar to this script and use the internal API onResourceLoad . This will not give a completely accurate load time, but it will give you an idea of ​​which modules were requested and how long after a given start time they were loaded.

<script>
require = {
    paths: {
        "jquery": "http://code.jquery.com/jquery-2.0.3",
        "underscore": "http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min"
    },
    shim: {
        "underscore": {
            deps: ["jquery"],
            exports: "_"
        }
    }
};
</script>
<script src="http://requirejs.org/docs/release/2.1.8/comments/require.js"></script>
<script>
// https://github.com/jrburke/requirejs/wiki/Internal-API:-onResourceLoad
requirejs.onResourceLoad = function(context, map, depArray) {
    var duration = new Date() - start;
    console.log("onResourceLoad", duration + "ms", map.id);
}
</script>

and this is js

start = +new Date();

require(["jquery", "underscore"], function() {
    // log the global context defineds
    console.log("require.s.contexts._.defined", require.s.contexts._.defined);
});

Produces this output in a test:

onResourceLoad 140ms jquery
onResourceLoad 167ms underscore
require.s.contexts._.defined  Object {jquery: function, underscore: function}
+8
source

All Articles