reinhard
reinhard

Reputation: 818

Basic Bundling, Minification in ASP.NET

ASP.NET MVC 4.0 application - Visual Studio 2012

I cannot get bundling and minification to work in release mode.

My basic non-understanding is:

Do I have to provide the *.MIN.css, *.MIN.js files beforehand, or should VS minify the files on its own? (ie: I provide a mcimagemanager.js and VS makes a mcimagemanager.MIN.js out of it) ???

Here is a code snippet - which gets called in Global.asax:

 public static void RegisterBundles(BundleCollection bundles)
    {
       var im = new ScriptBundle("~/bundles/MCImageManager").Include(
                    "~/Scripts/tinymce/plugins/imagemanager/js/mcimagemanager.js"
                    );
        bundles.Add(im);
    }

it works fine in Debug - not in Release-mode

Thank you!

Upvotes: 1

Views: 1527

Answers (2)

MikeSmithDev
MikeSmithDev

Reputation: 15797

No you don't need to provide .min file, nor will the bundler create that version (not something you see in the folder, at least).

The difference is this. Let's say you have both jquery-1.9.1.js and jquery-1.9.1.min.js in your scripts folder.

Debug mode will use jquery-1.9.1.js as the source script, and no it won't be minified or bundled, as the whole bundling/minification is disabled in debug mode (though you can override this).

Release mode will use jquery-1.9.1.min.js AND bundle it with other scripts for that bundle.

If you only have the one file, jquery-1.9.1.js, Release mode will use it and minify and bundle it.

Debug mode will NOT use .min files. So if you use a wildcard to include all files for a scripts directory, your .min files will not be included.

Debug mode, if you look at the rendered HTML, will reference all script files in the bundle individually. In Release mode, there will only be one script reference (with a querystring for versioning) per bundle.

Other relevant reading/posts:

Scripts.Render using outdated javascript file

Force ASP.Net MVC Bundle to render the javascript files in a certain order

Upvotes: 3

Peter Stegnar
Peter Stegnar

Reputation: 12925

Bundling and minification framework do also minification itself. So you just provide plain JavaScript/CSS files. It handle on the one hand files itself in the other hand it handle the registration in the view, for eample: @Scripts.Render("~/bundles/jquery"). So it "know" what file needs to be included in the view, weather it is "normal" or "min" version.

More on that topic you can find in this nice Exercise: http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetandvisualstudio_topic5.aspx

However this is might even better resource for the MVC oriented application: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

Upvotes: 1

Related Questions