Don't uglify certain file when using Microsoft Web Optimization Framework Don't uglify certain file when using Microsoft Web Optimization Framework asp.net asp.net

Don't uglify certain file when using Microsoft Web Optimization Framework


Bundles transform each file by using a collection of IItemTransform and concatenate result. Then it transform the result by using a collection of IBundleTransform.

The default script bundle minifies the complete bundle content by using JsMinify (which implements IBundleTransform).

So to prevent some file from minifying, you have to create your own IBundleBuilder which minifies the bundle file by file by using an IItemTransform.

public class CustomScriptBundle : Bundle{    public CustomScriptBundle(string virtualPath)        : this(virtualPath, null)    {    }    public CustomScriptBundle(string virtualPath, string cdnPath)        : base(virtualPath, cdnPath, null)    {        this.ConcatenationToken = ";" + Environment.NewLine;        this.Builder = new CustomBundleBuilder();    }}public class CustomBundleBuilder : IBundleBuilder{    internal static string ConvertToAppRelativePath(string appPath, string fullName)    {        return (string.IsNullOrEmpty(appPath) || !fullName.StartsWith(appPath, StringComparison.OrdinalIgnoreCase) ? fullName : fullName.Replace(appPath, "~/")).Replace('\\', '/');    }    public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<BundleFile> files)    {        if (files == null)            return string.Empty;        if (context == null)            throw new ArgumentNullException("context");        if (bundle == null)            throw new ArgumentNullException("bundle");        StringBuilder stringBuilder = new StringBuilder();        foreach (BundleFile bundleFile in files)        {            bundleFile.Transforms.Add(new CustomJsMinify());            stringBuilder.Append(bundleFile.ApplyTransforms());            stringBuilder.Append(bundle.ConcatenationToken);        }        return stringBuilder.ToString();    }}public class CustomJsMinify : IItemTransform{    public string Process(string includedVirtualPath, string input)    {        if (includedVirtualPath.EndsWith("min.js", StringComparison.OrdinalIgnoreCase))        {            return input;        }        Minifier minifier = new Minifier();        var codeSettings = new CodeSettings();        codeSettings.EvalTreatment = EvalTreatment.MakeImmediateSafe;        codeSettings.PreserveImportantComments = false;        string str = minifier.MinifyJavaScript(input, codeSettings);        if (minifier.ErrorList.Count > 0)            return "/* " + string.Concat(minifier.Errors) + " */";        return str;    }}

Then use the CustomScriptBundle instead of ScriptBundle

public static void RegisterBundles(BundleCollection bundles){    bundles.Add(new CustomScriptBundle("~/bundles/Sample").Include(                "~/Scripts/a.js",                "~/Scripts/b.js",                "~/Scripts/c.js"));}

If you provide a min.js file it will be used instead of minifying it.


The optimization framework takes filenames into account.

Try to rename your *.js file to recaptcha_ajax.min.js. If I'm correct, then it should skip the uglify/minify process.

Reference data : http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification(scroll down a bit to find this quote:)

The preceding code creates a new JavaScript bundle named ~/bundles/jquery that includes all the appropriate (that is debug or minified but not .vsdoc) files in the Scripts folder that match the wild card string "~/Scripts/jquery-{version}.js". For ASP.NET MVC 4, this means with a debug configuration, the file jquery-1.7.1.js will be added to the bundle. In a release configuration, jquery-1.7.1.min.js will be added. The bundling framework follows several common conventions such as:

  • Selecting “.min” file for release when “FileX.min.js” and “FileX.js” exist.
  • Selecting the non “.min” version for debug.
  • Ignoring “-vsdoc” files (such as jquery-1.7.1-vsdoc.js), which are used only by IntelliSense.


if you are bundling .js files in the ScriptManager you will notice that scripts are loaded in the <form>, not the <head> so pulling it out of the bundle will load that file before the others, which is not good if it's dependent other things in the bundle

Here is an example of a library that needs to be added to the bundle in a specific order.
This is added to App_Start/BundleConfig.vb

'############################################################' This is a ScriptManager Resource Definition ' for use in a ScriptManager mapping'############################################################    Dim ResourceDef as ScriptResourceDefinition = New ScriptResourceDefinition()    Dim ResourceName as  String = "ColorBox"    'add the Resource Definition details    ResourceDef.Path = "~/Scripts/colorbox/jquery.colorbox-min.js"    ResourceDef.DebugPath = "~/Scripts/colorbox/jquery.colorbox.js"    ScriptManager.ScriptResourceMapping.AddDefinition(ResourceName, ResourceDef)'############################################################

Note the use of ResourceDef.Path and ResourceDef.DebugPath. Which file gets included will depend on you making a Debug or Release publish

  • Debug: no "uglification"
  • Release: Total "uglification"

This is my ScriptManager bundle, note the placement of ColorBox, position means a lot:

<asp:ScriptManager runat="server" >    <Scripts>        <asp:ScriptReference Name="jquery" />        <asp:ScriptReference Name="jquery.ui.combined" />        <asp:ScriptReference Name="ColorBox" />                  <asp:ScriptReference Name="Infragistics" />         <asp:ScriptReference Name="MsAjaxBundle" />        <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />        <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />        <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />        <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />        <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />        <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />        <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />        <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />        <asp:ScriptReference Name="WebFormsBundle" />    </Scripts></asp:ScriptManager>