Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minifying RequireJS Javascript codebase to a single file

Tags:

I am new to RequireJS and maybe this is an already discussed issue but I could not find a clear answer or opinion about this.

I have an application working with RequireJS. It has a lot of JavaScript files loaded by Require as they are needed. Working as expected.

Looking to Fiddler inspector I could see that all the files were loaded when the application started. I believe that Require has made a deep traversal of all the references to JavaScript files and loaded them all in the beginning.

If this is the way it works I believe that it would be better to generate a single file, minify it, and load it using a <script> html tag.

If I generate one single file with all scripts, and pre-load them with <script> will RequireJS load them again?

Am I doing something wrong?

In a production environment what could be the better solution?

Thanks in advance.

like image 698
mvbaffa Avatar asked Jan 15 '13 12:01

mvbaffa


People also ask

How do I minify a JavaScript file?

To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

Should you minify JavaScript?

Minifying strips out all comments, superfluous white space and shortens variable names. It thus reduces download time for your JavaScript files as they are (usually) a lot smaller in filesize. So, yes it does improve performance. The obfuscation shouldn't adversely affect performance.

What is the main purpose of Minifying JavaScript files?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

Why should you minify your JavaScript scripts before publishing them?

These characters include whitespaces, line breaks, comments, and block delimiters which are useful for us humans but unnecessary for machines. We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.


1 Answers

What you want is the RequireJS Optimizer.

The optimizer does the following (from the docs):

  • Combines related scripts together into build layers and minifies them via UglifyJS (the default) or Closure Compiler (an option when using Java).
  • Optimizes CSS by inlining CSS files referenced by @import and removing comments.

Here's one article on the topic for further reference.

Have a look also at r.js, the command line tool which includes the optimizer. Using r.js, you can generate an optimized build from an application file main.js with just:

node r.js -o build.js 

where build.js is a requirejs config file containing your build profile (baseUrl, paths, etc.).

like image 88
Chris Salzberg Avatar answered Sep 29 '22 09:09

Chris Salzberg