我有一个奇怪的问题与mvc4捆绑不包括文件扩展名.min.js

在BundleConfig类中,我声明

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"));            
}

在我看来,我宣布

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

当它渲染时,它只渲染

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

如果我将jquery.tmpl.min.js重命名为jquery.tmpl.js(并相应地更新包中的路径),两个脚本都将正确呈现。

是否有一些配置设置导致它忽略'.min.js'文件?


当前回答

要呈现*.min.js文件,必须禁用BundleTable。EnableOptimizations,这是一个应用于所有捆绑包的全局设置。

如果您只想为特定的包启用优化,您可以创建自己的ScriptBundle类型,在枚举包中的文件时临时启用优化。

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;
        }
    }
}

对于始终应该使用最小化文件的包,无论是否存在非最小化文件,都应使用OptimizedScriptBundle而不是ScriptBundle。

ASP的Kendo UI示例。NET MVC,它仅作为一个缩小文件的集合分发。

bundles.Add(new OptimizedScriptBundle("~/bundles/kendo")
        .Include(
            "~/Scripts/kendo/kendo.all.*",
            "~/Scripts/kendo/kendo.aspnetmvc.*",
            "~/Scripts/kendo/cultures/kendo.*",
            "~/Scripts/kendo/messages/kendo.*"));

其他回答

微软暗示了以下行为(我更喜欢在我的项目中遵循它):

短的版本

在你的项目中,一个脚本的调试版和缩小版都在同一个文件夹下: script.js script.min.js 在代码中只向包中添加script.js。

因此,你会自动在DEBUG模式下包含script.js,在RELEASE模式下包含script.min.js。

长版本

你也可以有。debug.js版本。在这种情况下,文件包含在DEBUG的以下优先级中:

script.debug.js script.js

发布:

script.min.js script.js

note

顺便说一下,在MVC4中使用.min版本脚本的唯一原因是缩小版本不能自动处理。例如下面的代码不能被自动混淆:

if (DEBUG) console.log("Debug message");

在所有其他情况下,您可以只使用脚本的调试版本。

我最初发布的解决方案是有问题的(是一个肮脏的黑客)。正如许多评论者指出的那样,Microsoft.AspNet.Web.Optimization包中的调整行为已经改变,并且调整不再有效。现在我无法在1.1.3版本的包中重现这个问题。

请参阅System.Web.Optimization.BundleCollection的源代码(例如,您可以使用dotPeek),以便更好地理解您将要做的事情。 请阅读Max Shmelev的回答。

最初的回答:

要么将.min.js重命名为.js,要么做类似

    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
     }

对于我的案例,我使用的是(太棒了!)Knockout.js库作为调试/非版本:

"~/Scripts/knockout-{Version}.js"
"~/Scripts/knockout-{Version}.Debug.js"

为了做到这一点,我在我的Bundle中包含了“Knockout-{Version}.js”(非调试),并获得了.debug。js文件在调试模式。

一个简单的方法就是重命名。min文件,比如你有abc.min.css,而不是把这个文件重命名为abc_min.css,然后把它添加到你的bundle中。我知道这不是正确的方法,但这只是一个临时的解决办法。谢谢大家,编码愉快。

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.