Advanced Options of ASP.NET Bundling and Minification

        Introduction:


                    Rick Anderson has written a great introduction to ASP.NET Bundling and Minification at here. In addition to the options presented by Rick, there are some other features in System.Web.Optimization framework which is also important for developers to understand in order to make thier application performance and architecture good. In this article, I will show you some of the advance stuff of ASP.NET Bundling and Minification.


        Description:


                    Server.MapPath is one of the most used method in ASP.NET. Lot of libraries/assemblies has used this method. The problem of using this method inside a library  is that it will tie the users of this library with ASP.NET intrinsic Server object which 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 on 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 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



        Summary:


                    In this article, I showed you some of the advanced options of System.Web.Optimization framework. Hopefully you will enjoy my this article too.



Published Sunday, September 30, 2012 11:54 PM by imran_ku07

Comments

# re: Advanced Options of ASP.NET Bundling and Minification

Monday, October 8, 2012 3:29 AM by Peter

We've been into this last week at domlia.com. Thanks for the examples.

# re: Advanced Options of ASP.NET Bundling and Minification

Tuesday, October 9, 2012 10:23 AM by johhny iv young

good article, info useful. thank a lot

# re: Advanced Options of ASP.NET Bundling and Minification

Tuesday, October 9, 2012 4:59 PM by Aamir Akhtar

Imran i like your blog but tell where from you learn these things....?

# re: Advanced Options of ASP.NET Bundling and Minification

Wednesday, October 10, 2012 1:16 AM by imran_ku07

@Aamir, http://asp.net/learn is a great resource for getting started.

# re: Advanced Options of ASP.NET Bundling and Minification

Wednesday, December 12, 2012 3:25 AM by awj

Given the comparative lack of documentation for this feature, having a page like this is very useful - I'd love to see more info like this.

One shortcoming I have noticed is that I am unable to set the 'media' attribute on CSS bundles. This prevents us from building a bundle for screen and another bundle for print. In the context of building bundles for stylesheets there aren't many options which need to be catered for, but for the work I do this attribute really is required.

# re: Advanced Options of ASP.NET Bundling and Minification

Wednesday, December 12, 2012 4:02 AM by imran_ku07

@awj, see this link stackoverflow.com/.../make-asp-net-bundling-specify-media-screen-for-css-bundle