Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you bundle multiple AMD modules in a single file?

Tags:

javascript

amd

AMD seems to be the best practice to load javascript modules as-needed.

This should work great on large web-apps where users only use a fraction of the available functionality.

I've read about the optimizer, which concatenates all required modules into a single file, and I've read about no optimization, i.e. loading each module with a async request.

Both don't seem to fit this use-case: loading every module with a request might quickly result in a large number of request, whereas optimizing forces you to download all the code.

Is there a way to bundle multiple modules into a single file?

like image 259
markmarijnissen Avatar asked Aug 15 '12 21:08

markmarijnissen


People also ask

What is module bundling?

What is module bundling? On a high level, module bundling is simply the process of stitching together a group of modules (and their dependencies) into a single file (or group of files) in the correct order.

How is JavaScript bundled?

JavaScript bundling is an optimization technique you can use to reduce the number of server requests for JavaScript files. Bundling accomplishes this by merging multiple JavaScript files together into one file to reduce the number of page requests.

What is bundling in web development?

Bundling is the process of resolving the web of dependencies and merging (or 'packaging') the files (or modules) into optimized bundles for the browser, with the goal of reducing the number of requests for files when a user visits a web page.


2 Answers

Yes. Generally dividing application to so many files and loading them with AMD is only good for development. It helps to keep the code clean and understandable; it's quite logical that each module contains views, models, controllers and each of them is a separate file.

Yet that large number of request doesn't make sense in production. Thus you should use optimizer to compile and minimize files into one (or a few) to improve performance and user experience.

If you're using RequireJS please refer to http://requirejs.org/docs/optimization.html#wholeproject — it explains how to use r.js tool provided by RequireJS.

For example if my application was consisting of public area, administration and a very complex sign up form each build up with hundreds views/models/controllers I'd probably compile my code into just 4 files: common, public, admin, sign_up. Then appropriate file would be asynchronously loaded when user entered specific zone.

like image 66
Michał Miszczyszyn Avatar answered Oct 31 '22 20:10

Michał Miszczyszyn


RequireJS supports this use-case with their optimization tool.

With the proper configuration in build.js, it can be configured to bundle multiple modules in multiple files.

Their mapping can be defined in build.js as shown in this example project

like image 28
markmarijnissen Avatar answered Oct 31 '22 19:10

markmarijnissen