2

I know what you're all thinking, can't he just use all 3? Yes, I could, but my question is this: do they not cancel each other out?

tl;dr

  1. Does bundling make RequireJS obsolete?
  2. Does bundling make CDNs obsolete?
  3. If I have to make a choice between bundling or RequireJS+CDN, how do I determine which to use? Is it best practice to CDN common files, and bundle uncommon? What about SPA files?

The long version

For example, we bundle files for fewer HTTP requests. Whereas RequireJS is used so that files can be loaded dynamically and asynchronously, as they're needed.

So if we bundle all our JS files into 1, is RequireJS even needed?

Also, we use a CDN so the browser can access a cached version, so if the user visits 5 websites, all of which use a popular jQuery CDN - i.e. http://cdn.com/jquery.min.js - the browser actually needs to make no HTTP requests for this file on the last 4 sites, which is much faster than that file getting bundled. If those sites were to instead each access a unique bundled file from the CDN - specific to their needs - the odds of a cache "hit" become much lower. And, since the odds of a "hit" are lower, the file is less likely to be cached for even the people using that site.

So does it really help for a bundled file to even be on a CDN?

Community
  • 1
  • 1
Matt Johnson
  • 368
  • 2
  • 10

2 Answers2

2

Whereas RequireJS is used so that files can be loaded dynamically and asynchronously, as they're needed.

Many developers use RequireJS to have a clean way to separate concerns without the need for loading modules dynamically. I have a few applications and libraries developed with RequireJS. One of them is a library that does not use the dynamic loading capabilities at all and would not benefit from using them. In almost all use-case scenarios for this library, it should be loaded as a single bundle, and not as a collection of modules, one-by-one.

This brings me to another thing: the decision is not between bundling or RequireJS. You can use r.js (RequireJS' optimizer) to bundle your RequireJS modules into a single file for production. This could include 3rd party modules if you want. Or you could leave them out to be served from a CDN. An application can be optimized to a single bundle, or a series of bundles. As any other optimization question, what should be done depends on the architecture of the specific application being considered.

The rest of your question is wholly opinion-based. I've seen detailed arguments supported by statistics that you should use a CDN, that you should not use a CDN, and everything in between. It is an optimization question, which, again, depends on the specifics of the application being considered.

Louis
  • 146,715
  • 28
  • 274
  • 320
0

So if we bundle all our JS files into 1, is RequireJS even needed?

Not all of it. You might be interested in almond.

A replacement AMD loader for RequireJS. It provides a minimal AMD API footprint that includes loader plugin support. Only useful for built/bundled AMD modules, does not do dynamic loading.

ekuusela
  • 5,034
  • 1
  • 25
  • 43