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 21:48

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.

查看更多
看淡一切
3楼-- · 2018-12-31 21:49

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.

查看更多
柔情千种
4楼-- · 2018-12-31 21:49

An easy way just Rename the .min file for example you have abc.min.css than just rename this file to abc_min.css and add this to your bundle. I know its not the right way to do it, but just a temporary solution. thanks and happy coding.

查看更多
何处买醉
5楼-- · 2018-12-31 22:02

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.

查看更多
像晚风撩人
6楼-- · 2018-12-31 22:04

Folks I would just keep it simply until Microsoft gets it act together

Try this

In RegisterBundles create this bundle for Kendo

bundles.Add(new StyleBundle("~/Content/kendo/css").Include(
                    "~/Content/kendo/2012.3.1121/kendo.common.min.css",
                     "~/Content/kendo/2012.3.1121/kendo.dataviz.min.css",
                      "~/Content/kendo/2012.3.1121/kendo.blueopal.min.css"
 ));

In _Layout.cshtml put this:

@if (HttpContext.Current.IsDebuggingEnabled)
 { 
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.common.min.css")"      
rel="stylesheet"  type="text/css" />
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.dataviz.min.css")" 
rel="stylesheet" type="text/css" />   
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.blueopal.min.css")"    
rel="stylesheet" type="text/css" />
 }
 else
 {
     @Styles.Render("~/Content/kendo/css")   
 }

This way we get best of bundles in Production and a reference in Debug

Fix it up when Microsoft fixes their MVC 4 Code

查看更多
明月照影归
7楼-- · 2018-12-31 22:06

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.*"));
查看更多
登录 后发表回答