Bundler not including .min files

2018-12-31 21:32发布

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?

9条回答
弹指情弦暗扣
2楼-- · 2018-12-31 22:07

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
     }
查看更多
无与为乐者.
3楼-- · 2018-12-31 22:09

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.

查看更多
人气声优
4楼-- · 2018-12-31 22:10

Bundler have a lot of benefits check this page BUT:

The Microsoft MVC4 Platform Considers that you have at least both minified version & unminified version for each Script or Style Bundle(another files like Debug and vsdoc are available too). So We have trouble in situations that there is only one of these files.

You can change debug state in web.config file permanently to handle output:

<compilation debug="false" targetFramework="4.0" />

See the output changes! File filtering changed. To meet the purpose we must change ignore case filtration that will change application logic!

查看更多
登录 后发表回答