Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild minify & concat javascript, hash contents into filename

I've been using, up to this point, MS Ajax Minifier for squashing and combining my CSS and JS. My servers set very far future expires headers so i need a cache expiry strategy. Currently I'm manually versioning these items by altering the filename so they expire on each release.

I'd like to automate this somewhat, specifically by appending the hash of the file contents onto the squashed file name. Bonus points if we can then update some config XML (that exists in a different project) file with this updated filename

We are using TFS build server, so I guess this should be wrapped in an MSBuild task? Or can i just have it run as a pre/post build project step?

Would very much appreciate if someone has any knowledge in this area that they are happy to share.

Thanks

like image 853
WickyNilliams Avatar asked Jan 15 '13 14:01

WickyNilliams


2 Answers

Surely you could do it all from MSBuild:

  • Create your Ajax Minifier Manifest Files. I would keep these static and versioned.
  • Create one msbuild script containing:

    1 - Ajax Minifier Manifest Task to do the squashing and combining.

    2 - Hashing task. As you can see that's c# code. Change the code to read the output folder set on step '1' and change the name of the files, adding the calculated hash to each one.

  • Import the created msbuild script on your projects, or in your main build script, if you have one.

like image 83
Marcos Brigante Avatar answered Sep 28 '22 04:09

Marcos Brigante


Have you considered the bundling and minification feature in System.Web.Optimization?

http://blogs.msdn.com/b/rickandy/archive/2012/08/14/adding-bundling-and-minification-to-web-forms.aspx

http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

It should do you all you need without the msbuild maintenance hassle.

It doesn't touch the original files and alters the bundle hash per build. In addition, it can be programmatically turned off on the live site to allow debugging css and js on the live sites with all the original file names and comments retained.

We use it quite succesfully and I encourage UI programmers to split css and javascript files into smaller and more logically named files and leave rather more than little comments in them for documentation. The end user will always eventually get a single css and js which has a different name per build.

like image 33
Jani Hyytiäinen Avatar answered Sep 28 '22 04:09

Jani Hyytiäinen