27

I'm using ASP.net MVC 4. Like the question states, if I put a bunch of JS files (or CSS, for that matter) into a bundle, will it automatically be minified? For example, should my bundle read:

        bundles.Add(new ScriptBundle("~/bundles/exampleBundle").Include(
            "~/Scripts/jquery-masked.js"
            "~/Scripts/jquery.idletimer.js"
            ));

Or should it instead include the minified files initially:

        bundles.Add(new ScriptBundle("~/bundles/exampleBundle").Include(
            "~/Scripts/jquery-masked.min.js"
            "~/Scripts/jquery.idletimer.min.js"
            ));

??

Edit: Now I am wondering if bundling the .min files instead adds any optimization. Will it increase performance including the .min files in the bundle instead of the basic files? (Maybe the "minifier function" takes some time?)

AlbatrossCafe
  • 1,710
  • 6
  • 26
  • 49

3 Answers3

31

You don't have to include minified files, that's automatically done by bundle engine. In fact, I remember including minified files caused problems (maybe this is fixed on latest mvc version)

You may think this is not working since optimizations (bundling and minifying) are only performed when debug=false on web.config.

<system.web>
     <compilation debug="false" />
</system.web>

There is a way to force optimizations even when debug = true using BundleTable.EnableOptimizations. Here you have an example

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                 "~/Scripts/jquery-{version}.js"));

    BundleTable.EnableOptimizations = true;
}
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
25

The Asp.Net bundler does bundle all scripts in the same bundle into one single file, listed in the order they are defined in the bundle. This single file is then minified and delivered to the client.

If you include both the normal and minified versions of a script in your script directory, the bundler will automatically deploy the full script during debugging sessions and the minified version during production. You should avoid referring to the minified versions of your scripts in the bundle configuration, unless you want the minified version deployed to your debug sessions.

Claies
  • 22,124
  • 4
  • 53
  • 77
  • 2
    So just to clarify, if the minified file exists in your directory, it will automatically deploy that (during production sessions) and not do any minifying-conversion on the fly? – AlbatrossCafe May 20 '15 at 17:07
  • 5
    if minified versions of all the scripts in a bundle exist, they will still be bundled into a single bundle file, but that file won't undergo any further minification. otherwise, the minified versions will be inline in the file with the non-minified files, and the file *will* undergo minification. – Claies May 20 '15 at 21:05
3

These are two different terms called bundling and minification.
Minification : is you minified versions of JS files where you compress the content by renaming the variables.
Bundling : is altogether a different thing. To reduce the network roundtrips it's better to combine everything in one file and download it on client in one request.

So you can bundle the minified version of JS to get this benefit.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • Although this is not answering the question directly, it is good explanation about the difference between the *Minification* and *Bundling* – Hakan Fıstık Jun 30 '17 at 15:21