Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Advanced Options of ASP.NET Bundling and Minification

DZone's Guide to

Advanced Options of ASP.NET Bundling and Minification

· ·
Free Resource
Server.MapPath is one of the most-used methods in ASP.NET. Lot of libraries/assemblies has used this method. The problem with using this method inside a library is that it will tie the users of this library with a ASP.NET intrinsic Server object that makes unit testing a little difficult. Fortunately, System.Web.Optimization have a special property called BundleTable.MapPathMethod which allows users to provide a fake method for unit testing.

There are some script files that you will never expect to add to a web page, for example, *.intellisense.js and *-vsdoc.js script files which are used by Visual Studio for intellisense. There are some other script files that you need to ignore during production, like, *.debug.js file. There are some other script files that you will not add during debugging/testing, for e.g, *.min.js and *.min.css files. Luckily, our optimization library will automatically takes care of these situation through BundleCollection.IgnoreList property. Here is the what this property contains by default,

public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
{
	if (ignoreList != null)
	{
		ignoreList.Ignore("*.intellisense.js");
		ignoreList.Ignore("*-vsdoc.js");
		ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
		ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
		ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
		return;
	}
	else
	{
		throw new ArgumentNullException("ignoreList");
	}
}

You can easily override this by using the BundleCollection.IgnoreList property in BundleConfig.cs file. Note: it is also important that you should be aware about a bug in the implementation of this. 

The ordering of scripts and css files is also very important. For example, if you have jquery.js and jquery.ui.js then jquery.js file should be appear before jquery.ui.js. Similarly for css, reset.css should appear first(if exist) and then normalize.css(if exist) and then the remaining ones. Fortunately again, our optimization assembly will also handle this scenario by using BundleCollection.FileSetOrderList property. You can also override this inside BundleConfig.cs file. Here is the what this property contains by default,

public static void AddDefaultFileOrderings(IList<bundlefilesetordering> list)
{
	if (list != null)
	{
		BundleFileSetOrdering bundleFileSetOrdering = new BundleFileSetOrdering("css");
		bundleFileSetOrdering.Files.Add("reset.css");
		bundleFileSetOrdering.Files.Add("normalize.css");
		list.Add(bundleFileSetOrdering);
		BundleFileSetOrdering bundleFileSetOrdering1 = new BundleFileSetOrdering("jquery");
		bundleFileSetOrdering1.Files.Add("jquery.js");
		bundleFileSetOrdering1.Files.Add("jquery-min.js");
		bundleFileSetOrdering1.Files.Add("jquery-*");
		bundleFileSetOrdering1.Files.Add("jquery-ui*");
		bundleFileSetOrdering1.Files.Add("jquery.ui*");
		bundleFileSetOrdering1.Files.Add("jquery.unobtrusive*");
		bundleFileSetOrdering1.Files.Add("jquery.validate*");
		list.Add(bundleFileSetOrdering1);
		BundleFileSetOrdering bundleFileSetOrdering2 = new BundleFileSetOrdering("modernizr");
		bundleFileSetOrdering2.Files.Add("modernizr-*");
		list.Add(bundleFileSetOrdering2);
		BundleFileSetOrdering bundleFileSetOrdering3 = new BundleFileSetOrdering("dojo");
		bundleFileSetOrdering3.Files.Add("dojo.*");
		list.Add(bundleFileSetOrdering3);
		BundleFileSetOrdering bundleFileSetOrdering4 = new BundleFileSetOrdering("moo");
		bundleFileSetOrdering4.Files.Add("mootools-core*");
		bundleFileSetOrdering4.Files.Add("mootools-*");
		list.Add(bundleFileSetOrdering4);
		BundleFileSetOrdering bundleFileSetOrdering5 = new BundleFileSetOrdering("prototype");
		bundleFileSetOrdering5.Files.Add("prototype.js");
		bundleFileSetOrdering5.Files.Add("prototype-*");
		bundleFileSetOrdering5.Files.Add("scriptaculous-*");
		list.Add(bundleFileSetOrdering5);
		BundleFileSetOrdering bundleFileSetOrdering6 = new BundleFileSetOrdering("ext");
		bundleFileSetOrdering6.Files.Add("ext.js");
		bundleFileSetOrdering6.Files.Add("ext-*");
		list.Add(bundleFileSetOrdering6);
		return;
	}
	else
	{
		throw new ArgumentNullException("list");
	}
}

Now, let's say that you have only registered the jquery-1.6.2.js(not jquery-1.6.2.min.js). The optimization framework will still pick the jquery-1.6.2.min.js instead of jquery-1.6.2.js during production. This is possible via BundleCollection.FileExtensionReplacementList property. Like others, you can also override this inside BundleConfig.cs file. Here is the what this property contains by default, 

public static void AddDefaultFileExtensionReplacements(FileExtensionReplacementList list)
{
	if (list != null)
	{
		list.Add("min", OptimizationMode.WhenEnabled);
		list.Add("debug", OptimizationMode.WhenDisabled);
		return;
	}
	else
	{
		throw new ArgumentNullException("list");
	}
}

For clearing all these settings, you just need to call BundleCollection.ResetAll method. This method will clear all the above lists and empty the registered bundles.  

The System.Web.Optimization framework also allows you to define a custom orderer for a bundle. You just need to implement the IBundleOrderer.OrderFiles method in a new class and set the Bundle.Orderer property. Here is an example.

 

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}