是否有任何技巧来记录由require.js在控制台中加载的模块?例如加载jQuery负载下划线加载骨干网
我需要这样做才能理解下载每个模块并在不同环境下对其进行测试所需的时间。
谢谢,Mandar Katre
发布于 2013-09-21 19:37:41
您可以尝试类似于这把小提琴的东西,并使用(内部API onResourceLoad )。这不会给出完全准确的加载时间,但它会让您知道哪些模块已经被请求,以及在给定的启动时间之后,它们完成加载的时间。
<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>而这个JS
start = +new Date();
require(["jquery", "underscore"], function() {
// log the global context's defineds
console.log("require.s.contexts._.defined", require.s.contexts._.defined);
});在测试中生成此输出:
onResourceLoad 140ms jquery
onResourceLoad 167ms underscore
require.s.contexts._.defined Object {jquery: function, underscore: function}https://stackoverflow.com/questions/18919184
复制相似问题