ASP.NET捆绑和缩小-包括用于生产捆绑包的已缩小文件和用于开发的未缩小文件 [英] ASP.NET Bundling and Minification - Including Already Minified Files for Production Bundles and Unminified Files for Development

查看:70
本文介绍了ASP.NET捆绑和缩小-包括用于生产捆绑包的已缩小文件和用于开发的未缩小文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获得有关ASP.NET MVC捆绑和缩小的一些专家建议.我的项目脚本文件中同时包含未最小化(.js)和最小化版本(.min.js).我将它们包含在脚本包中,如下所示:

I want some expert advice on ASP.NET MVC Bundling and Minification. I have in my project script files that have both unminified (.js) and minified versions (.min.js). I have included them in my script bundle as follows:

bundles.Add(new ScriptBundle("~/bundles/layout").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/lib/errorhandling.js",
                    "~/Scripts/lib/errorhandling.min.js",
                    "~/Scripts/vendor/modernizr.custom.js",
                    "~/Scripts/vendor/modernizr.custom.min.js",
                    "~/Scripts/toastr.js",
                    "~/Scripts/toastr.min.js"));

似乎该捆绑包确实只包含每个脚本文件一次,而不包含两次.我已经为开发和生产确认了这一点. (作为附带说明,在开发中,即当debug = true时,不渲染捆绑包,但将文件作为单独的脚本标记包括在内.这也是我想要的行为.)

It seems that the bundle indeed contains only once each script file, not twice. I have confirmed this both for development and production. (As a side note, in development, that is, when debug=true, the bundles are not rendered but the files are included as separate script tags. This is the desired behaviour for me, as well.)

我的问题是:

(1)这是包含已最小化的文件(用于生产设置)和未最小化的文件(用于开发)的最佳和推荐方法吗?

(1) Is this the best and recommended way to include already minified files for production setup and unminified files for development?

(2)ASP.NET是否尝试最小化生产中的整个捆绑包(即使已经缩小了)?如果是,防止ASP.NET试图缩小捆绑包的最佳方法是什么?

(2) Does ASP.NET try to minify the whole bundle in production (even though it is already minified)? If yes, what is the best way to prevent ASP.NET from trying to minify the bundle?

提前谢谢!

推荐答案

无需在脚本包中专门包含缩小的版本.默认情况下,MVC将搜索带有.min.js的匹配文件并包含该文件(不完全确定它是否尝试进一步缩小).如果没有,它将创建一个缩小版本.您可以通过将以下内容添加到BundleConfig.cs

There is no need to specifically include the minified versions in your script bundle. By default, MVC will search for a matching file with .min.js and include that (not entirely sure if it trys to minify further). If not, it creates a minified version. You can test this by adding the following to BundleConfig.cs

using System.Web.Optimization;

然后在末尾添加以下内容以覆盖开发中的debug=true

then adding the following at the end to override debug=true in development

BundleTable.EnableOptimizations = true;

来自 MS文档

捆绑框架遵循几种常见约定,例如:

The bundling framework follows several common conventions such as:

  • 在"FileX.min.js"和"FileX.js"时选择要发布的".min"文件 存在.
  • 选择非".min"版本进行调试.
  • 忽略仅由以下人员使用的"-vsdoc"文件(例如jquery-1.7.1-vsdoc.js) IntelliSense.
  • Selecting ".min" file for release when "FileX.min.js" and "FileX.js" exist.
  • Selecting the non ".min" version for debug.
  • Ignoring "-vsdoc" files (such as jquery-1.7.1-vsdoc.js), which are used only by IntelliSense.

这篇关于ASP.NET捆绑和缩小-包括用于生产捆绑包的已缩小文件和用于开发的未缩小文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆