Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minified script only in MVC4 BundleConfig

I am adding the following ScriptBundle in BundleConfig:

    bundles.Add(new ScriptBundle("~/bundles/javascript").Include(
        "~/Scripts/jquery-1.*",
        "~/Scripts/load-image.min.js",
        "~/Scripts/bootstrap.*",
        "~/Scripts/bootstrap-image-gallery.*",
        "~/Scripts/my.global.js"));

This is referenced at the end of my _Layout.cshtml as:

@Scripts.Render("~/bundles/javascript")

When debugging I notice that the output of this script rendering is:

<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/bootstrap-image-gallery.js"></script>
<script src="/Scripts/my.global.js"></script>

Notice the load-image.min.js script is missing? What I want is to use that same minified script whether I'm debugging or not. Under release conditions the script is included in the bundled JS file.

I assume it's seeing the 'min', looking for an un-minified version, not finding one, then deciding what's best is to ignore it entirely. Brilliant. If I make a copy of load-image.min.js, call it load-image.js and then reference it in BundleConfig as "load-image.*" I find it is included in both configurations but what's the point of having to do that?

I assume I'm missing something here. I don't have the un-minified version and I frankly don't care about it. It's used by my Bootstrap image gallery plugin and nothing else. Any ideas out there?

like image 836
Tom Troughton Avatar asked Oct 26 '12 13:10

Tom Troughton


People also ask

How do you override the bundling setting of web config?

To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.

HOW include script in MVC application?

Go to Views -> Shared -> _Layout. cshtml file and add the render code. Make sure to register the custom javascript file after the jquery bundle since we are going to use jquery inside our js file. Otherwise we will get a jquery error and also register this before the script RenderSection.

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.


2 Answers

This behavior has been improved (fixed) in the 1.1.0-alpha1 release. We moved all of the old default ignore list entries into a new DirectoryFilter ignore list that are only used when including search patterns like *.js which was the origional intent for this functionality. As a result this should no longer be an issue when you are including individual files explicitly.

Note: the one place this might still be an issue is if you try to include something like jquery-{version}.min.js.

like image 57
Hao Kung Avatar answered Oct 19 '22 05:10

Hao Kung


There is ignoreList, which you can clear if you need, it looks like:

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

More details: Advanced Options of ASP.NET Bundling and Minification

like image 34
webdeveloper Avatar answered Oct 19 '22 03:10

webdeveloper