C# 捆绑程序不包括 .min 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11980458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:56:27  来源:igfitidea点击:

Bundler not including .min files

c#javascriptasp.net-mvc-4bundling-and-minification

提问by Fatal

I have a weird issue with the mvc4 bundler not including files with extension .min.js

我对 mvc4 bundler 有一个奇怪的问题,不包括扩展名为 .min.js 的文件

In my BundleConfig class, I declare

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

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.

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

Is there some config setting that is causing it to ignore '.min.js' files?

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

采纳答案by Pavel

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.

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

Please see sources of System.Web.Optimization.BundleCollection (you can use dotPeekfor example) for better understanding of what you are about to do. Also read Max Shmelev's answer.

请参阅 System.Web.Optimization.BundleCollection 的来源(例如,您可以使用dotPeek)以更好地了解您将要做什么。另请阅读Max Shmelev 的回答

Original answer:

原答案

Either rename .min.js to .js or do something like

将 .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
     }

回答by Max Shmelev

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.jsto a bundle in your code.
  1. 您的项目中的同一文件夹下有调试版本和缩小版本的脚本:
    • 脚本.js
    • 脚本.min.js
  2. 您只将script.js添加到代码中的包中。

As a result you will automaticallyhave the script.jsincluded in DEBUGmode and script.min.jsin RELEASEmode.

因此,您将自动script.js包含在DEBUG模式中,并将script.min.js 包含RELEASE模式中。

long version

长版

You can have also .debug.jsversion. In that case the file is included in the following priority in DEBUG:

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

  1. script.debug.js
  2. script.js
  1. 脚本.debug.js
  2. 脚本.js

in RELEASE:

发布中:

  1. script.min.js
  2. script.js
  1. 脚本.min.js
  2. 脚本.js

note

笔记

And by the way, the only reason to have a .minversions 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:

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

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

In all the other cases you can go with just a debug version of your script.

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

回答by AcidPAT

For my case, I was using the (wonderful!) Knockout.js library which comes as Debug/Non versions:

就我而言,我使用的是(很棒!)Knockout.js 库,它作为调试/非版本提供:

"~/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.

为了完成这项工作,我在我的 Bundle 中包含了“Knockout-{Version}.js”(非调试)并获得了 .debug。调试模式下的js文件。

回答by user1819794

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

伙计们,我会简单地保留它,直到微软让它一起行动

Try this

尝试这个

In RegisterBundles create this bundle for Kendo

在 RegisterBundles 中为 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:

在 _Layout.cshtml 中输入:

@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

当微软修复他们的 MVC 4 代码时修复它

回答by Amirhossein Mehrvarzi

Bundler have a lot of benefits check this pageBUT:

Bundler 有很多好处检查这个页面但是

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.

Microsoft MVC4 平台认为每个脚本或样式包至少有缩小版和未缩小版(也可以使用其他文件,如 Debug 和 vsdoc)。因此,在只有这些文件之一的情况下,我们会遇到麻烦。

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

您可以永久更改 web.config 文件中的调试状态以处理输出:

<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!

查看输出变化!文件过滤已更改。为了达到目的,我们必须改变将改变应用程序逻辑的忽略大小写过滤!

回答by joelfp

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.

如果您拥有的只是文件的缩小版本,那么我找到的最简单的解决方案是复制缩小的文件,从复制的文件名中删除 .min,然后在您的包中引用非缩小的文件名。

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:

例如,假设您购买了一个 js 组件,他们给了您一个名为 some-lib-3.2.1.min.js 的文件。要在捆绑中使用此文件,请执行以下操作:

  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"
    ));
    
  1. 复制 some-lib-3.2.1.min.js 并将复制的文件重命名为 some-lib-3.2.1.js。在您的项目中包含这两个文件。

  2. 引用包中的非缩小文件,如下所示:

    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.

仅仅因为名称中没有 'min' 的文件实际上被缩小了不应该导致任何问题(除了它基本上不可读的事实)。它仅用于调试模式并作为单独的脚本写出。当不处于调试模式时,预编译的 min 文件应包含在您的包中。

回答by R K Sharma

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.

一个简单的方法就是重命名 .min 文件,例如你有 abc.min.css 而不是简单地将此文件重命名为 abc_min.css 并将其添加到你的包中。我知道这不是正确的方法,而只是一个临时解决方案。感谢和快乐编码。

回答by Ilya Chernomordik

I have found a good solution that works at least in MVC5, you can just use Bundleinstead of ScriptBundle. It does not have the smart behavior of ScriptBundlethat we don't like (ignoring .min, etc.) in this case. In my solution I use Bundlefor 3d party scripts with .min and .map files and I use ScriptBundlefor 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.

我已经找到了一个很好的解决方案,在MVC5至少作品,你可以用Bundle代替ScriptBundleScriptBundle在这种情况下,它没有我们不喜欢的智能行为(忽略 .min 等)。在我的解决方案中,我将Bundle.min 和 .map 文件用于 3d 派对脚本,并ScriptBundle用于我们的代码。我没有注意到这样做的任何缺点。要使其以这种方式工作,您需要添加原始文件,例如 angular.js,它将在调试中加载 angular.js,并将在发布模式下捆绑 angular.min.js。

回答by Steven Liekens

To render *.min.jsfiles, you must disable BundleTable.EnableOptimizations, which is a global setting that applies to all bundles.

要渲染*.min.js文件,您必须禁用BundleTable.EnableOptimizations,这是适用于所有包的全局设置。

If you want to enable optimizations for specific bundles only, you can create your own ScriptBundletype that temporarily enables optimizations when enumerating files in the bundle.

如果您只想为特定包启用优化,您可以创建自己的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;
        }
    }
}

Use OptimizedScriptBundleinstead of ScriptBundlefor bundles whose minified file should always be used, regardless of whether a non-minified file exists.

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

Example for Kendo UI for ASP.NET MVC, which is distributed as a collection of only minified files.

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

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