简体   繁体   中英

Require JS(AMD) working under the hood on minification of files in to a single file

Require.js looks like a perfect solution for having a module based code.As it is mentioned in its website it loads the dependencies on demand(So its faster).But when we use their r.js plugin to minify the code base in to single file , it loads the entire combined file.

So does that mean it is a normal async load and not on demand load? or will r.js has any optimization technique internally to do this efficiently?

Loading individual JS modules on demand is a bit of a red herring because it's often a better idea to put all your code into a single minified bundle even if most users will only use a subset of it. GZipping your resources will reduce size of your scripts by further 33%-50% (at least) so you may end up with a total that's smaller than a single JPEG file (depending on how big your project is, of course).

When loading individual files suddenly the network itself becomes the biggest bottleneck - the browser limits the number of parallel downloads (so you end up loading files sequentially) and there's the connection negotiation overhead for each resource. Together with the fact that it complicates r.js configuration I'd say on-demand loading should be considered only when the modules/libraries are really big and are only used by a tiny fraction of users.

The real benefit of AMD modules isn't actually asynchronous loading but code organisation - with neatly organised modules with well-defined dependencies it's much harder to write bad or untestable code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM