Package and Compress with Google Closure via MSBuild 0
The Problem
So our problem is we are building a library of JavaScript files that we use all over the place. I’ve been pushing, on the project where I’m the sole developer, to wrote 99% of the JavaScript as jQuery plugins. This works great because we can compile that 99% into a single file, or to reverse the perspective, we can split that huge file into smaller, more manageable and logical files.
For example, we have a file called jquery.mycompany.core.js which does a variety of mundane tasks. The file is huge, but unlike C# code, it’s huge for a reason. We want the browser to make as few requests as possible so there is less handshaking, less overhead, and faster page loads.
Previously we had several files and we manually combine them to one and then run it through some online compressing tool once we get close to production. This technically worked, but changes found a way of making their way into the huge file and the segmented files become out of date and obsolete.
The Hack
So here was my plan, take a list of file, concatenate them, and then compress them. Simple enough right? Well, it actually isn’t that bad
<ItemGroup>
<JavaScriptFiles Include="scripts\jQueryPlugins\Custom\core\**\*.js" />
</ItemGroup>
<Target Name="Default" DependsOnTargets="BuildAndCompressOrdersJavaScript">
</Target>
<Target Name="BuildAndCompressJavaScript">
<ReadLinesFromFile File="%(JavaScriptFiles.Identity)" >
<Output
TaskParameter="Lines"
ItemName="lines"/>
</ReadLinesFromFile>
<WriteLinesToFile File="scripts\jquery.mycompany.core.uncompressed.js"
Lines="@(Lines)"
Overwrite="true" />
</Target>
Not so bad right? Lastly, just add a bit of Google Closure. I just shell out and run it from the command line here.
<Exec Command='java -jar ..\compiler.jar --js scpts\jq.myco.core.un.js > scpts\jq.myco.core.js' />
I shortened some paths and filenames there so it would fit better in the blog’s column, but you get the idea.
I threw this into a post build event even though I could have added it to the csproj directly. The post build event was easier and it avoids those nasty warnings when you open the csproj.
The only issue I see with it right now is it is not as portable as I would like it to be. You pretty much company and past the build steps if you need to use it again. This is my major gripe about MSBuild. It’s difficult to create functions you repeat in the build.
This is a total cheapskate way to accomplish this task. I should be creating a nice parameterized MSBuild task, or even better, dump MSBuild for Rake. MSBuild and I acknowledge we are never going to be friends and have come to a professional arrangement. MSBuild does nothing to accommodate me and I do as little as possible with it.

