Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do concatenated RequireJS AMD modules need a loader?

We love RequireJS and AMD during development, where we can edit a module, hit reload in our browser, and immediately see the result. But when it comes time to concatenate our modules into a single file for production deployment, there apparently has to be an AMD loader still present, whether that loader is RequireJS itself or its smaller partner “almond” as explained here:

http://requirejs.org/docs/faq-optimization.html#wrap

My confusion is: why is a loader necessary at all? Unless you have very unusual circumstances that make it necessary for you to make require() calls inside of your modules, it would appear that a series of AMD modules could be concatenated without a loader present at all. The simplest possible example would be a pair of modules like the following.

ModA.js:

define([], function() {     return {a: 1}; }); 

ModB.js:

define(['ModA'], function(A) {     return {b : 2}; }); 

Given these two modules, it seems that a concatenator could simply produce the following text, and not burden the production server or browser with the extra bandwidth or computation required by either RequireJS or Almond.

I imagine a concatenator that produces (and I am using chevron-quotes «,» to show where the snippets from the two modules above have been inserted):

(function() {     var ModA = «function() {         return {a: 1};     }»();     var ModB = «function(A) {         return {b : 2};     }»(ModA);     return ModB; })(); 

This, so far as I can see, would correctly reproduce the semantics of AMD, with a minimum of extraneous glue JavaScript. Is there such a concatenator available? If not, would I be a fool for thinking that I should write one — are there really very few code bases that consist of simple and clean modules written with define() and that never need further require() calls inside that kick off later asynchronous fetches of code?

like image 786
Brandon Rhodes Avatar asked Jun 11 '12 21:06

Brandon Rhodes


People also ask

Why do we need RequireJS?

RequireJS is a basic loader, which is used to loads the JavaScript files, it is a framework to manage dependencies between JavaScript files, and in modular programming, all the functionality divides in different modules, so RequireJs is a best tool to assemble different JavaScript files from different modules by which ...

How does AMD work?

Asynchronous module definition (AMD) is a specification for the programming language JavaScript. It defines an application programming interface (API) that defines code modules and their dependencies, and loads them asynchronously if desired.

What is non AMD libraries?

Non-AMD compliant libraries can be ones you have no control over or are yours but too unwieldy to convert. Non-AMD libs are implemented as JavaScript namespaces (a concept, not really a construct). JavaScript namespaces are objects.


1 Answers

An AMD optimiser has the scope to optimise more than the number of files to be downloaded, it can also optimise the number of modules loaded in memory.

For example, if you have 10 modules and can optimise them to 1 file, then you have saved yourself 9 downloads.

If Page1 uses all 10 modules then that's great. But what if Page2 only uses 1? An AMD loader can delay the execution of the 'factory function' until a module is require'd. Therefore, Page2 only triggers a single 'factory function' to execute.

If each module consumes 100kb of memory upon being require'd, then an AMD framework that has runtime optimisation will also save us 900kb of memory on Page2.

An example of this could be an 'About Box' style dialog. Where the very execution of it is delayed until the very last second as it won't be accessed in 99% of cases. E.g. (in loose jQuery syntax):

aboutBoxBtn.click(function () {     require(['aboutBox'], function (aboutBox) {         aboutBox.show();     } }); 

You save the expense of creating the JS objects and DOM associated with the 'About Box' until you are sure it's necessary.

For more info, see Delay executing defines until first require for requirejs's take on this.

like image 164
Paul Grime Avatar answered Sep 20 '22 21:09

Paul Grime