Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify jQuery based js files

Tags:

We are using jQuery in our project. We have numerous custom javascript files in our web-app that have UDFs utilizing the jQuery features. We need to reduce the size (as a part of performance improvement activities) and I am looking for a reliable 'minifier' for these files (it would be great if the same tool could minify the CSS files too)

We tried JSLint and JSMin - but JSLint does not complete and throws many exceptions as soon as it encounters jQuery code.

like image 787
Vini Avatar asked May 08 '09 18:05

Vini


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.

How NPM minify js file?

js . Simply copy + paste the code starting with cat, including the EOT on the last line, and press . Use the command minify followed by the path to and name of the js file intended to be minified. This will minify the code and output it to the screen.

Should I minify CSS and JS?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.


2 Answers

The YUI Compressor is a tool I use, it compresses both JS and CSS well, and it is written in Java (so you can work it into a build process via ant).

Someone's even made an online version of it.

like image 69
Dan Lew Avatar answered Oct 19 '22 17:10

Dan Lew


There's also a .NET port of YUI Compressor which allows you to:-

  • intergrate the minification/file combining into Visual Studio post-build events
  • intergrate into a TFS Build (including CI)
  • if you wish to just use the dll's in your own code (eg. on the fly minification).

because this is a port of the (original) java version YUI Compressor, which a few peeps mention above, it should give you the same results BUT all in the .NET environment -- no need for java.

HTH.

like image 39
Pure.Krome Avatar answered Oct 19 '22 17:10

Pure.Krome