jAndy
jAndy

Reputation: 235962

requireJS optimizer does not include nested require calls

I'm reading through the optimizer documentation for quite a while, but it seems like I can't figure it. The doc says:

The optimizer will only combine modules that are specified in arrays of string literals that are passed to top-level require and define calls, or the require('name') string literal calls in a simplified CommonJS wrapping. So, it will not find modules that are loaded via a variable name:

OK so far so good. This basically means r.js won't include nor crawl nested dependencies. Now lets assume we have a "main application" file which looks like the following:

require([ 'es5shim', 'tools' ], function() {
    console.log('fictive app entry point');

    require([ 'domready!' ], function( doc ) {
        console.log('domReady, loading GUI modules...');
        require([ 'GUI/window', 'GUI/header', 'GUI/content' ]);
    });
});

I guess the problem becomes pretty obvious here. r.js (the optimizer) creates that file by only linking es5shim.js and tools.js into it. Is there any good way / workaround to tell that optimizer, that it also should link the window.js, header.js and content.js files in this example ?

Of course the domReady plugin in this instance will get loaded and it will eventually execute the callback, but the structure itself here it seems, prevents the optimizer from doing its job.

Question are:

Of course you don't want to lose the option to lazy-load modules later-on, but for this kind of dependency (waiting for DOMContentLoaded), I hope there is a way to workaround that.

Upvotes: 24

Views: 4774

Answers (1)

Aaronius
Aaronius

Reputation: 4953

By default RequireJS doesn't scan nested require calls because it assumes those are meant to load dependencies at runtime. You override this by setting

findNestedDependencies: true

in your optimization config. Hope that helps.

Upvotes: 45

Related Questions