Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use require.js if I still concenate all files on build?

It sure makes a lot of sense to write modular, independent testable code - especially for big projects.

But what difference does using require.js/amd make in a big project where I still need to concenate & minify my project on build?

like image 730
Industrial Avatar asked Dec 21 '12 15:12

Industrial


People also ask

Why we use RequireJS?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.

What is better to load one complete JavaScript file on all pages or separate files based on different pages?

It is best to keep separate files or include all files in one file Javascript? To avoid multiple server requests, it is better to group all your JavaScript files into only one. If you're using C#.Net + ASP.Net, you can bundle your files — it is a very good way to compress your scripts.

Should you have multiple JavaScript files?

You can write your JS in separate files, but when it comes to deploying, it's more efficient to minify them all into a single file. For each script you load in your browser, you make a round-trip to the server, so it makes sense to minimize those.

Why do we have to separate the JS file from HTML?

It separates HTML and code. It makes HTML and JavaScript easier to read and maintain.


2 Answers

I would like to preface my response with saying that I do think Require.JS is an utterly useless framework. It over-complicates a fairly simple concept.

That being said, dependency loading is VERY useful when it comes to writing platform independent code.

For example, say you wanted to develop a web application that you can also port into Apache Cordova for mobile apps and into AppJS for desktop apps. You wouldn't want to rewrite all of your business logic, so it makes sense to build a bootstrap that loads dependencies dynamically to adapt the software to multiple architectures. That way you only have one product, which is able to run on a variety of platforms. Add in the usage of NodeJS for server side script and you can not only write front end software, but back end with the exact same code.

Modularity helps a lot with cross-platform projects, but as I said: Require.JS really isn't that useful. I have found it to be overly complicated. Instead, I just use an object built around jQuery's getScript function that contains a registry of all loaded packages so that a dev doesn't try to load a package that has already been loaded (larger projects).

like image 185
Andrew Rhyne Avatar answered Oct 13 '22 00:10

Andrew Rhyne


There are a number of advantages to require.js, such as:

  • Conditional loading
  • Asynchronous loading
  • Logical modules
  • Abstraction of nested dependencies
  • Multiversion support
  • Automated minification
like image 40
Wolfgang Stengel Avatar answered Oct 13 '22 00:10

Wolfgang Stengel