require.js: Access all loaded modules

霸气de小男生 提交于 2019-11-27 17:58:37

Yes, require.s.contexts._.defined is an object which its keys are the module names and the values include the value returned from that module.

require.s.contexts._.defined includes all the modules (either define() or require() such as the Javascript file that is the starting point of the program and is indicated using data-main for RequireJS).

Just finished a similar behavior within my RequireJS project. require.s.contexts['_'].registry holds the list of the registered modules.

I am using Underscore.js for getting, filtering and iterating the list of modules. Maybe the following code fragment helps you:

var modules_registered = _.keys(require.s.contexts['_'].registry);
var modules_to_be_initialized = _.filter(modules_registered, function(module_title) {
    return module_title.indexOf('modules') > -1;
});

_.each(modules_to_be_initialized, function(module_name) {
    require([module_name], function(current_module) {
        current_module.init();
    });
});

I'm lazy, so I just did this:

    var modulesToLoad = Object.keys(require.s.contexts['_'].registry);
    require(modulesToLoad);

Based on other answers here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!