Bundler not including .min files

100,085

Solution 1

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
     }

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.*"));
Share:
100,085

Related videos on Youtube

Fatal
Author by

Fatal

Updated on September 15, 2022

Comments

  • Fatal
    Fatal over 1 year

    I have a weird issue with the mvc4 bundler not including files with extension .min.js

    In my BundleConfig class, I declare

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/Scripts/jquery")
            .Include("~/Scripts/jquery-1.8.0.js")
            .Include("~/Scripts/jquery.tmpl.min.js"));            
    }
    

    In my view, I declare

    <html>
        <head>
        @Scripts.Render("~/Scripts/jquery")
        </head><body>test</body>
    </html>
    

    And when it renders, it only renders

    <html>
        <head>
             <script src="/Scripts/jquery-1.8.0.js"></script>
        </head>
        <body>test</body>
    </html>
    

    If I rename the jquery.tmpl.min.js to jquery.tmpl.js (and update the path in the bundle accordingly), both scripts are rendered correctly.

    Is there some config setting that is causing it to ignore '.min.js' files?

    • Eric J.
      Eric J. over 11 years
      I am using MVC 4 bundler and it is including .min.js files.
    • Fatal
      Fatal over 11 years
      the RTM version or the RC? it was working ok in the RC for me too
    • Pieter Germishuys
      Pieter Germishuys over 11 years
      The idea is that working in debug mode, that the "dev" version without minification will be used and when you are in non-debug mode, that the minified version is picked. To see it in action, change your web.config debug value from true to false.
    • Fatal
      Fatal over 11 years
      in some cases you dont have the non-minified version of the script though. I could possibly understand it if both files existed.
    • nothingisnecessary
      nothingisnecessary about 9 years
      It's a bummer that it works like this by default... sure, the file may already be minified, but I think Microsoft failed to see the benefit of adding pre-minified scripts to bundles for cache-busting purposes (the nice little v parameter hash that gets added to the url, and changes when file contents change)
  • Dan B
    Dan B over 11 years
    Thanks from me too - so adding this to my MVC4 gotcha list :)
  • Giedrius
    Giedrius over 11 years
    this was long wtf moment, they could have been adding those min files commented out with reason or something to result, now whole hour wasted wondering who is stealing my script files from output.
  • danmiser
    danmiser over 11 years
    Per Fatal's comment in the OP, this solution will end up rednering duplicate references for both the minified and regular versions if they both exist (e.g. jquery).
  • Eonasdan
    Eonasdan over 11 years
    nice explanation however, the OP's problem was that some scripts (jquery.tmpl) don't have, didn't come with, didn't get downloaded the non-min version of the file. The bundler ignores the script files altogether if it doesn't have a non-min version in debug mode
  • Diego
    Diego over 11 years
    Agree, nice explanation but it does not answer at all OP's question.
  • user981375
    user981375 over 11 years
    Short version didn't work for me. "script.js" would be included in both of these release types. I toggled DEBUG/RELEASE and (when I looked at the source) 'script.js' was the one selected/rendered.
  • Max Shmelev
    Max Shmelev over 11 years
    user981375, you also need to set <compilation debug="false" /> in the web.config file
  • GONeale
    GONeale over 11 years
    I don't like the idea of doing this, but I have no other option, the library I am using didn't provide anything but the .min.js :(
  • Fatal
    Fatal over 11 years
    I'm not a fan of this proposed solution at all. You're now having to maintain the set of scripts in (at least) two places
  • Adaptabi
    Adaptabi about 11 years
    M$, when somefile.min.js is explicitely specified, or when just the .min.js file exists, then only include the .min.js file! Otherwise just apply the current behavior!
  • James Reategui
    James Reategui about 11 years
    I prefer this answer because it is a "best practices" approach and explains how the bundler's default rules can be followed to achieve the same goal.
  • Oliver
    Oliver about 11 years
    This pretty much defeats the purpose of the bundling, don't you think?
  • J.P.
    J.P. almost 11 years
    Incredible that the actual class is called "IgnoreList", yet it derives straight from Object. No LINQ, no iteration. - msdn.microsoft.com/en-us/library/…
  • Chad Levy
    Chad Levy almost 11 years
    Bundling already works this way. If you run the project in debug mode each CSS file is loaded individually, however when you run in release they're combined and minified into one. The code within the if block is identical to what's rendered on the page automatically in debug mode.
  • MoSs
    MoSs over 10 years
    Adding debug="false" still doesn't work for me. min.js files are still not included even though I clear the IgnoreList as well...
  • jmrnet
    jmrnet almost 10 years
    I also had to add: bundles.DirectoryFilter.Clear();
  • Mike Cheel
    Mike Cheel almost 10 years
    Doing the code above did nothing for me. After calling Clear() on bundles.DirectoryFilter did things start working (without clearing the ignore list).
  • mykola.rykov
    mykola.rykov almost 10 years
    I can reproduce it with Microsoft.AspNet.Web.Optimization 1.0.0, and it is fixed at v. 1.1.3.
  • Buthrakaur
    Buthrakaur over 9 years
    Is it possible to add custom transformation logic (IItemTransform) for *.min.css files? The CssRewriteUrlTransform transformation doesn't seem to get invoked on *.min.css files, so just a presence of *.min.css file breaks URL references (they're not being transformed).
  • Anirudha Gupta
    Anirudha Gupta over 9 years
    Everytime nuget download the file, you need to rename then.
  • R K Sharma
    R K Sharma over 9 years
    I know its not the right way to do it, but just a temporary solution
  • Anirudha Gupta
    Anirudha Gupta over 9 years
    put this line are working for me // BundleTable.EnableOptimizations = true;
  • Steven Liekens
    Steven Liekens about 8 years
    It doesn't seem to work when optimizations are disabled (ScriptTable.EnableOptimizations = false). Script paths that contain a pattern are not rendered (e.g. ~/Scripts/thirdpartylib/*.js). It does work when EnableOptimizations = true, but so does ScriptBundle.
  • Ilya Chernomordik
    Ilya Chernomordik about 8 years
    I use it with false as well (during development) and I don't have problems that you describe, so it might be something else that's affecting it for you.You also need to do IncludeDirectory instead of include for the pattern to work.
  • Steven Liekens
    Steven Liekens about 8 years
    Are you sure it is rendering the ".min.js" version of your file? Are you using bundles from the namespace System.Web.Optimizations or from the nuget package Microsoft.Web.Optimizations?
  • Ilya Chernomordik
    Ilya Chernomordik about 8 years
    I use System.Web.Optimization and I checked that it renders min version, but I have figured out I do not use IncludeDirectory in fact, but it would be strange if that did not work with min version though... As IncludeDirectory is not enough for me I do a custom scan and add all the filter using Include, so might be that the pattern and IncludeDirectory does not work properly there, though it's a bit strange.
  • Steven Liekens
    Steven Liekens about 8 years
    The problem only exists when including files using a wildcard pattern. Pattern matching doesn't work for *.min.js files. Not even when using Bundle instead of ScriptBundle.
  • Ilya Chernomordik
    Ilya Chernomordik about 8 years
    You should do *.js, not *.min.js, the whole point is that it figures out itself. Probably that is the reason?
  • Steven Liekens
    Steven Liekens about 8 years
    No, the problem is two-fold: only a .min.js file exists, and the *.js pattern does not match files ending in .min.js.
  • Ilya Chernomordik
    Ilya Chernomordik about 8 years
    You can try adding the .js file in addition to the .min.js file to the same folder. That might fix it. I think it ignores all the .min.js files alltogether. As .min.js never handled on their own, but only as a part of original file when optimization is turned on. Sounds reasonable to have both files since one is used for development and another for production.
  • Steven Liekens
    Steven Liekens about 8 years
    That's assuming that the non-minified file is available and that you are licensed to use it. That's not a given for many libraries and plugins on the internet.
  • Ilya Chernomordik
    Ilya Chernomordik about 8 years
    Then just rename you .min.js to .js since you will never have access to the full version :) Another option is to copy the file and make .js. But I see the problem, but I never used any libraries that are not available in both (or at least unminifed) version, so never had this problem.
  • user2211290
    user2211290 over 7 years
    Minification automatically happens with bundling when debug mode is set to false in web.cong . Even debug mode we cab force to bundle using or bundletable.enableoptimizations = true in bundleconfig.cs
  • Felipe
    Felipe over 6 years
    I came across some missing scripts in a production publish action, and thinking it was an MVC issue, I found this answer.. lo and behold, kendo is involved. Having worked with it for too long, I do anything to get away from kendo
  • Steven Liekens
    Steven Liekens over 6 years
    @Felipe I was in the same situation which is why I wrote this answer. Kendo is horrible. I hope my example is helpful.