JS and CSS minification with YUI
by Pascal Opitz on July 25 2008, 10:49
At the moment I am working on a large website project for UKTV, where for maintainability reasons I keep files separated and pretty verbose.
Every box and page type would get its own CSS file, and all would be included through one big CSS, which looks somehow like this:
@import url('../global/tools.css');
@import url('../global/foo.css');
@import url('../global/bar.css');
@import url('../content/foo.css');
@import url('../content/bar.css');
@import url('../sidebar/foo.css');
@import url('../sidebar/bar.css');
The same would go for the JS files, which were broken down into class files and then included one by one:
<script type="text/javascript" src="/js/project.js"></script>
<script type="text/javascript" src="/js/project/foo.js"></script>
<script type="text/javascript" src="/js/project/bar.js"></script>
All together this generated more than different 35 hits, something that inevitably makes the rendering of an HTML page very slow.
The solution came to me when my friend Simon from Seventytwo highlighted the YUI compression tools to me, first just as a Textmate bundle, but then he pointed out to me that there is a jar file for it as well.
20 minutes and one little shellscript later I have one CSS files and one JS file, all nicely minified, plus an IE6 and IE7 stylesheet in conditional comments.
cat
./templates/css/global/tools.css
./templates/css/global/layout.css
./templates/css/global/foo.css
./templates/css/global/bar.css
./templates/css/global/flyouts.css
./templates/css/content/foo.css
./templates/css/content/bar.css
./templates/css/sidebar/foo.css
./templates/css/sidebar/bar.css
> ./templates/css/shared_concated.css
java -jar yuicompressor-2.3.5.jar
./templates/css/shared_concated.css >
./templates/css/shared_minified.css
rm ./templates/css/shared_concated.css
cat
./templates/js/project.js
./templates/js/project/foo.js
./templates/js/project/bar.js
> ./templates/js/project_concated.js
java -jar yuicompressor-2.3.5.jar
./templates/js/project_concated.js >
./templates/js/project_minified.js
rm ./templates/js/project_concated.js
Neat, me thinks!
Comments
find lib/jquery -type f -name ”*.js”
-exec cat {} + > jquery-bundle.js
Once you’ve run that command you can then pass the concatenated file on to YUI Compressor.
by Simon Plenderleith on August 10 2008, 11:47 #
by Ali Farhadi on August 26 2008, 01:12 #
by Pascal Opitz on August 22 2008, 09:50 #
by Gerard on November 26 2009, 02:28 #