Posted on 3/17/2010 5:31:01 PM by Justin Etheredge
A few days ago I pushed a new version of Bundler out to GitHub. The first change I made is that now JavaScriptBundler is just called Bundler. This is because of the title of this post. I want to use it with more than just JavaScript, so I decided to change it.
For right now, the command line version of Bundler is disabled. I want to get it in there and working again, but I am focused on the ASP.NET WebForms and ASP.NET MVC integrated versions directly. I changed the syntax a bit for the JavaScript bundling, so here is a quick example of how you would use it now.
Bundle some JavaScript:
<%= Bundle.JavaScript()
.AddJs("~/js/jquery-1.4.2.js")
.AddJs("~/js/jquery-ui-1.8rc3.js")
.RenderJs("~/js/combined.js") %>
Bundle some CSS:
<%= Bundle.Css()
.AddCss("~/css/jquery-ui-1.8rc3.css")
.AddCss("~/css/CodeThinked.css")
.RenderCss("~/css/combined.css") %>
CSS also has support for .less now. Any css file that ends in .less will be run through the .less compiler:
<%= Bundle.Css()
.AddCss("~/css/testdotless.css.less")
.RenderCss("~/css/combined.css") %>
The JavaScript minifying and combining is still being handled by JSMin. I'll make changes soon so that you can pick which minifier you want to use. Currently the css compressing is being done with the stock YUI compressor. I will also updated this soon so that you can pick which compressor you want to use.
How To Use Bundler
So, how do you get it working? Well, go to GitHub and download the beta from the downloads page. Then, all you need to do is reference the Bundler.Framework.dll from your website. Then just throw the code in that I provided above. You will just put the files in the order you want them in, and they will be minified or compressed and then combined into the an output file and written to disk where you specify. Once a file is written to disk, it is then cached and fed back over and over. The output will look like this:
<script type="text/javascript" src="/js/combined.js?r=C4F1EE2DBA3F667EB911E27BDD42216E"></script>
<link rel="stylesheet" type="text/css" href="/css/combined.css?r=87CABE31E3D5180E6FAAC6AB4BFE631E" />
Notice that we are putting a hash on the end of the path to the javascript and css. This way, when we change the files that go into the combined file, this will invalidate your browser's cache and you will get the new file fed up to your immediately. This way you don't need to change the file name in order to get browsers to pull the latest version of your file.
Where Do We Go From Here
For me, bundler is already to the point where it is extremely useful. I set out to create a very simple framework that I could leverage to combine my css and javascript without a lot of fuss. Right now I am considering a few other features for future versions, but I want to make sure that I keep things easy and simple, so I'm not entirely sure what new features I'll throw in. I hope that you'll go check it out, and let me know if you use it, how you use it, and what your thoughts are.