ASP.Net MVC捆绑和缩小

时间:2022-01-14 01:50:00

When moving from development to a production environment I have run into some problems with the way in which my javascript files are being minified. It seems that some do not minify properly, and so I have been looking around to find a way to not minify a specific bundle.

从开发转移到生产环境时,我的javascript文件被缩小的方式遇到了一些问题。似乎有些人没有正确缩小,所以我一直在四处寻找一种不缩小特定束的方法。

    public static void RegisterBundles(BundleCollection _bundles)
    {
        _bundles.Add(new ScriptBundle("~/bundles/toNotMinify").Include(
            "~/Scripts/xxxxxx.js"
            ));

        _bundles.Add(new ScriptBundle("~/bundles/toMinify").Include(
            "~/Scripts/yyyyy.js"
            ));
        etc..

This is the basic layout in my bundle config class. I want to find a way in which to have all of my bundles minified, apart from the first one. Is this possible? So far the only solution I have found to achieve something similar is to turn off minification globally.

这是我的bundle配置类中的基本布局。我希望找到一种方法,让我的所有捆绑包都缩小,除了第一个。这可能吗?到目前为止,我发现实现类似目标的唯一解决方案是在全球范围内关闭缩小。

2 个解决方案

#1


12  

You have a couple of options, you can either replace your use of ScriptBundle with Bundle as in this example:

您有两个选项,您可以使用Bundle替换ScriptBundle的使用,如下例所示:

_bundles.Add(new Bundle("~/bundles/toNotMinify").Include(
    "~/Scripts/xxxxxx.js"
));

.. or you could disable all transformations on a newly created bundle, like so:

..或者您可以禁用新创建的包上的所有转换,如下所示:

var noMinify = new ScriptBundle("~/bundles/toNotMinify").Include(
    "~/Scripts/xxxxxx.js"
);
noMinify.Transforms.Clear();
_bundles.Add(noMinify);

Obviously the first solution is much prettier :)

显然第一个解决方案更漂亮:)

#2


2  

You just have to declare a generic Bundle object and specify the transforms you need:

您只需要声明一个通用的Bundle对象并指定所需的转换:

var dontMinify = new Bundle("~/bundles/toNotMinify").Include(
                                        "~/Scripts/xxxxx.js");
            bundles.Add(dontMinify);

            var minify = new Bundle("~/bundles/toNotMinify").Include(
                "~/Scripts/yyyyyy.js");
            minify.Transforms.Add(new JsMinify());
            bundles.Add(minify);

#1


12  

You have a couple of options, you can either replace your use of ScriptBundle with Bundle as in this example:

您有两个选项,您可以使用Bundle替换ScriptBundle的使用,如下例所示:

_bundles.Add(new Bundle("~/bundles/toNotMinify").Include(
    "~/Scripts/xxxxxx.js"
));

.. or you could disable all transformations on a newly created bundle, like so:

..或者您可以禁用新创建的包上的所有转换,如下所示:

var noMinify = new ScriptBundle("~/bundles/toNotMinify").Include(
    "~/Scripts/xxxxxx.js"
);
noMinify.Transforms.Clear();
_bundles.Add(noMinify);

Obviously the first solution is much prettier :)

显然第一个解决方案更漂亮:)

#2


2  

You just have to declare a generic Bundle object and specify the transforms you need:

您只需要声明一个通用的Bundle对象并指定所需的转换:

var dontMinify = new Bundle("~/bundles/toNotMinify").Include(
                                        "~/Scripts/xxxxx.js");
            bundles.Add(dontMinify);

            var minify = new Bundle("~/bundles/toNotMinify").Include(
                "~/Scripts/yyyyyy.js");
            minify.Transforms.Add(new JsMinify());
            bundles.Add(minify);