Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC Bundles and Minification

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.

    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.

like image 914
Thewads Avatar asked Feb 11 '13 16:02

Thewads


People also ask

What is bundling and minification in ASP.NET MVC?

Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)

What is bundling and minification in asp net core?

What is bundling and minification. Bundling and minification are two distinct performance optimizations you can apply in a web app. Used together, bundling and minification improve performance by reducing the number of server requests and reducing the size of the requested static assets.

Which bundle is used for minification of styles in MVC under System web optimization?

StyleBundle: StyleBundle is responsible for CSS minification of single or multiple style sheet files.

What is the advantage of bundling in MVC?

Bundling and Minification provide us a way to both reduce the number of requests needed to get JS and CSS resource files and reduce the size of the files themselves, thereby improving the responsiveness of our apps. They are nice little optimizations for our MVC apps that can improve performance and add responsiveness.


2 Answers

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

_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 :)

like image 70
Rudi Visser Avatar answered Sep 22 '22 14:09

Rudi Visser


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

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);
like image 21
amhed Avatar answered Sep 25 '22 14:09

amhed