Wednesday, November 30, 2011

Bundling and Minification in .NET 4.5

A minified being
A minified being

.NET 4.5 (WebForms and MVC) will come with a cool new feature to help improve page load times from your web applications. CSS and JS files will be able to be bundled and minified at the folder level.

See ScottGu's post here, and a quick (1 min) video runthrough below (source).

This will leave your working files unaltered, and in their separate logical locations. But it will remove whitespace, use minify rules, and combine like files in response to browser requests.

One thing that isn't mentioned is cacheing, i.e. presumably the server caches the bundled files rather than perform the bundle operation every time a page is assembled. Will have to see how that works when 4.5 is released.

By default the bundled files within a folder are ordered alphabetically, but this can be overridden by customising Bundle objects. You also have the option of specifying CSS or JS syntax to convert to, including specifying custom rules. Pretty cool.