0
votes

I have been looking into requirejs for couple of days. I think it's a great tool for code structure, modules etc. I'm more interested in performance and lazy loading of modules in production environment. It can minify & merge js and loads modules on demand.

I don't really understand how requirejs achieves this feature(lazy loading of modules in a minified and concatenated file)? I'll appreciate if someone can shed some light on this?

From requirejs:

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:

Not sure what above statement means? Can we achieve this lazy loading feature with vanilla JS?

1
The statement means that this fails: var name='nameofmodule';var module=require(name);, and that it should be written as var module=require('nameofmodule');Rob W

1 Answers

1
votes

It does not minify and concatenate on demand. The documentation you have read is for r.js - the require.js optimizer which can run on nodejs and can minimize and concatenate your code, but not "on demand" - you have to run the optimizer to create the minified and concatenated files and then put those on your live site.

It can't take non-minified files on your live site and minify them when a user requests them.