Webhelpers and Javascript Minification
Here's a practical idea which I have in use in a project of mine.
Most of you probably use some javascript library or have some javascript files that you use in your pylons application.
There's also some talk going on related to javascript files compression, and more recently, minification.
While I'm developing, I like to use the normal(non compressed) javascript files, because it helps on debugging. But for production, using compressed javascript files reduces page load times.
Pylons Webhelpers comes with a useful function to help you include javascript files on your templates,
javascript_include_tag
.
The idea I had was at first, make that function provide the minified version of the JS file if we're not running our application in debug mode. If we are on debug mode, server the normal file.
So, I added a minified option to that function above, which does just that.
After a while, I wanted to know how I could minify my own javascript files, and I found out about JSMin. There's also a python version of it.
That's when I also thought, hell, I could minify my own javascript files upon request, and even cache that so the routine only runs once.
Ok, the first thing you should do, is download a copy of the python version of JSMin, save it inside the lib directory of your project, and, on your project helpers file, have the following code:
=
=
return
=
=
=
=
=
=
=
return
=
=
=
=
= +
return
= =
=
=
return
Now, on your templates, all you have to do is:
${ h.javascript_include_tag('/js/jquery-latest.js', minified=True) }
Hope the has helped you in some way. Happy coding and Happy New Year!!!!
Update on 2008/01/02
Changed function to also combine the several JS files into a single one to reduce requests,
just pass combined=True
.