14

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.

Thewads
  • 4,953
  • 11
  • 55
  • 71

2 Answers2

13

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

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • Rudy Sorry! I edited your response by mistake instead of mine (I left some code out by mistake and pressed on "edit" to change it. My bad ;( – amhed Feb 11 '13 at 16:27
  • Thanks. I have tried both these methods, but it still seems to minify it down when building in release mode for some reason – Thewads Feb 12 '13 at 08:28
  • 1
    Rudis soln should work. If you construct a basic Bundle without specifying any transform, it will simply bundle the files as is, so this definitely should not minify your bundle. – Hao Kung Feb 20 '13 at 11:15
  • for file 'x.js' in the bundle ensure that there is NOT a 'x.min.js' file in the folder – stooboo Aug 09 '14 at 22:14
  • @stooboo why not? A normal Bundle will only include what it is given. – Rudi Visser Aug 14 '14 at 13:55
  • you're right for Bundle .. but for ScriptBundle we found that even after 'Clear'ing the transforms it still picked up the x.min.js file if it existed in the filesystem :-( – stooboo Aug 14 '14 at 17:58
  • With the basic bundle (first snippet), how do you output it on the view? `@Bundle.Render("~/bundles/toNotMinify")`, `@Bundles.Render("~/bundles/toNotMinify")` and `@Bundle("~/bundles/toNotMinify")` all don't work. – Big McLargeHuge Oct 29 '14 at 17:12
  • @Koveras the same was as every bundle. `@Scripts.Render("~/Bundles/Bundle")` – Rudi Visser Oct 30 '14 at 21:17
2

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);
amhed
  • 3,649
  • 2
  • 31
  • 56