Out of the box

Personal rants

Minify and Concatenate JavaScript plus CSS files

leave a comment »

Yesterday I wrote this little Makefile to minify and concatenate multiple JavaScript and CSS files. It depends on the YUI Compressor JAR. :-)

Since the browser can open at most 5 concurrent connections to the same host, and I had a web application depending on 20+ of these files, the first request was taking too long.

The only solution I saw was minifying and concatenating them to serve only 2 files (1 JS + 1 CSS). And that’s what I did.

Here’s how it works:

$ make
[css] static/css/first.css
[css] static/css/second.css
[css] static/css/third.css
[css] static/css/and_so_on.css
[tag] @import url("static/css/all.css");
[js] static/js/first.js
[js] static/js/second.js
[js] static/js/third.js
[js] static/js/and_so_on.js
[tag] <script type="text/javascript" src="static/js/all.js"></script>
Done.

Simple huh?
Now, replace those bunch of includes, and be happy!

Advertisement

Written by jweyrich

July 31, 2010 at 7:33 pm

Posted in Web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.