ASP.NET捆绑包如何禁用代码压缩

202

我在我的web.config(s)中都有debug="true",但是我不想让我的bundles被压缩,但是我尝试了enableoptimisations=false,但好像无法禁用它。以下是我的代码:

//Javascript
bundles.Add(new ScriptBundle("~/bundles/MainJS")
            .Include("~/Scripts/regular/lib/mvc/jquery.validate.unobtrusive.js*")
            .Include("~/Scripts/regular/lib/mvc/jquery.validate*")
            .Include("~/Scripts/regular/lib/bootstrap.js")
            .IncludeDirectory("~/Scripts/regular/modules", "*.js", true)
            .IncludeDirectory("~/Scripts/regular/pages", "*.js", true)
            .IncludeDirectory("~/Scripts/regular/misc", "*.js", true));

//CSS
bundles.Add(new StyleBundle("~/bundles/MainCSS")
            .Include("~/Content/css/regular/lib/bootstrap.css*")
            .IncludeDirectory("~/Content/css/regular/modules", "*.css", true)
            .IncludeDirectory("~/Content/css/regular/pages", "*.css", true))

2
@RickAnd-MSFT,请求是如何在禁用缩小的情况下启用捆绑。使用web.config debug = true/false或EnableOptimizations只能同时打开或关闭两者。Martin Devillers的答案允许启用捆绑而禁用缩小。 - guymid
2
对于捆绑包中的文件'x.js',请确保该文件夹中没有'x.min.js'文件,否则即使您已删除了缩小转换,捆绑包也会提供“预”缩小文件。例如,如果您有'angular.js',请删除'angular.min.js'文件;-) - stooboo
14个回答

6

在您的项目中搜索关键字EnableOptimizations

如果找到了该关键字

BundleTable.EnableOptimizations = true;

将它变为false

这会禁用代码压缩, 并且完全禁用捆绑。


2
这会禁用代码压缩,但也会完全禁用捆绑,我认为至少应该注意到这一点。 - John Pavek

4
仅作为补充,如果您想要对一些文件进行非缩小/混淆/合并,同时允许其他文件完全捆绑和缩小,则最好选择自定义渲染器,它将读取特定包的内容并在页面中呈现文件,而不是呈现包的虚拟路径。我个人需要这样做,因为即使关闭了缩小,我的CSS文件在捆绑时也会导致IE 9出现问题。

非常感谢this article,它为我提供了代码的起点,我用它创建了CSS渲染器,它将为CSS文件呈现文件,但仍允许系统捆绑/缩小/混淆我的javascript文件。

创建静态帮助类:

using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;

namespace Helpers
{
  public static class OptionalCssBundler
  {
    const string CssTemplate = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />";

    public static MvcHtmlString ResolveBundleUrl(string bundleUrl, bool bundle)
    {
      return bundle ? BundledFiles(BundleTable.Bundles.ResolveBundleUrl(bundleUrl)) : UnbundledFiles(bundleUrl);
    }

    private static MvcHtmlString BundledFiles(string bundleVirtualPath)
    {
      return new MvcHtmlString(string.Format(CssTemplate, bundleVirtualPath));
    }

    private static MvcHtmlString UnbundledFiles(string bundleUrl)
    {
      var bundle = BundleTable.Bundles.GetBundleFor(bundleUrl);

      StringBuilder sb = new StringBuilder();
      var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

      foreach (BundleFile file in bundle.EnumerateFiles(new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, bundleUrl)))
      {
        sb.AppendFormat(CssTemplate + Environment.NewLine, urlHelper.Content(file.VirtualFile.VirtualPath));
      }

      return new MvcHtmlString(sb.ToString());
    }

    public static MvcHtmlString Render(string bundleUrl, bool bundle)
    {
      return ResolveBundleUrl(bundleUrl, bundle);
    }
  }

}

然后在 razor 布局文件中:
@OptionalCssBundler.Render("~/Content/css", false)

而不是标准:

@Styles.Render("~/Content/css")

我相信为JavaScript文件创建一个可选的渲染器也只需要对这个帮助程序进行少量更新。

1
运行良好。如果您希望在文件更新时更改URL,则可以将CssTemplate更改为类似于"<link href=\"{0}?f={1}\" rel=\"stylesheet\" type=\"text/css\" />"的内容,并将sb.AppendFormat行更改为类似于sb.AppendFormat(CssTemplate + Environment.NewLine, urlHelper.Content(file.VirtualFile.VirtualPath), System.IO.File.GetLastWriteTimeUtc(HttpContext.Current.Server.MapPath(file.IncludedVirtualPath)).Ticks);的内容。 - franzo
没错,我们在工作中也做了类似的事情。我们有一个名为JSVersion的公共静态字符串,放置在Global.asax类中,它提取了执行程序集的主/次/生成/修订版本。然后我们像这样引用它:<script type="text/javascript" src="Scripts/jsfile_name.js<%=Global.JSVersion%>"></script> - James Eby

1
这对于未来的某些人可能会有所帮助,因为通过VS设置时,新框架会得到默认的web.configweb.Debug.configweb.Release.config。在web.release.config中,您会发现这一行:
<compilation xdt:Transform="RemoveAttributes(debug)" />

这似乎覆盖了我所做的任何内联更改。我将此行注释掉,这样我们就可以看到“发布”版本中的非最小化代码了。


1
如果您正在使用LESS/SASS CSS转换,则可以在web.config中设置useNativeMinification选项为false以禁用缩小。对于我的目的,我只需在需要时在此处更改它,但您可以使用web.config转换始终在发布构建上启用它,或者找到一种在代码中修改它的方法。
<less useNativeMinification="false" ieCompat="true" strictMath="false"
      strictUnits="false" dumpLineNumbers="None">

提示:整个目的是查看您的CSS,您可以在浏览器检查工具中执行此操作,也可以只需打开文件。当启用捆绑时,每次编译时该文件名都会更改,因此我将以下内容放在页面顶部,以便每次更改时都可以在新的浏览器窗口中轻松查看我的已编译CSS。

@if (Debugger.IsAttached) 
{
    <a href="@Styles.Url(ViewBag.CSS)" target="css">View CSS</a>
}

这将是一个动态URL,类似于https://example.com/Content/css/bundlename?v=UGd0FjvFJz3ETxlNN9NVqNOeYMRrOkQAkYtB04KisCQ1


更新:我创建了一个web.config转换,在部署/发布构建期间将其设置为true。
  <bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
    <less xdt:Transform="Replace" useNativeMinification="true" ieCompat="true" strictMath="false" strictUnits="false" dumpLineNumbers="None">
      <jsEngine name="MsieJsEngine" />
    </less>
  </bundleTransformer>

1
文件名在每次编译时不会改变。它是基于文件内容的,因此只要文件发生更改,它就会改变。 - Jim Raden

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接