Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • If I just would list all modules in the "Top require call", would r.js also include+link all top require and define calls from nested and nested-nested modules into the main-app file ?

  • They are mentioning the include option for r.js in the Docs. Does it make sense here and if so, how to properly invoke it ?

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.

like image 206
jAndy Avatar asked Jul 26 '12 15:07

jAndy


1 Answers

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.

like image 72
Aaronius Avatar answered Oct 05 '22 16:10

Aaronius