How to disable Javascript/CSS minification in ASP.NET MVC 4 Beta
In Global.asax.cs
#if DEBUG
foreach (var bundle in BundleTable.Bundles)
{
bundle.Transform = new NoTransform();
}
#endif
ASP.NET MVC 4 app with bundling and minification, why is minification enabled in debug mode?
The red flag is with the link/script tags rendered in your HTML:
These should contain a version hashcode if you are using Script/Style.Render, i.e.
< script src="/bundles/jquery?v=wvLq7H7qEZB2giyIRn7aEZAxhHOb2RfTYYh2HMd9EqM1"/>
To get the debug/release behavior that the MVC4 templates are using, you must use the Script/Style.Render methods as well. When calling these methods, you must pass virtual bundle paths, in your example:
@Styles.Render("~/content/css")
@Scripts.Render("~/bundles/jquery")
In debug mode, you should not get link/script tags pointing at the bundle (which will always be minified/bundled). Instead you should be getting script/link tags to the individual resources in debug mode.
MVC4 Beta Minification and Bundling: Ordering files and debugging in browser
To temporarily get non-minified output use this
public class NonMinifyingJavascript : IBundleTransform
{
public void Process(BundleContext context, BundleResponse bundle)
{
if(bundle == null)
{
throw new ArgumentNullException("bundle");
}
context.HttpContext.Response.Cache.SetLastModifiedFromFileDependencies();
foreach(FileInfo file in bundle.Files)
{
HttpContext.Current.Response.AddFileDependency(file.FullName);
}
bundle.ContentType = "text/javascript";
//base.Process(context, bundle);
}
}
If you wanted it based totally on a config setting, I imagine you could create an IBundle transform that delegates to this one or JsMinify depending on your config setting
In order to control the ordering of the javascript files you need to use the BundleFileSetOrdering
var javascriptBundle = new Bundle("~/site/js", new NonMinifyingJavascript());
//controls ordering for javascript files, otherwise they are processed in order of AddFile calls
var bootstrapOrdering = new BundleFileSetOrdering("bootstrap");
//The popover plugin requires the tooltip plugin
bootstrapOrdering.Files.Add("bootstrap-tooltip.js");
bootstrapOrdering.Files.Add("bootstrap-popover.js");
BundleTable.Bundles.FileSetOrderList.Add(bootstrapOrdering);
javascriptBundle.AddDirectory("~/Scripts", "bootstrap-*.js");
Why is MVC4 bundling and minification making my files bigger?
Most likely what you are seeing here is some of the debug/release 'magic' that comes from the FileExtensionReplacementList
.
Let's take jQuery for example. Typically in your scripts folder you will see two copies of each file, i.e. jquery-1.6.2.js
and jquery-1.6.2.min.js
.
By default optimization will use the min.js
version when debug=false
, and use the regular jquery-1.6.2.js
when debug=true
, since typically that makes debugging easier (no bundling and no minification of the bundle).
This file selection 'magic' is controlled via the FileExtensionReplacementList
on BundleCollection
.
In the next release (RTM), there will be a bit more granularity in this list, as typically developers will want to target when these are should be used, i.e.
list.Add("min", OptimizationMode.WhenEnabled);
list.Add("debug", OptimizationMode.WhenDisabled);
Bundling and minification is sending old styles
Don't I feel like the bonehead. Apparently I am working too many hours. I had a style.min.css file that the optimizer was using, but I was making changes to style.css. I even knew that is what the optimizer did, but I... Well no excuses. It was a brain fart.
Cassette bundles vs MVC4 bundles
Information about ASP.NET MVC bundling is here: http://weblogs.asp.net/scottgu/archive/2011/11/27/new-bundling-and-minification-support-asp-net-4-5-series.aspx.
ASP.NET is adding a feature that makes it easy to “bundle” or
“combine” multiple CSS and JavaScript files into fewer HTTP requests.
This causes the browser to request a lot fewer files and in turn
reduces the time it takes to fetch them.The next release of ASP.NET is also adding a new feature that makes it
easy to reduce or “minify” the download size of the content as well.
Looks like it's essentially the same thing as Cassette. All other things being equal, use the solution that is native to ASP.NET MVC.
Bundling and Minification in Release Mode deploys source code
Answer 1: Bundling and minification are done at run-time, not at build, compile, or deploy time. The "bundle" that is downloaded is a virtual file, it doesn't actually exist anywhere on disk. Thus the original "source" files are needed.
Answer 2: Sorry but your understanding is not correct. The non-bundled/minified files are required in Release mode as they form the basis for creating the bundled/minified payload that is sent to the client.
Related Topics
Detect Failure to Load Contents of an Iframe
IE8 Alternative to Window.Scrolly
Play Pause HTML5 Video JavaScript
Div Vertical Scroll Bar on Left
HTML Canvas: How to Draw a Flipped/Mirrored Image
Googlemaps Does Not Load on Page Load
D3 Bar Graph Example Not Working Locally
Jquery Position Div Fixed at Top on Scroll
Ie Not Allowing Onclick Event on Dynamically Created Dom 'A' Element
I Can't Get My Slick Slider to Work at All
Setting Multiple Style.Background Values
How to Extract Color Values from Rgb String in JavaScript
Syncing Column Width of Between Tables in Two Different Frames, etc
Update Three.Js Raycaster After CSS Tranformation
Getting Absolute Position of an Element Relative to Browser
How to Get the Real (Unscaled) Size of an Embedded Image in the Svg Document!
Fixed/Absolute Positioning Neglected in iOS When Focusing on Input