Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundler not including .min files

The solution I originally posted is questionable (is a dirty hack). The tweaked behaviour has changed in Microsoft.AspNet.Web.Optimization package and the tweak does not work anymore, as pointed out by many commenters. Right now I cannot reproduce the issue at all with the version 1.1.3 of the package.

Please see sources of System.Web.Optimization.BundleCollection (you can use dotPeek for example) for better understanding of what you are about to do. Also read Max Shmelev's answer.

Original answer:

Either rename .min.js to .js or do something like

    public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
    {
        if (ignoreList == null)
            throw new ArgumentNullException("ignoreList");
        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);
    }

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.IgnoreList.Clear();
        AddDefaultIgnorePatterns(bundles.IgnoreList);
        //NOTE: it's bundles.DirectoryFilter in Microsoft.AspNet.Web.Optimization.1.1.3 and not bundles.IgnoreList

        //...your code
     }

Microsoft implies the following behavior (and I prefer to follow it in my projects):

short version

  1. You have both debug and minified versions of a script in your project under the same folder:
    • script.js
    • script.min.js
  2. You add only script.js to a bundle in your code.

As a result you will automatically have the script.js included in DEBUG mode and script.min.js in RELEASE mode.

long version

You can have also .debug.js version. In that case the file is included in the following priority in DEBUG:

  1. script.debug.js
  2. script.js

in RELEASE:

  1. script.min.js
  2. script.js

note

And by the way, the only reason to have a .min versions of your scripts in MVC4 is the case when the minified version can not be processed automatically. For example the following code can not be obfuscated automatically:

if (DEBUG) console.log("Debug message");

In all the other cases you can go with just a debug version of your script.


If all you have is a minified version of a file, the simplest solution I've found is to copy the minified file, remove .min from the copied file's name, then reference the non-minified file name in your bundle.

For example, let's say you purchased a js component and they gave you a file called some-lib-3.2.1.min.js. To use this file in a bundle, do the following:

  1. Copy some-lib-3.2.1.min.js and rename the copied file to some-lib-3.2.1.js. Include both files in your project.

  2. Reference the non-minified file in your bundle, like this:

    bundles.Add(new ScriptBundle("~/bundles/libraries").Include(
        "~/Scripts/some-lib-{version}.js"
    ));
    

Just because the file without 'min' in the name is actually minified shouldn't cause any issues (other than the fact it's essentially unreadable). It's only used in debug mode and gets written out as a separate script. When not in debug mode the pre-compiled min file should be included in your bundle.


I have found a good solution that works at least in MVC5, you can just use Bundle instead of ScriptBundle. It does not have the smart behavior of ScriptBundle that we don't like (ignoring .min, etc.) in this case. In my solution I use Bundle for 3d party scripts with .min and .map files and I use ScriptBundle for our code. I have not noticed any drawbacks of doing it. To make it work this way you will need to add original file e.g. angular.js, and it will load angular.js in debug and it will bundle the angular.min.js in release mode.


To render *.min.js files, you must disable BundleTable.EnableOptimizations, which is a global setting that applies to all bundles.

If you want to enable optimizations for specific bundles only, you can create your own ScriptBundle type that temporarily enables optimizations when enumerating files in the bundle.

public class OptimizedScriptBundle : ScriptBundle
{
    public OptimizedScriptBundle(string virtualPath)
        : base(virtualPath)
    {
    }

    public OptimizedScriptBundle(string virtualPath, string cdnPath)
        : base(virtualPath, cdnPath)
    {
    }

    public override IEnumerable<BundleFile> EnumerateFiles(BundleContext context)
    {
        if (context.EnableOptimizations)
            return base.EnumerateFiles(context);
        try
        {
            context.EnableOptimizations = true;
            return base.EnumerateFiles(context);
        }
        finally
        {
            context.EnableOptimizations = false;
        }
    }
}

Use OptimizedScriptBundle instead of ScriptBundle for bundles whose minified file should always be used, regardless of whether a non-minified file exists.

Example for Kendo UI for ASP.NET MVC, which is distributed as a collection of only minified files.

bundles.Add(new OptimizedScriptBundle("~/bundles/kendo")
        .Include(
            "~/Scripts/kendo/kendo.all.*",
            "~/Scripts/kendo/kendo.aspnetmvc.*",
            "~/Scripts/kendo/cultures/kendo.*",
            "~/Scripts/kendo/messages/kendo.*"));

For my case, I was using the (wonderful!) Knockout.js library which comes as Debug/Non versions:

"~/Scripts/knockout-{Version}.js"
"~/Scripts/knockout-{Version}.Debug.js"

To make this work, I included "Knockout-{Version}.js" (non-debug) in my Bundle and got the .debug. js file in Debug mode.