I have been trying to find an answer why webpack cares about module loading on the backend. Is there a reason why this may be needed? Does JSPM do backend module loading as well?
-
Can you clarify what you mean by "loading in/on the backend?" Are you referring to module bundling perhaps? – ebpa May 24 '16 at 05:05
-
Is "Why pre-bundle JavaScript code for the client?" a fair rephrasing of your question? – ebpa May 25 '16 at 23:13
-
Actually this will be the better rephrasing of the question: why care about bundling javascript for nodejs? – Joe Saad May 27 '16 at 17:31
-
You hadn't mentioned Node until now, but 1,3,4 and more tenuously 2 (depending on where/how code is run) still apply. – ebpa May 27 '16 at 17:39
2 Answers
Assuming your first question is along the lines of "Why pre-bundle JavaScript code for the client?"
There are many reasons for module bundling. A few:
- Simple file aggregation: Bundling related code makes many tasks easier / more intuitive. Instead of deploying a large directory tree of files after bundling those files may instead be a single bundle file.
- Loading Performance: Individually loading dependencies that are in separate files on the client side has historically been very slow. Each file must be parsed and evaluated separately and depending on the module system used may incur considerable delay while waiting for dependencies to be discovered and loaded.
- Media type abstraction: Bundlers typically allow methods for bundling non-JavaScript content. Including assets like images and stylesheets is convenient and encourages explicit/clear dependency by parts of your application that use them.
- Tree shaking: By analyzing dependency among modules and code it's often possible to selectively include what is needed by the application and reduce the size of your overall code base. This isn't inherently a characteristic of bundling, but is commonly done because there is some notion of a build step.
Regarding your second question:
JSPM does offer this functionality. This is can be done on the command line with the jspm bundle
command.

- 1,171
- 1
- 12
- 31
The simplest reason is for performance. Opening a file and closing a file is a slower processes than the time it takes to send the file (stream) so the fewer open and close file operations the faster the server can send the files requested. So by reducing the number of files that make up a javascript/web project the faster the browser will finish getting the files and start processing them for the end user.
The things that a good build process can do for you web project can go beyond simply adding all your Js files together as tools such as JSPM can also bring css and html files together into one bundle.js file further adding to your end-user experience.

- 31
- 3