Reputation: 1842
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?)
Upvotes: 29
Views: 18558
Reputation: 14677
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.
Upvotes: 4
Reputation: 68400
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;
}
Upvotes: 32
Reputation: 22323
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.
Upvotes: 26