简体   繁体   中英

Webpack random module loading at runtime

I have a number of modules I'd like to load/don't load conditionally at runtime. However, I do not know the name of the module at bundling time. My code is something like this:

function A(depModule){
    return {
        theActionThatDependsOnThatModule: function(){
            require([depModule], function(dep){
                dep.doSomething();
            });
        }
    }
}

and then I have a bunch of modules that consume the one above, being something like this:

require.ensure('../window');
var obj = A('../window');
obj.theActionThatDependsOnThatModule()

But I get the 'require.ensure is undefined' error in browser. What am I doing wrong? Am I missing something?

So I kinda found a solution. It's ugly, and maybe someone can tell me how to do it better, but in the meanwhile it works for me:

function A(depModule){
    return {
        theActionThatDependsOnThatModule: function(){
            depModule(function(dep){
                dep.doSomething();
            });
        }
    }
}

var obj = A(function(cb){
    require(['./window'], cb);
});
obj.theActionThatDependsOnThatModule()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM