RequireJS 2.0 Delayed Module Evaluation and Google Maps
The RequireJS behavior changed from version 1.0 to 2.0. Now module dependencies are only loaded after a explicit require()
call or if it is on the dependency tree of some of the require
‘d modules. That means that the module factory (define
callback) won’t execute unless it is needed. That is a huge win for many reasons, less work for the JS engine and code has the same behavior before and after build. It also makes it possible to create alias to complex modules without triggering a download.
On my current project I have ~15 JS widgets that can be placed on any page and the amount of JS required by each widget is small so it makes sense to bundle all the JS into a single file for production (<30KB minified + gzipped excluding jQuery) instead of loading things on demand (a single request have better perf results than multiple requests in many cases).
I will show a very simple technique I used on the project to create an alias to the Google Maps API since I think it can be useful to other people as well in different contexts.