We write a lot of JavaScript.
Thus the bundling, compression, and minification of JavaScript is important to the speed and performance of modern websites. This is why I love and have been a big advocate of tools like Combres, and also why I was so excited to hear that such features were (finally) coming built in to ASP.NET MVC 4.
Introducing MvcBundleConfig
MvcBundleConfig is a very simple minimalist project I wrote to add configuration and debugging features to MVC 4's bundling framework, and achieves all 6 six of the goals listed below. It requires only MVC4 to run, and you need only add one config file to your project, one line of code to your application start, and you are off and running.
NuGet Package: https://nuget.org/packages/MvcBundleConfig/
Source on GitHub: https://github.com/tdupont750/MvcBundleConfig
Before we get to the demonstration at the bottom, let's review the needs and wants of a good minification framework.
What I NEED in a minification tool:
- Compress resources into single files.
- Multiple request take time and resources, neither of which are things that any of us have to spare. By compressing resources into single requests and can limit the overhead and load time on both our clients and our servers.
- Minify JavaScript and CSS content.
- Minification removes as many unnecessary white spaces and characters as possible from your resource files, reducing file size by up to 80% on average. When then compounded with gzip, we can reduce the file size another 50%. This means that our web applications can be brought down to clients 90% faster.
- Make use of both client and server side caching.
- Making 90% less requests is nice, and making those requests 90% smaller is even better, but only ever having to request or serve them once is the key to true performance. Unfortunately client and server caching can be a bit complex due to quirks of different technologies and browsers. A good minification framework should abstract these concerns away from us.
- Ability to turn off during debugging.
- As fantastic as everything that we have listed about is for a production website, it is a nightmare for a development website. Debugging JavaScript is no less complicated or time consuming than debugging C#, and we need to be able to use a debuggers and other client side tools that are inhibited by minification. A good minification framework must expose a debugging mode that skips compression pipeline.
What I WANT in a minification tool:
- Simple and dynamic configuration.
- I hate hardcoded configuration. It bloats my code, and it requires bin drops to deploy. Meanwhile I really like the ability to add simple configuration files to my site as often as I can. Config files are explicit, abstract, and can be updated at any time. Win.
- Take a few dependencies as possible.
- I mentioned above that I like Combres and it has a reasonably sized code base, unfortunately the fact that it's NuGet package pulls down half a dozen additional dependencies makes it feel quite heavy. The fewer dependencies a framework takes the better.
MvcBundleConfig Examples
Bundles.config
<?xml version="1.0"?>
<bundleConfig ignoreIfDebug="true" ignoreIfLocal="true">
<cssBundles>
<add bundlePath="~/css/shared">
<directories>
<add directoryPath="~/content/" searchPattern="*.css" />
</directories>
</add>
</cssBundles>
<jsBundles>
<add bundlePath="~/js/shared" minify="false">
<files>
<add filePath="~/scripts/jscript1.js" />
<add filePath="~/scripts/jscript2.js" />
</files>
</add>
</jsBundles>
</bundleConfig>
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
// Only code required for MvcBundleConfig wire up
BundleTable.Bundles.RegisterConfigurationBundles();
}
_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@* Call bundle helpers from MvcBundleConfig *@
@Html.CssBundle("~/css/shared")
@Html.JsBundle("~/js/shared")
</head>
<body>
@RenderBody()
</body>
</html>
In the Browser
NuGet Package: https://nuget.org/packages/MvcBundleConfig/
Source on GitHub: https://github.com/tdupont750/MvcBundleConfig
Enjoy,
Tom