Disable JS/CSS bundling in BundlingTransformer if debug=true

Viewed 19

I am using BundleTransformer to bundle and minify JS/CSS in a legacy ASP.NET WebForms app. However, I'd like to disable bundling if <compilation debug="true"> in Web.config.

  • Current Behavior: If debug=true, resources are still bundled together, but nothing is minified.
  • Desired Behavior: Bundling and minification only occur if debug=false. Otherwise, references to the individual scripts and stylesheets are rendered in the HTML.

Is this possible?

Below is an example of how I currently create one of the bundles:

BundleConfig.cs

var jsMinifier = new BundleTransformer.JsMin.Minifiers.CrockfordJsMinifier();
var customJsTransformer = new BundleTransformer.Core.Transformers.ScriptTransformer(jsMinifier);
var CustomJsBundle = new BundleTransformer.Core.Bundles.CustomScriptBundle("~/bundles/CustomJsBundle");

foreach (var item in jsFiles)
{
    CustomJsBundle.Include(item);
}

CustomJsBundle.Transforms.Clear();
CustomJsBundle.Transforms.Add(customJsTransformer);
BundleTable.Bundles.Add(CustomJsBundle);

Navigation.master

<%@ Master Language="C#" CodeFile="Navigation.master.cs" Inherits="Main_Nav" %>
<%@ Import Namespace= "System.Web.Optimization" %>
<html>
    <head runat="server">
        <%= Scripts.Render("~/bundles//CustomJsBundle") %> 
        <!-- if debug=true, I'd like the server to just render references to the individual script files
        instead of the bundle -->
    </head>
    <body>
        <!-- other markup -->
    </body>
</html>
0 Answers
Related