I am tasked with a multi-page website that uses angularjs as a front-end framework. Currently, I used browserify with npm to manage the module dependencies, which generate a bundled file for the whole app. However, each page only use part of that bundled file (1 controller file and a few directive/service files). Is it possible to lazy load the files instead of one large bundled files?

I have read about webpack lazy-load, but I don't know if it does what I want (i.e. auto detect and load files, enable the use of npm package, etc.)

Yes. You can use require.ensure inside a function instead of simple require. In this case the module will be exported in other file and loaded only when needed. Here is an example:

function (callback) {
    require.ensure(['module'], function (require) {
        callback(require('module'));
    });
}

So 'module' loaded only when you call this function first time.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.