MVC 4 beta: RegisterTemplateBundles and EnableDefaultBundles

After trying to figure out why the bundling of all my JavaScript skips some files (like Knockout.js, and History.js) I opened the reflector to find out more. It seems that the RegisterTemplateBundles just adds the default JavaScript libraries, like JQuery and MicrosoftAjax, hardcoded. This means that they are the “template” when registering the template bundle.

public void RegisterTemplateBundles()
{
    Bundle bundle = new Bundle("~/Scripts/js", new JsMinify());
    bool searchSubdirectories = false;
    bool throwIfNotExist = false;
    bundle.AddDirectory("~/Scripts", "jquery-*", searchSubdirectories, throwIfNotExist);
    bool flag3 = false;
    bool flag4 = false;
    bundle.AddDirectory("~/Scripts", "jquery.mobile*", flag3, flag4);
    bool flag5 = false;
    bool flag6 = false;
    bundle.AddDirectory("~/Scripts", "jquery-ui*", flag5, flag6);
    .
    .
    .
}

When calling EnableDefaultBundles a completely different thing happens: DynamicFolderBundles are created

public void EnableDefaultBundles()
{
    this.Add(new DynamicFolderBundle("js", JsMinify.Instance, "*.js"));
    this.Add(new DynamicFolderBundle("css", CssMinify.Instance, "*.css"));
}

If we try to figure out the DynamicFolderBundles we can see that code like this

var context = new BundleContext(new HttpContextWrapper(Context), new BundleCollection(), "~/Scripts/js");
System.IO.DirectoryInfo parent = new System.IO.DirectoryInfo(
    context.HttpContext.Server.MapPath(context.BundleVirtualPath)).Parent;


DynamicFolderBundle bundle = new DynamicFolderBundle("js", new JsMinify(), "*.js");

bundle.EnumerateFiles(context);

Results enumerating all the “*.js” files, which is exactly what we need. Frankly I think this is a bug in the beta since the global.ascx by default calls RegisterTemplateBundles instead of EnableDefaultBundles. This is important since when you add NuGet packages (like require.js) they will be added to the Scripts folder and won’t get bundled by default. I can see a reason for loading the extra (non template\common\default) JavaScript libraries with a custom bundle. But clearly I can see a better reason why to EnableDefaultBundles by default instead.

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com