Minified script only in MVC4 BundleConfig

喜你入骨 提交于 2019-12-10 02:54:14

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/13087527/minified-script-only-in-mvc4-bundleconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!